~ps-jenkins/indicator-transfer/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to tests/glib-fixture.h

  • Committer: Charles Kerr
  • Date: 2014-03-31 02:21:02 UTC
  • mto: (1.2.1 draft-2)
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: charles.kerr@canonical.com-20140331022102-aakhmppqos4w81i3
get the test coverage reports working

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
{
32
32
  private:
33
33
 
34
 
    //GLogFunc realLogHandler;
35
 
 
36
 
  protected:
37
 
 
38
 
    std::map<GLogLevelFlags,int> logCounts;
39
 
 
40
 
    void testLogCount(GLogLevelFlags log_level, int /*expected*/)
 
34
    GLogFunc realLogHandler;
 
35
 
 
36
    std::map<GLogLevelFlags,size_t> expected_log;
 
37
    std::map<GLogLevelFlags,std::vector<std::string>> log;
 
38
 
 
39
    void test_log_counts()
41
40
    {
42
 
#if 0
43
 
      EXPECT_EQ(expected, logCounts[log_level]);
44
 
#endif
45
 
 
46
 
      logCounts.erase(log_level);
 
41
      const GLogLevelFlags levels_to_test[] = { G_LOG_LEVEL_ERROR,
 
42
                                                G_LOG_LEVEL_CRITICAL,
 
43
                                                G_LOG_LEVEL_MESSAGE,
 
44
                                                G_LOG_LEVEL_WARNING };
 
45
 
 
46
      for(const auto& level : levels_to_test)
 
47
      {
 
48
        const auto& v = log[level];
 
49
        const auto n = v.size();
 
50
 
 
51
        EXPECT_EQ(expected_log[level], n);
 
52
 
 
53
        if (expected_log[level] != n)
 
54
            for (size_t i=0; i<n; ++i)
 
55
                g_message("%d %s", (n+1), v[i].c_str());
 
56
      }
 
57
 
 
58
      expected_log.clear();
 
59
      log.clear();
47
60
    }
48
61
 
49
 
  private:
50
 
 
51
62
    static void default_log_handler(const gchar    * log_domain,
52
63
                                    GLogLevelFlags   log_level,
53
64
                                    const gchar    * message,
54
65
                                    gpointer         self)
55
66
    {
56
 
      g_print("%s - %d - %s\n", log_domain, (int)log_level, message);
57
 
      static_cast<GlibFixture*>(self)->logCounts[log_level]++;
 
67
      auto tmp = g_strdup_printf ("%s:%d \"%s\"", log_domain, (int)log_level, message);
 
68
      static_cast<GlibFixture*>(self)->log[log_level].push_back(tmp);
 
69
      g_free(tmp);
58
70
    }
59
71
 
60
72
  protected:
61
73
 
 
74
    void increment_expected_errors(GLogLevelFlags level, int n=1)
 
75
    {
 
76
      expected_log[level] += n;
 
77
    }
 
78
 
62
79
    virtual void SetUp()
63
80
    {
64
81
      setlocale(LC_ALL, "C.UTF-8");
65
82
 
66
83
      loop = g_main_loop_new(nullptr, false);
67
84
 
68
 
      //g_log_set_default_handler(default_log_handler, this);
69
 
 
70
 
      // only use local, temporary settings
71
 
      //g_assert(g_setenv("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, true));
72
 
      g_assert(g_setenv("GSETTINGS_BACKEND", "memory", true));
73
 
      //g_debug("SCHEMA_DIR is %s", SCHEMA_DIR);
 
85
      g_log_set_default_handler(default_log_handler, this);
74
86
 
75
87
      g_unsetenv("DISPLAY");
76
 
 
77
88
    }
78
89
 
79
90
    virtual void TearDown()
80
91
    {
81
 
#if 0
82
 
      // confirm there aren't any unexpected log messages
83
 
      EXPECT_EQ(0, logCounts[G_LOG_LEVEL_ERROR]);
84
 
      EXPECT_EQ(0, logCounts[G_LOG_LEVEL_CRITICAL]);
85
 
      EXPECT_EQ(0, logCounts[G_LOG_LEVEL_WARNING]);
86
 
      EXPECT_EQ(0, logCounts[G_LOG_LEVEL_MESSAGE]);
87
 
      EXPECT_EQ(0, logCounts[G_LOG_LEVEL_INFO]);
88
 
#endif
 
92
      test_log_counts();
89
93
 
90
 
      // revert to glib's log handler
91
 
      //g_log_set_default_handler(realLogHandler, this);
 
94
      g_log_set_default_handler(realLogHandler, this);
92
95
 
93
96
      g_clear_pointer(&loop, g_main_loop_unref);
94
97
    }