static void assert_same_output (TestSanityCheckerFixture * fixture, const gchar * expected_output_format, ...) { gboolean is_exact_match; va_list args; gchar * expected_output; va_start (args, expected_output_format); expected_output = g_strdup_vprintf (expected_output_format, args); va_end (args); is_exact_match = strcmp (fixture->output->str, expected_output) == 0; if (!is_exact_match) { GString * message; gchar * diff; message = g_string_new ("Generated output not like expected:\n\n"); diff = test_util_diff_text (expected_output, fixture->output->str); g_string_append (message, diff); g_free (diff); g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, message->str); g_string_free (message, TRUE); } g_free (expected_output); }
static void check_property (const char *output, GParamSpec *pspec, GValue *value) { GValue default_value = G_VALUE_INIT; char *v, *dv, *msg; if (g_param_value_defaults (pspec, value)) return; g_value_init (&default_value, G_PARAM_SPEC_VALUE_TYPE (pspec)); g_param_value_set_default (pspec, &default_value); v = g_strdup_value_contents (value); dv = g_strdup_value_contents (&default_value); msg = g_strdup_printf ("%s %s.%s: %s != %s\n", output, g_type_name (pspec->owner_type), pspec->name, dv, v); g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); g_free (msg); g_free (v); g_free (dv); g_value_unset (&default_value); }
void _cockpit_assert_data_eq_msg (const char *domain, const char *file, int line, const char *func, gconstpointer data, gssize len, gconstpointer expect, gssize exp_len) { char *a1, *a2, *s; if (!data && !expect) return; if (len < 0) len = strlen (data); if (exp_len < 0) exp_len = strlen (expect); if (len == exp_len && memcmp (data, expect, len) == 0) return; a1 = test_escape_data (data, len); a2 = test_escape_data (expect, exp_len); s = g_strdup_printf ("data is not the same (%s != %s)", a1, a2); g_free (a1); g_free (a2); g_assertion_message (domain, file, line, func, s); g_free (s); }
void _cockpit_assert_json_eq_msg (const char *domain, const char *file, int line, const char *func, JsonObject *object, const gchar *expect) { GError *error = NULL; JsonNode *node; JsonNode *exnode; gchar *escaped; gchar *msg; node = json_node_init_object (json_node_alloc (), object); exnode = cockpit_json_parse (expect, -1, &error); if (error) g_assertion_message_error (domain, file, line, func, "error", error, 0, 0); g_assert (exnode); if (!cockpit_json_equal (exnode, node)) { escaped = cockpit_json_write (node, NULL); msg = g_strdup_printf ("%s != %s", escaped, expect); g_assertion_message (domain, file, line, func, msg); g_free (escaped); g_free (msg); } json_node_free (node); json_node_free (exnode); }
/** * cockpit_assert_expected: * * Assert that all the things we were expecting in a test * happened. This should be called in a teardown() function * or after a cockpit_expect_xxx() function. */ void cockpit_assert_expected (void) { ExpectedMessage *expected = NULL; gchar *message = NULL; g_assert (cockpit_test_init_was_called); G_LOCK (expected); if (expected_messages) { expected = expected_messages->data; message = g_strdup_printf ("Did not see expected %s-%s: %s", expected->log_domain, calc_prefix (expected->log_level), expected->pattern); } G_UNLOCK (expected); if (expected) { g_assertion_message (expected->log_domain, expected->file, expected->line, expected->func, message); g_free (message); } ignore_fatal_count = 0; }
static void expected_message_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) { gint level = log_level & G_LOG_LEVEL_MASK; ExpectedMessage *expected = NULL; gchar *expected_message; gboolean skip = FALSE; G_LOCK (expected); if (level && expected_messages && (level & G_LOG_LEVEL_DEBUG) == 0) { expected = expected_messages->data; if (log_level & G_LOG_FLAG_FATAL) { ignore_fatal_count = 1; /* This handler is reset for each test, so set it right before we need it */ g_test_log_set_fatal_handler (expected_fatal_handler, NULL); } if (g_strcmp0 (expected->log_domain, log_domain) == 0 && ((log_level & expected->log_level) == expected->log_level) && g_pattern_match_simple (expected->pattern, message)) { expected_messages = g_slist_delete_link (expected_messages, expected_messages); g_free (expected->log_domain); g_free (expected->pattern); g_free (expected); skip = TRUE; } } G_UNLOCK (expected); if (skip) return; gtest_default_log_handler (log_domain, log_level, message, NULL); if (expected) { expected_message = g_strdup_printf ("Did not see expected %s-%s: %s", expected->log_domain, calc_prefix (expected->log_level), expected->pattern); g_assertion_message (expected->log_domain, expected->file, expected->line, expected->func, expected_message); g_free (expected_message); } }
static void qmp_assertion_message_error(const char *domain, const char *file, int line, const char *func, const char *expr, QDict *dict) { const char *class, *desc; char *s; QDict *error; error = qdict_get_qdict(dict, "error"); class = qdict_get_try_str(error, "class"); desc = qdict_get_try_str(error, "desc"); s = g_strdup_printf("assertion failed %s: %s %s", expr, class, desc); g_assertion_message(domain, file, line, func, s); g_free(s); }
/** * crank_assert_message_eq: (skip) * @domain: A logging domain. * @file: File name of source file. * @line: Line number of source file. * @func: Function name of source file. * @name_a: Name of argument 'a' * @name_b: Name of argument 'b' * @str_a: Stringification of argument 'a' * @str_b: Stringification of argument 'b' * * Report that argument 'a' and 'b' are not equal. * * This function might not return, but abort test instead. */ void crank_assert_message_eq (const gchar *domain, const gchar *file, const gint line, const gchar *func, const gchar *name_a, const gchar *name_b, const gchar *str_a, const gchar *str_b) { GString *msg = g_string_new (NULL); g_string_printf (msg, "%s and %s are not equal.\n", name_a, name_b); g_string_append_printf (msg, " %s\t: %s\n", name_a, str_a); g_string_append_printf (msg, " %s\t: %s", name_b, str_b); g_assertion_message (domain, file, line, func, msg->str); g_string_free (msg, TRUE); }
void egg_assertion_message_cmpmem (const char *domain, const char *file, int line, const char *func, const char *expr, gconstpointer arg1, gsize n_arg1, const char *cmp, gconstpointer arg2, gsize n_arg2) { char *a1, *a2, *s; a1 = arg1 ? hex_dump (arg1, n_arg1) : g_strdup ("NULL"); a2 = arg2 ? hex_dump (arg2, n_arg2) : g_strdup ("NULL"); s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2); g_free (a1); g_free (a2); g_assertion_message (domain, file, line, func, s); g_free (s); }
void mono_invoke_unhandled_exception_hook (MonoObject *exc) { if (unhandled_exception_hook) { unhandled_exception_hook (exc, unhandled_exception_hook_data); } else { MonoError inner_error; MonoObject *other = NULL; MonoString *str = mono_object_try_to_string (exc, &other, &inner_error); char *msg = NULL; if (str && is_ok (&inner_error)) { msg = mono_string_to_utf8_checked (str, &inner_error); if (!is_ok (&inner_error)) { msg = g_strdup_printf ("Nested exception while formatting original exception"); mono_error_cleanup (&inner_error); } } else if (other) { char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc); char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other); msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n", original_backtrace, nested_backtrace); g_free (original_backtrace); g_free (nested_backtrace); } else { msg = g_strdup ("Nested exception trying to figure out what went wrong"); } mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg); g_free (msg); #if defined(HOST_IOS) g_assertion_message ("Terminating runtime due to unhandled exception"); #else exit (mono_environment_exitcode_get ()); #endif } g_assert_not_reached (); }
/** * cockpit_assert_strmatch: * @str: the string * @pattern: to match * * Checks that @str matches the wildcard style @pattern */ void _cockpit_assert_strmatch_msg (const char *domain, const char *file, int line, const char *func, const gchar *string, const gchar *pattern) { const gchar *suffix; gchar *escaped; gchar *msg; int len; if (!string || !g_pattern_match_simple (pattern, string)) { escaped = g_strescape (pattern, ""); if (!string) { msg = g_strdup_printf ("'%s' does not match: (null)", escaped); } else { suffix = ""; len = strlen (string); /* To avoid insane output */ if (len > 8192) { len = 8192; suffix = "\n...\n"; } msg = g_strdup_printf ("'%s' does not match: %.*s%s", escaped, len, string, suffix); } g_assertion_message (domain, file, line, func, msg); g_free (escaped); g_free (msg); } }