gtestutils: fix g_test_set_nonfatal_assertions()

g_test_set_nonfatal_assertions() was a no-op, because
g_assertion_message() wasn't actually checking the
test_nonfatal_assertions flag. Fix that and add a test.

Also, g_test_set_nonfatal_assertions() has to set test_mode_fatal to
FALSE as well, or else a failed assertion will cause the test program
to abort at the end of the failed test.

Also, belatedly add this and the new g_assert_* methods to the docs.

https://bugzilla.gnome.org/show_bug.cgi?id=711800
This commit is contained in:
Dan Winship 2013-11-10 15:44:06 -05:00
parent 910191597a
commit f4c30feb95
3 changed files with 42 additions and 4 deletions

View File

@ -2966,6 +2966,7 @@ g_test_rand_double_range
g_assert
g_assert_not_reached
g_assert_cmpstr
g_assert_cmpint
g_assert_cmpuint
@ -2973,6 +2974,10 @@ g_assert_cmphex
g_assert_cmpfloat
g_assert_no_error
g_assert_error
g_assert_true
g_assert_false
g_assert_null
g_test_set_nonfatal_assertions
GTestCase
GTestSuite

View File

@ -1754,7 +1754,9 @@ g_test_failed (void)
* g_assert_true(), g_assert_false(), g_assert_null(), g_assert_no_error(),
* g_assert_error(), g_test_assert_expected_messages() and the various
* g_test_trap_assert_*() macros to not abort to program, but instead
* call g_test_fail() and continue.
* call g_test_fail() and continue. (This also changes the behavior of
* g_test_fail() so that it will not cause the test program to abort
* after completing the failed test.)
*
* Note that the g_assert_not_reached() and g_assert() are not
* affected by this.
@ -1769,6 +1771,7 @@ g_test_set_nonfatal_assertions (void)
if (!g_test_config_vars->test_initialized)
g_error ("g_test_set_nonfatal_assertions called without g_test_init");
test_nonfatal_assertions = TRUE;
test_mode_fatal = FALSE;
}
/**
@ -2271,15 +2274,23 @@ g_assertion_message (const char *domain,
" ", message, NULL);
g_printerr ("**\n%s\n", s);
g_test_log (G_TEST_LOG_ERROR, s, NULL, 0, NULL);
if (test_nonfatal_assertions)
{
g_free (s);
g_test_fail ();
return;
}
/* store assertion message in global variable, so that it can be found in a
* core dump */
if (__glib_assert_msg != NULL)
/* free the old one */
free (__glib_assert_msg);
/* free the old one */
free (__glib_assert_msg);
__glib_assert_msg = (char*) malloc (strlen (s) + 1);
strcpy (__glib_assert_msg, s);
g_test_log (G_TEST_LOG_ERROR, s, NULL, 0, NULL);
g_free (s);
_g_log_abort ();
}

View File

@ -563,6 +563,25 @@ test_dash_p (void)
g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
}
static void
test_nonfatal_subprocess (void)
{
g_test_set_nonfatal_assertions ();
g_assert_cmpint (4, ==, 5);
g_print ("The End\n");
}
static void
test_nonfatal (void)
{
g_test_trap_subprocess ("/misc/nonfatal/subprocess", 0, 0);
g_test_trap_assert_failed ();
g_test_trap_assert_stderr ("*assertion failed*4 == 5*");
g_test_trap_assert_stdout ("*The End*");
}
int
main (int argc,
char *argv[])
@ -624,5 +643,8 @@ main (int argc,
g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);
g_test_add_func ("/misc/nonfatal", test_nonfatal);
g_test_add_func ("/misc/nonfatal/subprocess", test_nonfatal_subprocess);
return g_test_run();
}