4 /* Test g_warn macros */
8 if (!g_test_undefined ())
11 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
15 g_test_trap_assert_failed();
16 g_test_trap_assert_stderr ("*WARNING*test_warnings*should not be reached*");
18 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
20 g_warn_if_fail (FALSE);
22 g_test_trap_assert_failed();
23 g_test_trap_assert_stderr ("*WARNING*test_warnings*runtime check failed*");
26 static guint log_count = 0;
29 log_handler (const gchar *log_domain,
30 GLogLevelFlags log_level,
34 g_assert_cmpstr (log_domain, ==, "bu");
35 g_assert_cmpint (log_level, ==, G_LOG_LEVEL_INFO);
40 /* test that custom log handlers only get called for
41 * their domain and level
44 test_set_handler (void)
48 id = g_log_set_handler ("bu", G_LOG_LEVEL_INFO, log_handler, NULL);
50 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT))
52 g_log ("bu", G_LOG_LEVEL_DEBUG, "message");
53 g_log ("ba", G_LOG_LEVEL_DEBUG, "message");
54 g_log ("bu", G_LOG_LEVEL_INFO, "message");
55 g_log ("ba", G_LOG_LEVEL_INFO, "message");
57 g_assert_cmpint (log_count, ==, 1);
60 g_test_trap_assert_passed ();
62 g_log_remove_handler ("bu", id);
66 test_default_handler (void)
68 if (g_test_undefined ())
70 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
75 g_test_trap_assert_failed ();
76 g_test_trap_assert_stderr ("*ERROR*message1*");
78 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
80 g_critical ("message2");
83 g_test_trap_assert_failed ();
84 g_test_trap_assert_stderr ("*CRITICAL*message2*");
86 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
88 g_warning ("message3");
91 g_test_trap_assert_failed ();
92 g_test_trap_assert_stderr ("*WARNING*message3*");
95 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
97 g_message ("message4");
100 g_test_trap_assert_passed ();
101 g_test_trap_assert_stderr ("*Message*message4*");
103 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT))
105 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "message5");
108 g_test_trap_assert_passed ();
109 g_test_trap_assert_stdout_unmatched ("*INFO*message5*");
111 g_setenv ("G_MESSAGES_DEBUG", "foo bar baz", TRUE);
112 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT))
114 g_log ("bar", G_LOG_LEVEL_INFO, "message5");
117 g_test_trap_assert_passed ();
118 g_test_trap_assert_stdout ("*INFO*message5*");
120 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT))
122 g_log ("baz", G_LOG_LEVEL_DEBUG, "message6");
125 g_test_trap_assert_passed ();
126 g_test_trap_assert_stdout ("*DEBUG*message6*");
128 g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
129 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT))
131 g_log ("foo", G_LOG_LEVEL_DEBUG, "6");
132 g_log ("bar", G_LOG_LEVEL_DEBUG, "6");
133 g_log ("baz", G_LOG_LEVEL_DEBUG, "6");
136 g_test_trap_assert_passed ();
137 g_test_trap_assert_stdout ("*DEBUG*6*6*6*");
139 g_unsetenv ("G_MESSAGES_DEBUG");
140 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT))
142 g_log (G_LOG_DOMAIN, 1<<10, "message7");
145 g_test_trap_assert_passed ();
146 g_test_trap_assert_stdout ("*LOG-0x400*message7*");
149 /* test that setting levels as fatal works */
151 test_fatal_log_mask (void)
153 GLogLevelFlags flags;
155 if (!g_test_undefined ())
158 flags = g_log_set_fatal_mask ("bu", G_LOG_LEVEL_INFO);
159 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT))
160 g_log ("bu", G_LOG_LEVEL_INFO, "fatal");
161 g_test_trap_assert_failed ();
162 g_log_set_fatal_mask ("bu", flags);
165 static gint my_print_count = 0;
167 my_print_handler (const gchar *text)
173 test_print_handler (void)
175 GPrintFunc old_print_handler;
177 old_print_handler = g_set_print_handler (my_print_handler);
178 g_assert (old_print_handler == NULL);
182 g_assert_cmpint (my_print_count, ==, 1);
184 g_set_print_handler (NULL);
188 test_printerr_handler (void)
190 GPrintFunc old_printerr_handler;
192 old_printerr_handler = g_set_printerr_handler (my_print_handler);
193 g_assert (old_printerr_handler == NULL);
196 g_printerr ("bu ba");
197 g_assert_cmpint (my_print_count, ==, 1);
199 g_set_printerr_handler (NULL);
202 static char *fail_str = "foo";
203 static char *log_str = "bar";
206 good_failure_handler (const gchar *log_domain,
207 GLogLevelFlags log_level,
211 g_test_message ("The Good Fail Message Handler\n");
212 g_assert ((char *)user_data != log_str);
213 g_assert ((char *)user_data == fail_str);
219 bad_failure_handler (const gchar *log_domain,
220 GLogLevelFlags log_level,
224 g_test_message ("The Bad Fail Message Handler\n");
225 g_assert ((char *)user_data == log_str);
226 g_assert ((char *)user_data != fail_str);
232 test_handler (const gchar *log_domain,
233 GLogLevelFlags log_level,
237 g_test_message ("The Log Message Handler\n");
238 g_assert ((char *)user_data != fail_str);
239 g_assert ((char *)user_data == log_str);
245 if (!g_test_undefined ())
248 g_test_bug ("653052");
250 g_test_log_set_fatal_handler (good_failure_handler, fail_str);
251 g_log_set_default_handler (test_handler, log_str);
253 g_return_if_fail (0);
255 g_test_log_set_fatal_handler (bad_failure_handler, fail_str);
256 g_log_set_default_handler (test_handler, log_str);
258 g_return_if_fail (0);
262 main (int argc, char *argv[])
264 g_test_init (&argc, &argv, NULL);
265 g_test_bug_base ("http://bugzilla.gnome.org/");
267 g_test_add_func ("/logging/default-handler", test_default_handler);
268 g_test_add_func ("/logging/warnings", test_warnings);
269 g_test_add_func ("/logging/fatal-log-mask", test_fatal_log_mask);
270 g_test_add_func ("/logging/set-handler", test_set_handler);
271 g_test_add_func ("/logging/print-handler", test_print_handler);
272 g_test_add_func ("/logging/printerr-handler", test_printerr_handler);
273 g_test_add_func ("/logging/653052", bug653052);
275 return g_test_run ();