Bug 553447 $(Q#|(B g_assert_no_error()

* glib/gtestutils.h (g_assert_no_error, g_assert_error): Macros to
	assert that a GError is not set, or else is set to a particular
	error.

	* glib/gtestutils.c (g_assertion_message_error): utility for
	those macros

	* glib/tests/keyfile.c:
	* tests/asyncqueue-test.c:
	* tests/bookmarkfile-test.c:
	* tests/convert-test.c:
	* tests/file-test.c: Use g_assert_error/g_assert_no_error

svn path=/trunk/; revision=7555
This commit is contained in:
Dan Winship 2008-09-27 01:43:29 +00:00
parent 764e187fd9
commit 5c53925ed0
9 changed files with 108 additions and 103 deletions

View File

@ -1,3 +1,20 @@
2008-09-26 Dan Winship <danw@gnome.org>
Bug 553447 g_assert_no_error()
* glib/gtestutils.h (g_assert_no_error, g_assert_error): Macros to
assert that a GError is not set, or else is set to a particular
error.
* glib/gtestutils.c (g_assertion_message_error): utility for
those macros
* glib/tests/keyfile.c:
* tests/asyncqueue-test.c:
* tests/bookmarkfile-test.c:
* tests/convert-test.c:
* tests/file-test.c: Use g_assert_error/g_assert_no_error
2008-09-26 Dan Winship <danw@gnome.org> 2008-09-26 Dan Winship <danw@gnome.org>
* glib/gthreadpool.c (wakeup_thread_marker): make this a "const * glib/gthreadpool.c (wakeup_thread_marker): make this a "const

View File

@ -1317,6 +1317,7 @@ g_assertion_message G_GNUC_NORETURN
g_assertion_message_cmpnum G_GNUC_NORETURN g_assertion_message_cmpnum G_GNUC_NORETURN
g_assertion_message_cmpstr G_GNUC_NORETURN g_assertion_message_cmpstr G_GNUC_NORETURN
g_assertion_message_expr G_GNUC_NORETURN g_assertion_message_expr G_GNUC_NORETURN
g_assertion_message_error G_GNUC_NORETURN
g_strcmp0 g_strcmp0
g_test_add_data_func g_test_add_data_func
g_test_add_func g_test_add_func

View File

@ -1358,6 +1358,40 @@ g_assertion_message_cmpstr (const char *domain,
g_free (s); g_free (s);
} }
void
g_assertion_message_error (const char *domain,
const char *file,
int line,
const char *func,
const char *expr,
GError *error,
GQuark error_domain,
int error_code)
{
GString *gstring;
/* This is used by both g_assert_error() and g_assert_no_error(), so there
* are three cases: expected an error but got the wrong error, expected
* an error but got no error, and expected no error but got an error.
*/
gstring = g_string_new ("assertion failed ");
if (error_domain)
g_string_append_printf (gstring, "(%s == (%s, %d)): ", expr,
g_quark_to_string (error_domain), error_code);
else
g_string_append_printf (gstring, "(%s == NULL): ", expr);
if (error)
g_string_append_printf (gstring, "%s (%s, %d)", error->message,
g_quark_to_string (error->domain), error->code);
else
g_string_append_printf (gstring, "%s is NULL", expr);
g_assertion_message (domain, file, line, func, gstring->str);
g_string_free (gstring, TRUE);
}
/** /**
* g_strcmp0: * g_strcmp0:
* @str1: a C string or %NULL * @str1: a C string or %NULL

View File

@ -53,6 +53,12 @@ typedef struct GTestSuite GTestSuite;
if (__n1 cmp __n2) ; else \ if (__n1 cmp __n2) ; else \
g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0) #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0)
#define g_assert_no_error(err) do { if (err) \
g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#err, err, 0, 0); } while (0)
#define g_assert_error(err, dom, c) do { if (!err || (err)->domain != dom || (err)->code != c) \
g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#err, err, dom, c); } while (0)
#ifdef G_DISABLE_ASSERT #ifdef G_DISABLE_ASSERT
#define g_assert_not_reached() do { (void) 0; } while (0) #define g_assert_not_reached() do { (void) 0; } while (0)
#define g_assert(expr) do { (void) 0; } while (0) #define g_assert(expr) do { (void) 0; } while (0)
@ -197,6 +203,14 @@ void g_assertion_message_cmpnum (const char *domain,
const char *cmp, const char *cmp,
long double arg2, long double arg2,
char numtype) G_GNUC_NORETURN; char numtype) G_GNUC_NORETURN;
void g_assertion_message_error (const char *domain,
const char *file,
int line,
const char *func,
const char *expr,
GError *error,
GQuark error_domain,
int error_code) G_GNUC_NORETURN;
void g_test_add_vtable (const char *testpath, void g_test_add_vtable (const char *testpath,
gsize data_size, gsize data_size,
gconstpointer test_data, gconstpointer test_data,

View File

@ -12,7 +12,7 @@ load_data (const gchar *data,
keyfile = g_key_file_new (); keyfile = g_key_file_new ();
g_key_file_load_from_data (keyfile, data, -1, flags, &error); g_key_file_load_from_data (keyfile, data, -1, flags, &error);
g_assert (error == NULL); g_assert_no_error (error);
return keyfile; return keyfile;
} }
@ -21,9 +21,7 @@ check_error (GError **error,
GQuark domain, GQuark domain,
gint code) gint code)
{ {
g_assert (*error != NULL); g_assert_error (*error, domain, code);
g_assert ((*error)->domain == domain);
g_assert ((*error)->code == code);
g_error_free (*error); g_error_free (*error);
*error = NULL; *error = NULL;
} }
@ -31,7 +29,7 @@ check_error (GError **error,
static void static void
check_no_error (GError **error) check_no_error (GError **error)
{ {
g_assert (*error == NULL); g_assert_no_error (*error);
} }
static void static void

View File

@ -162,7 +162,7 @@ main (int argc, char *argv[])
g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error); g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
g_assert (error == NULL); g_assert_no_error (error);
} }
if (!SORT_QUEUE_AFTER) { if (!SORT_QUEUE_AFTER) {

View File

@ -16,68 +16,6 @@
#define TEST_APP_NAME "bookmarkfile-test" #define TEST_APP_NAME "bookmarkfile-test"
#define TEST_APP_EXEC "bookmarkfile-test %f" #define TEST_APP_EXEC "bookmarkfile-test %f"
static void
test_assert_empty_error (GError **error)
{
if (*error != NULL)
{
g_warning ("Unexpected error (d: %s, c: %d): %s\n",
g_quark_to_string ((*error)->domain),
(*error)->code,
(*error)->message);
g_error_free (*error);
g_assert_not_reached ();
}
}
static void
test_assert_not_empty_error (GError **error,
GQuark domain,
gint code)
{
if (*error == NULL)
{
g_warning ("Unexpected success (%s domain expected)\n",
g_quark_to_string (domain));
g_assert_not_reached ();
}
if ((*error)->domain != domain)
{
g_warning ("Unexpected domain %s (%s domain expected)\n",
g_quark_to_string ((*error)->domain),
g_quark_to_string (domain));
g_assert_not_reached ();
}
if ((*error)->code != code)
{
g_warning ("Unexpected code %d (%d code expected)\n",
(*error)->code,
code);
g_assert_not_reached ();
}
g_error_free (*error);
*error = NULL;
}
static void
test_assert_str_equal (const gchar *str,
const gchar *cmp)
{
if (strcmp (str, cmp) != 0)
{
g_warning ("Unexpected string '%s' ('%s' expected)\n", str, cmp);
g_assert_not_reached ();
}
}
static gboolean static gboolean
test_load (GBookmarkFile *bookmark, test_load (GBookmarkFile *bookmark,
const gchar *filename) const gchar *filename)
@ -139,13 +77,13 @@ test_modify (GBookmarkFile *bookmark)
g_bookmark_file_set_description (bookmark, NULL, "a bookmark file"); g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
text = g_bookmark_file_get_title (bookmark, NULL, &error); text = g_bookmark_file_get_title (bookmark, NULL, &error);
test_assert_empty_error (&error); g_assert_no_error (error);
test_assert_str_equal (text, "a file"); g_assert_cmpstr (text, ==, "a file");
g_free (text); g_free (text);
text = g_bookmark_file_get_description (bookmark, NULL, &error); text = g_bookmark_file_get_description (bookmark, NULL, &error);
test_assert_empty_error (&error); g_assert_no_error (error);
test_assert_str_equal (text, "a bookmark file"); g_assert_cmpstr (text, ==, "a bookmark file");
g_free (text); g_free (text);
g_print ("ok\n"); g_print ("ok\n");
@ -154,14 +92,15 @@ test_modify (GBookmarkFile *bookmark)
g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description"); g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error); text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
test_assert_empty_error (&error); g_assert_no_error (error);
test_assert_str_equal (text, "a title"); g_assert_cmpstr (text, ==, "a title");
g_free (text); g_free (text);
g_print ("ok\n"); g_print ("ok\n");
g_print ("\t=> check non existing bookmark..."); g_print ("\t=> check non existing bookmark...");
g_bookmark_file_get_description (bookmark, TEST_URI_1, &error); g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
g_clear_error (&error);
g_print ("ok\n"); g_print ("ok\n");
g_print ("\t=> check application..."); g_print ("\t=> check application...");
@ -175,7 +114,7 @@ test_modify (GBookmarkFile *bookmark)
&count, &count,
&stamp, &stamp,
&error); &error);
test_assert_empty_error (&error); g_assert_no_error (error);
g_assert (count == 1); g_assert (count == 1);
g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL)); g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
g_free (text); g_free (text);
@ -185,7 +124,8 @@ test_modify (GBookmarkFile *bookmark)
&count, &count,
&stamp, &stamp,
&error); &error);
test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED); g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
g_clear_error (&error);
g_print ("ok\n"); g_print ("ok\n");
g_print ("\t=> check groups..."); g_print ("\t=> check groups...");
@ -196,9 +136,10 @@ test_modify (GBookmarkFile *bookmark)
g_print ("\t=> check remove..."); g_print ("\t=> check remove...");
g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE); g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
test_assert_empty_error (&error); g_assert_no_error (error);
g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE); g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
test_assert_not_empty_error (&error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
g_clear_error (&error);
g_print ("ok\n"); g_print ("ok\n");
return TRUE; return TRUE;

View File

@ -48,7 +48,7 @@ test_iconv_state (void)
if (error && error->code == G_CONVERT_ERROR_NO_CONVERSION) if (error && error->code == G_CONVERT_ERROR_NO_CONVERSION)
return; /* silently skip if CP1255 is not supported, see bug 467707 */ return; /* silently skip if CP1255 is not supported, see bug 467707 */
g_assert (error == NULL); g_assert_no_error (error);
g_assert (bytes_read == 5); g_assert (bytes_read == 5);
g_assert (bytes_written == 10); g_assert (bytes_written == 10);
g_assert (strcmp (out, expected) == 0); g_assert (strcmp (out, expected) == 0);
@ -70,7 +70,7 @@ test_one_half (void)
&bytes_read, &bytes_written, &bytes_read, &bytes_written,
&error); &error);
g_assert (error == NULL); g_assert_no_error (error);
g_assert (bytes_read == 2); g_assert (bytes_read == 2);
g_assert (bytes_written == 1); g_assert (bytes_written == 1);
g_assert (strcmp (out, "\xbd") == 0); g_assert (strcmp (out, "\xbd") == 0);
@ -81,7 +81,7 @@ test_one_half (void)
&bytes_read, &bytes_written, &bytes_read, &bytes_written,
&error); &error);
g_assert (error && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE); g_assert_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE);
g_assert (bytes_read == 0); g_assert (bytes_read == 0);
g_assert (bytes_written == 0); g_assert (bytes_written == 0);
g_assert (out == NULL); g_assert (out == NULL);
@ -94,7 +94,7 @@ test_one_half (void)
&bytes_read, &bytes_written, &bytes_read, &bytes_written,
&error); &error);
g_assert (error == NULL); g_assert_no_error (error);
g_assert (bytes_read == 2); g_assert (bytes_read == 2);
g_assert (bytes_written == 1); g_assert (bytes_written == 1);
g_assert (strcmp (out, "a") == 0); g_assert (strcmp (out, "a") == 0);
@ -117,7 +117,7 @@ test_byte_order (void)
&bytes_read, &bytes_written, &bytes_read, &bytes_written,
&error); &error);
g_assert (error == NULL); g_assert_no_error (error);
g_assert (bytes_read == 4); g_assert (bytes_read == 4);
g_assert (bytes_written == 2); g_assert (bytes_written == 2);
g_assert (strcmp (out, expected) == 0); g_assert (strcmp (out, expected) == 0);
@ -128,7 +128,7 @@ test_byte_order (void)
&bytes_read, &bytes_written, &bytes_read, &bytes_written,
&error); &error);
g_assert (error == NULL); g_assert_no_error (error);
g_assert (bytes_read == 4); g_assert (bytes_read == 4);
g_assert (bytes_written == 2); g_assert (bytes_written == 2);
g_assert (strcmp (out, expected) == 0); g_assert (strcmp (out, expected) == 0);
@ -187,7 +187,7 @@ check_utf8_to_ucs4 (const char *utf8,
if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT) if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == error_pos); g_assert (items_read == error_pos);
g_assert (items_written == ucs4_len); g_assert (items_written == ucs4_len);
g_assert (result); g_assert (result);
@ -207,14 +207,14 @@ check_utf8_to_ucs4 (const char *utf8,
} }
else else
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == utf8_len); g_assert (items_read == utf8_len);
g_assert (items_written == ucs4_len); g_assert (items_written == ucs4_len);
g_assert (result); g_assert (result);
for (i = 0; i <= items_written; i++) for (i = 0; i <= items_written; i++)
g_assert (result[i] == ucs4[i]); g_assert (result[i] == ucs4[i]);
g_assert (error3 == NULL); g_assert_no_error (error3);
g_assert (result3); g_assert (result3);
for (i = 0; i <= ucs4_len; i++) for (i = 0; i <= ucs4_len; i++)
g_assert (result3[i] == ucs4[i]); g_assert (result3[i] == ucs4[i]);
@ -273,13 +273,13 @@ check_ucs4_to_utf8 (const gunichar *ucs4,
} }
else else
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == ucs4_len); g_assert (items_read == ucs4_len);
g_assert (items_written == utf8_len); g_assert (items_written == utf8_len);
g_assert (result); g_assert (result);
g_assert (strcmp (result, utf8) == 0); g_assert (strcmp (result, utf8) == 0);
g_assert (error3 == NULL); g_assert_no_error (error3);
g_assert (result3); g_assert (result3);
g_assert (strcmp (result3, utf8) == 0); g_assert (strcmp (result3, utf8) == 0);
} }
@ -327,7 +327,7 @@ check_utf8_to_utf16 (const char *utf8,
if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT) if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == error_pos); g_assert (items_read == error_pos);
g_assert (items_written == utf16_len); g_assert (items_written == utf16_len);
g_assert (result); g_assert (result);
@ -347,14 +347,14 @@ check_utf8_to_utf16 (const char *utf8,
} }
else else
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == utf8_len); g_assert (items_read == utf8_len);
g_assert (items_written == utf16_len); g_assert (items_written == utf16_len);
g_assert (result); g_assert (result);
for (i = 0; i <= items_written; i++) for (i = 0; i <= items_written; i++)
g_assert (result[i] == utf16[i]); g_assert (result[i] == utf16[i]);
g_assert (error3 == NULL); g_assert_no_error (error3);
g_assert (result3); g_assert (result3);
for (i = 0; i <= utf16_len; i++) for (i = 0; i <= utf16_len; i++)
g_assert (result3[i] == utf16[i]); g_assert (result3[i] == utf16[i]);
@ -401,7 +401,7 @@ check_utf16_to_utf8 (const gunichar2 *utf16,
if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT) if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == error_pos); g_assert (items_read == error_pos);
g_assert (items_read + 1 == utf16_len); g_assert (items_read + 1 == utf16_len);
g_assert (items_written == utf8_len); g_assert (items_written == utf8_len);
@ -421,13 +421,13 @@ check_utf16_to_utf8 (const gunichar2 *utf16,
} }
else else
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == utf16_len); g_assert (items_read == utf16_len);
g_assert (items_written == utf8_len); g_assert (items_written == utf8_len);
g_assert (result); g_assert (result);
g_assert (strcmp (result, utf8) == 0); g_assert (strcmp (result, utf8) == 0);
g_assert (error3 == NULL); g_assert_no_error (error3);
g_assert (result3); g_assert (result3);
g_assert (strcmp (result3, utf8) == 0); g_assert (strcmp (result3, utf8) == 0);
} }
@ -487,14 +487,14 @@ check_ucs4_to_utf16 (const gunichar *ucs4,
} }
else else
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == ucs4_len); g_assert (items_read == ucs4_len);
g_assert (items_written == utf16_len); g_assert (items_written == utf16_len);
g_assert (result); g_assert (result);
for (i = 0; i <= utf16_len; i++) for (i = 0; i <= utf16_len; i++)
g_assert (result[i] == utf16[i]); g_assert (result[i] == utf16[i]);
g_assert (error3 == NULL); g_assert_no_error (error3);
g_assert (result3); g_assert (result3);
for (i = 0; i <= utf16_len; i++) for (i = 0; i <= utf16_len; i++)
g_assert (result3[i] == utf16[i]); g_assert (result3[i] == utf16[i]);
@ -542,7 +542,7 @@ check_utf16_to_ucs4 (const gunichar2 *utf16,
if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT) if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == error_pos); g_assert (items_read == error_pos);
g_assert (items_read + 1 == utf16_len); g_assert (items_read + 1 == utf16_len);
g_assert (items_written == ucs4_len); g_assert (items_written == ucs4_len);
@ -563,14 +563,14 @@ check_utf16_to_ucs4 (const gunichar2 *utf16,
} }
else else
{ {
g_assert (error == NULL); g_assert_no_error (error);
g_assert (items_read == utf16_len); g_assert (items_read == utf16_len);
g_assert (items_written == ucs4_len); g_assert (items_written == ucs4_len);
g_assert (result); g_assert (result);
for (i = 0; i <= ucs4_len; i++) for (i = 0; i <= ucs4_len; i++)
g_assert (result[i] == ucs4[i]); g_assert (result[i] == ucs4[i]);
g_assert (error3 == NULL); g_assert_no_error (error3);
g_assert (result3); g_assert (result3);
for (i = 0; i <= ucs4_len; i++) for (i = 0; i <= ucs4_len; i++)
g_assert (result3[i] == ucs4[i]); g_assert (result3[i] == ucs4[i]);

View File

@ -132,12 +132,12 @@ test_readlink (void)
error = NULL; error = NULL;
data = g_file_read_link (link3, &error); data = g_file_read_link (link3, &error);
g_assert (data == NULL && "could read link3"); g_assert (data == NULL && "could read link3");
g_assert (error != NULL && "error not set"); g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
error = NULL; error = NULL;
data = g_file_read_link (filename, &error); data = g_file_read_link (filename, &error);
g_assert (data == NULL && "could read regular file as link"); g_assert (data == NULL && "could read regular file as link");
g_assert (error != NULL && "error not set"); g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
remove (filename); remove (filename);
remove (link1); remove (link1);