Bug 553447 \e$(Q#|\e(B g_assert_no_error()
authorDan Winship <danw@src.gnome.org>
Sat, 27 Sep 2008 01:43:29 +0000 (01:43 +0000)
committerDan Winship <danw@src.gnome.org>
Sat, 27 Sep 2008 01:43:29 +0000 (01:43 +0000)
* 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

ChangeLog
glib/glib.symbols
glib/gtestutils.c
glib/gtestutils.h
glib/tests/keyfile.c
tests/asyncqueue-test.c
tests/bookmarkfile-test.c
tests/convert-test.c
tests/file-test.c

index 25341931f3133b8b5740f479d21d0f370a402a61..8bc0b010439d0d0329e3b0f948367264a7e3f865 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
 
        * glib/gthreadpool.c (wakeup_thread_marker): make this a "const
index 5ca5fec546683e561f3d1ed7ba4f6fe94fa654ec..2d4ba8c38ddd505c27dbee2f6149c6b559ec1c42 100644 (file)
@@ -1317,6 +1317,7 @@ g_assertion_message G_GNUC_NORETURN
 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
index 0f248628c7cf857a233b28f8c284f2dcd4ddb715..d4be2c88cf88a860a2597076aebd23bb3ffc71c3 100644 (file)
@@ -1358,6 +1358,40 @@ g_assertion_message_cmpstr (const char     *domain,
   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
index 193dd1c801295d1090ff153bdbc2521718c4b754..4c474589d3602b36f00f800fc4c75b7e38884a6f 100644 (file)
@@ -53,6 +53,12 @@ typedef struct GTestSuite GTestSuite;
                                              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 @@ void    g_assertion_message_cmpnum      (const char     *domain,
                                          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,
index 35252bd31d44997bbe4307077bfc0fb4149ed664..57823fccdadc6f113a0f98e47059dc948cd72203 100644 (file)
@@ -12,7 +12,7 @@ load_data (const gchar   *data,
 
   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 @@ check_error (GError **error,
             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 @@ check_error (GError **error,
 static void 
 check_no_error (GError **error)
 {
-  g_assert (*error == NULL);
+  g_assert_no_error (*error);
 }
 
 static void
index da65d7531bec04d030642528d8efcf8ed72d9770..eb38d9ec53458b787e46b5846485f7c68dcf4077 100644 (file)
@@ -162,7 +162,7 @@ main (int argc, char *argv[])
   
     g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
     
-    g_assert (error == NULL);
+    g_assert_no_error (error);
   }
 
   if (!SORT_QUEUE_AFTER) {
index 06019ca1fb672de4962c360905e749c0b82698a4..40e9a0581277b30d8d067888413af8f835f64dd2 100644 (file)
 #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 @@ test_modify (GBookmarkFile *bookmark)
   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 @@ test_modify (GBookmarkFile *bookmark)
   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 @@ test_modify (GBookmarkFile *bookmark)
                                &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 @@ test_modify (GBookmarkFile *bookmark)
                                &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 @@ test_modify (GBookmarkFile *bookmark)
 
   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;
index f21972737de0e671aca1bfbc79635590adba862c..10b8c00032b157cb05638bc65b26725cb7df0db1 100644 (file)
@@ -48,7 +48,7 @@ test_iconv_state (void)
   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 @@ test_one_half (void)
                   &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 @@ test_one_half (void)
                   &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 @@ test_one_half (void)
                                 &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 @@ test_byte_order (void)
                   &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 @@ test_byte_order (void)
                   &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 @@ check_utf8_to_ucs4 (const char     *utf8,
       
   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 @@ check_utf8_to_ucs4 (const char     *utf8,
     }
   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 @@ check_ucs4_to_utf8 (const gunichar *ucs4,
     }
   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 @@ check_utf8_to_utf16 (const char      *utf8,
       
   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 @@ check_utf8_to_utf16 (const char      *utf8,
     }
   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 @@ check_utf16_to_utf8 (const gunichar2 *utf16,
   
   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 @@ check_utf16_to_utf8 (const gunichar2 *utf16,
     }
   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 @@ check_ucs4_to_utf16 (const gunichar  *ucs4,
     }
   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 @@ check_utf16_to_ucs4 (const gunichar2 *utf16,
       
   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 @@ check_utf16_to_ucs4 (const gunichar2 *utf16,
     }
   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]);
index b38d16545d3be717451cc7673260ad72e6791ccd..adab1f20f9b1bb9bf54c129754540533c9677e2f 100644 (file)
@@ -132,12 +132,12 @@ test_readlink (void)
   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);