glib r7555 - in trunk: . glib glib/tests tests
- From: danw svn gnome org
- To: svn-commits-list gnome org
- Subject: glib r7555 - in trunk: . glib glib/tests tests
- Date: Sat, 27 Sep 2008 01:43:29 +0000 (UTC)
Author: danw
Date: Sat Sep 27 01:43:29 2008
New Revision: 7555
URL: http://svn.gnome.org/viewvc/glib?rev=7555&view=rev
Log:
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
Modified:
trunk/ChangeLog
trunk/glib/glib.symbols
trunk/glib/gtestutils.c
trunk/glib/gtestutils.h
trunk/glib/tests/keyfile.c
trunk/tests/asyncqueue-test.c
trunk/tests/bookmarkfile-test.c
trunk/tests/convert-test.c
trunk/tests/file-test.c
Modified: trunk/glib/glib.symbols
==============================================================================
--- trunk/glib/glib.symbols (original)
+++ trunk/glib/glib.symbols Sat Sep 27 01:43:29 2008
@@ -1317,6 +1317,7 @@
g_assertion_message_cmpnum G_GNUC_NORETURN
g_assertion_message_cmpstr G_GNUC_NORETURN
g_assertion_message_expr G_GNUC_NORETURN
+g_assertion_message_error G_GNUC_NORETURN
g_strcmp0
g_test_add_data_func
g_test_add_func
Modified: trunk/glib/gtestutils.c
==============================================================================
--- trunk/glib/gtestutils.c (original)
+++ trunk/glib/gtestutils.c Sat Sep 27 01:43:29 2008
@@ -1358,6 +1358,40 @@
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:
* @str1: a C string or %NULL
Modified: trunk/glib/gtestutils.h
==============================================================================
--- trunk/glib/gtestutils.h (original)
+++ trunk/glib/gtestutils.h Sat Sep 27 01:43:29 2008
@@ -53,6 +53,12 @@
if (__n1 cmp __n2) ; else \
g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#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
#define g_assert_not_reached() do { (void) 0; } while (0)
#define g_assert(expr) do { (void) 0; } while (0)
@@ -197,6 +203,14 @@
const char *cmp,
long double arg2,
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,
gsize data_size,
gconstpointer test_data,
Modified: trunk/glib/tests/keyfile.c
==============================================================================
--- trunk/glib/tests/keyfile.c (original)
+++ trunk/glib/tests/keyfile.c Sat Sep 27 01:43:29 2008
@@ -12,7 +12,7 @@
keyfile = g_key_file_new ();
g_key_file_load_from_data (keyfile, data, -1, flags, &error);
- g_assert (error == NULL);
+ g_assert_no_error (error);
return keyfile;
}
@@ -21,9 +21,7 @@
GQuark domain,
gint code)
{
- g_assert (*error != NULL);
- g_assert ((*error)->domain == domain);
- g_assert ((*error)->code == code);
+ g_assert_error (*error, domain, code);
g_error_free (*error);
*error = NULL;
}
@@ -31,7 +29,7 @@
static void
check_no_error (GError **error)
{
- g_assert (*error == NULL);
+ g_assert_no_error (*error);
}
static void
Modified: trunk/tests/asyncqueue-test.c
==============================================================================
--- trunk/tests/asyncqueue-test.c (original)
+++ trunk/tests/asyncqueue-test.c Sat Sep 27 01:43:29 2008
@@ -162,7 +162,7 @@
g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
- g_assert (error == NULL);
+ g_assert_no_error (error);
}
if (!SORT_QUEUE_AFTER) {
Modified: trunk/tests/bookmarkfile-test.c
==============================================================================
--- trunk/tests/bookmarkfile-test.c (original)
+++ trunk/tests/bookmarkfile-test.c Sat Sep 27 01:43:29 2008
@@ -16,68 +16,6 @@
#define TEST_APP_NAME "bookmarkfile-test"
#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
test_load (GBookmarkFile *bookmark,
const gchar *filename)
@@ -139,13 +77,13 @@
g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
text = g_bookmark_file_get_title (bookmark, NULL, &error);
- test_assert_empty_error (&error);
- test_assert_str_equal (text, "a file");
+ g_assert_no_error (error);
+ g_assert_cmpstr (text, ==, "a file");
g_free (text);
text = g_bookmark_file_get_description (bookmark, NULL, &error);
- test_assert_empty_error (&error);
- test_assert_str_equal (text, "a bookmark file");
+ g_assert_no_error (error);
+ g_assert_cmpstr (text, ==, "a bookmark file");
g_free (text);
g_print ("ok\n");
@@ -154,14 +92,15 @@
g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
- test_assert_empty_error (&error);
- test_assert_str_equal (text, "a title");
+ g_assert_no_error (error);
+ g_assert_cmpstr (text, ==, "a title");
g_free (text);
g_print ("ok\n");
g_print ("\t=> check non existing bookmark...");
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 ("\t=> check application...");
@@ -175,7 +114,7 @@
&count,
&stamp,
&error);
- test_assert_empty_error (&error);
+ g_assert_no_error (error);
g_assert (count == 1);
g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
g_free (text);
@@ -185,7 +124,8 @@
&count,
&stamp,
&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 ("\t=> check groups...");
@@ -196,9 +136,10 @@
g_print ("\t=> check remove...");
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);
- 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");
return TRUE;
Modified: trunk/tests/convert-test.c
==============================================================================
--- trunk/tests/convert-test.c (original)
+++ trunk/tests/convert-test.c Sat Sep 27 01:43:29 2008
@@ -48,7 +48,7 @@
if (error && error->code == G_CONVERT_ERROR_NO_CONVERSION)
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_written == 10);
g_assert (strcmp (out, expected) == 0);
@@ -70,7 +70,7 @@
&bytes_read, &bytes_written,
&error);
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (bytes_read == 2);
g_assert (bytes_written == 1);
g_assert (strcmp (out, "\xbd") == 0);
@@ -81,7 +81,7 @@
&bytes_read, &bytes_written,
&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_written == 0);
g_assert (out == NULL);
@@ -94,7 +94,7 @@
&bytes_read, &bytes_written,
&error);
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (bytes_read == 2);
g_assert (bytes_written == 1);
g_assert (strcmp (out, "a") == 0);
@@ -117,7 +117,7 @@
&bytes_read, &bytes_written,
&error);
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (bytes_read == 4);
g_assert (bytes_written == 2);
g_assert (strcmp (out, expected) == 0);
@@ -128,7 +128,7 @@
&bytes_read, &bytes_written,
&error);
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (bytes_read == 4);
g_assert (bytes_written == 2);
g_assert (strcmp (out, expected) == 0);
@@ -187,7 +187,7 @@
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_written == ucs4_len);
g_assert (result);
@@ -207,14 +207,14 @@
}
else
{
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (items_read == utf8_len);
g_assert (items_written == ucs4_len);
g_assert (result);
for (i = 0; i <= items_written; i++)
g_assert (result[i] == ucs4[i]);
- g_assert (error3 == NULL);
+ g_assert_no_error (error3);
g_assert (result3);
for (i = 0; i <= ucs4_len; i++)
g_assert (result3[i] == ucs4[i]);
@@ -273,13 +273,13 @@
}
else
{
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (items_read == ucs4_len);
g_assert (items_written == utf8_len);
g_assert (result);
g_assert (strcmp (result, utf8) == 0);
- g_assert (error3 == NULL);
+ g_assert_no_error (error3);
g_assert (result3);
g_assert (strcmp (result3, utf8) == 0);
}
@@ -327,7 +327,7 @@
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_written == utf16_len);
g_assert (result);
@@ -347,14 +347,14 @@
}
else
{
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (items_read == utf8_len);
g_assert (items_written == utf16_len);
g_assert (result);
for (i = 0; i <= items_written; i++)
g_assert (result[i] == utf16[i]);
- g_assert (error3 == NULL);
+ g_assert_no_error (error3);
g_assert (result3);
for (i = 0; i <= utf16_len; i++)
g_assert (result3[i] == utf16[i]);
@@ -401,7 +401,7 @@
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 + 1 == utf16_len);
g_assert (items_written == utf8_len);
@@ -421,13 +421,13 @@
}
else
{
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (items_read == utf16_len);
g_assert (items_written == utf8_len);
g_assert (result);
g_assert (strcmp (result, utf8) == 0);
- g_assert (error3 == NULL);
+ g_assert_no_error (error3);
g_assert (result3);
g_assert (strcmp (result3, utf8) == 0);
}
@@ -487,14 +487,14 @@
}
else
{
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (items_read == ucs4_len);
g_assert (items_written == utf16_len);
g_assert (result);
for (i = 0; i <= utf16_len; i++)
g_assert (result[i] == utf16[i]);
- g_assert (error3 == NULL);
+ g_assert_no_error (error3);
g_assert (result3);
for (i = 0; i <= utf16_len; i++)
g_assert (result3[i] == utf16[i]);
@@ -542,7 +542,7 @@
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 + 1 == utf16_len);
g_assert (items_written == ucs4_len);
@@ -563,14 +563,14 @@
}
else
{
- g_assert (error == NULL);
+ g_assert_no_error (error);
g_assert (items_read == utf16_len);
g_assert (items_written == ucs4_len);
g_assert (result);
for (i = 0; i <= ucs4_len; i++)
g_assert (result[i] == ucs4[i]);
- g_assert (error3 == NULL);
+ g_assert_no_error (error3);
g_assert (result3);
for (i = 0; i <= ucs4_len; i++)
g_assert (result3[i] == ucs4[i]);
Modified: trunk/tests/file-test.c
==============================================================================
--- trunk/tests/file-test.c (original)
+++ trunk/tests/file-test.c Sat Sep 27 01:43:29 2008
@@ -132,12 +132,12 @@
error = NULL;
data = g_file_read_link (link3, &error);
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;
data = g_file_read_link (filename, &error);
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 (link1);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]