[gjs/gnome-3-34] tests: Don't use g_assert and g_assert_not_reached in tests



commit 5a7afb70e2617725a755b69d49703c8135c28c86
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat May 9 16:01:04 2020 -0700

    tests: Don't use g_assert and g_assert_not_reached in tests
    
    These macros are no-ops if GLib is built with G_DISABLE_ASSERT, so they
    are not appropriate to use in tests.
    
    See: #298

 test/gjs-test-call-args.cpp | 18 ++++++++++--------
 test/gjs-test-coverage.cpp  | 21 ++++++++++-----------
 test/gjs-tests.cpp          | 32 ++++++++++++++++----------------
 3 files changed, 36 insertions(+), 35 deletions(-)
---
diff --git a/test/gjs-test-call-args.cpp b/test/gjs-test-call-args.cpp
index 3e681573..70a8b4ba 100644
--- a/test/gjs-test-call-args.cpp
+++ b/test/gjs-test-call-args.cpp
@@ -10,14 +10,16 @@
 #include "test/gjs-test-common.h"
 #include "test/gjs-test-utils.h"
 
-#define assert_match(str, pattern)                                            \
-    G_STMT_START {                                                            \
-        const char *__s1 = (str), *__s2 = (pattern);                          \
-        if (!g_pattern_match_simple(__s2, __s1)) {                            \
-            g_printerr("**\nExpected \"%s\" to match \"%s\"\n", __s1, __s2);  \
-            g_assert_not_reached();                                           \
-        }                                                                     \
-    } G_STMT_END
+#define assert_match(str, pattern)                                           \
+    G_STMT_START {                                                           \
+        const char *__s1 = (str), *__s2 = (pattern);                         \
+        if (!g_pattern_match_simple(__s2, __s1)) {                           \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                "assertion failed (\"" #str                  \
+                                "\" matches \"" #pattern "\")");             \
+        }                                                                    \
+    }                                                                        \
+    G_STMT_END
 
 typedef enum _test_enum {
     ZERO,
diff --git a/test/gjs-test-coverage.cpp b/test/gjs-test-coverage.cpp
index d59bee49..a68560f6 100644
--- a/test/gjs-test-coverage.cpp
+++ b/test/gjs-test-coverage.cpp
@@ -359,7 +359,7 @@ static void test_previous_contents_preserved(void* fixture_data, const void*) {
                                           fixture->tmp_js_script,
                                           fixture->lcov_output);
 
-    g_assert(strstr(coverage_data_contents, existing_contents) != NULL);
+    g_assert_nonnull(strstr(coverage_data_contents, existing_contents));
     g_free(coverage_data_contents);
 }
 
@@ -378,7 +378,7 @@ static void test_new_contents_written(void* fixture_data, const void*) {
                                           fixture->lcov_output);
 
     /* We have new content in the coverage data */
-    g_assert(strlen(existing_contents) != strlen(coverage_data_contents));
+    g_assert_cmpstr(existing_contents, !=, coverage_data_contents);
     g_free(coverage_data_contents);
 }
 
@@ -481,7 +481,7 @@ branch_at_line_should_be_taken(const char *line,
         g_assert_cmpint(hit_count_num, >, 0);
         break;
     default:
-        g_assert_not_reached();
+        g_assert_true(false && "Invalid branch state");
     };
 }
 
@@ -642,7 +642,7 @@ any_line_matches_not_executed_branch(const char *data)
         line = line_starting_with(line + 1, "BRDA:");
     }
 
-    g_assert_not_reached();
+    g_assert_true(false && "BRDA line with line 3 not found");
 }
 
 static void test_branch_not_hit_written_to_coverage_data(void* fixture_data,
@@ -990,14 +990,13 @@ line_hit_count_is_more_than(const char *line,
 
     unsigned int lineno = strtol(coverage_line, &comma_ptr, 10);
 
-    g_assert(comma_ptr[0] == ',');
+    g_assert_cmpint(comma_ptr[0], ==, ',');
 
     char *end_ptr = NULL;
 
     unsigned int value = strtol(&comma_ptr[1], &end_ptr, 10);
 
-    g_assert(end_ptr[0] == '\0' ||
-             end_ptr[0] == '\n');
+    g_assert_true(end_ptr[0] == '\0' || end_ptr[0] == '\n');
 
     g_assert_cmpuint(lineno, ==, data->expected_lineno);
     g_assert_cmpuint(value, >, data->expected_to_be_more_than);
@@ -1104,7 +1103,7 @@ static void test_end_of_record_section_written_to_coverage_data(
                                           fixture->tmp_js_script,
                                           fixture->lcov_output);
 
-    g_assert(strstr(coverage_data_contents, "end_of_record") != NULL);
+    g_assert_nonnull(strstr(coverage_data_contents, "end_of_record"));
     g_free(coverage_data_contents);
 }
 
@@ -1191,10 +1190,10 @@ static void test_multiple_source_file_records_written_to_coverage_data(
                                           fixture->base_fixture.lcov_output);
 
     const char *first_sf_record = line_starting_with(coverage_data_contents, "SF:");
-    g_assert(first_sf_record != NULL);
+    g_assert_nonnull(first_sf_record);
 
     const char *second_sf_record = line_starting_with(first_sf_record + 1, "SF:");
-    g_assert(second_sf_record != NULL);
+    g_assert_nonnull(second_sf_record);
 
     g_free(coverage_data_contents);
 }
@@ -1231,7 +1230,7 @@ assert_coverage_data_for_source_file(ExpectedSourceFileCoverageData *expected,
         }
     }
 
-    g_assert_not_reached();
+    g_assert_true(false && "Expected source file path to be found in section");
 }
 
 static void
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index c5bd9e17..a833aeab 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -125,7 +125,7 @@ gjstest_test_func_gjs_gobject_js_defined_type(void)
     g_assert_cmpuint(foo_type, !=, G_TYPE_INVALID);
 
     gpointer foo = g_object_new(foo_type, NULL);
-    g_assert(G_IS_OBJECT(foo));
+    g_assert_true(G_IS_OBJECT(foo));
 
     g_object_unref(foo);
     g_object_unref(context);
@@ -135,7 +135,7 @@ static void gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(
     GjsUnitTestFixture* fx, const void*) {
     JS::RootedValue js_string(fx->cx);
     g_assert_true(gjs_string_from_utf8(fx->cx, VALID_UTF8_STRING, &js_string));
-    g_assert(js_string.isString());
+    g_assert_true(js_string.isString());
 
     JS::UniqueChars utf8_result;
     g_assert(gjs_string_to_utf8(fx->cx, js_string, &utf8_result));
@@ -150,15 +150,15 @@ static void gjstest_test_func_gjs_jsapi_util_error_throw(GjsUnitTestFixture* fx,
 
     gjs_throw(fx->cx, "This is an exception %d", 42);
 
-    g_assert(JS_IsExceptionPending(fx->cx));
+    g_assert_true(JS_IsExceptionPending(fx->cx));
 
     JS_GetPendingException(fx->cx, &exc);
-    g_assert(!exc.isUndefined());
+    g_assert_false(exc.isUndefined());
 
     JS::RootedObject exc_obj(fx->cx, &exc.toObject());
     JS_GetProperty(fx->cx, exc_obj, "message", &value);
 
-    g_assert(value.isString());
+    g_assert_true(value.isString());
 
     JS::UniqueChars s;
     bool ok = gjs_string_to_utf8(fx->cx, value, &s);
@@ -171,21 +171,21 @@ static void gjstest_test_func_gjs_jsapi_util_error_throw(GjsUnitTestFixture* fx,
 
     JS_ClearPendingException(fx->cx);
 
-    g_assert(!JS_IsExceptionPending(fx->cx));
+    g_assert_false(JS_IsExceptionPending(fx->cx));
 
     /* Check that we don't overwrite a pending exception */
     JS_SetPendingException(fx->cx, previous);
 
-    g_assert(JS_IsExceptionPending(fx->cx));
+    g_assert_true(JS_IsExceptionPending(fx->cx));
 
     gjs_throw(fx->cx, "Second different exception %s", "foo");
 
-    g_assert(JS_IsExceptionPending(fx->cx));
+    g_assert_true(JS_IsExceptionPending(fx->cx));
 
     exc = JS::UndefinedValue();
     JS_GetPendingException(fx->cx, &exc);
-    g_assert(!exc.isUndefined());
-    g_assert(&exc.toObject() == &previous.toObject());
+    g_assert_false(exc.isUndefined());
+    g_assert_true(&exc.toObject() == &previous.toObject());
 }
 
 static void test_jsapi_util_string_utf8_nchars_to_js(GjsUnitTestFixture* fx,
@@ -294,8 +294,8 @@ gjstest_test_func_util_misc_strv_concat_null(void)
     char **ret;
 
     ret = gjs_g_strv_concat(NULL, 0);
-    g_assert(ret != NULL);
-    g_assert(ret[0] == NULL);
+    g_assert_nonnull(ret);
+    g_assert_null(ret[0]);
 
     g_strfreev(ret);
 }
@@ -316,12 +316,12 @@ gjstest_test_func_util_misc_strv_concat_pointers(void)
     stuff[3] = strv3;
 
     ret = gjs_g_strv_concat(stuff, 4);
-    g_assert(ret != NULL);
+    g_assert_nonnull(ret);
     g_assert_cmpstr(ret[0], ==, strv0[0]);  /* same string */
-    g_assert(ret[0] != strv0[0]);           /* different pointer */
+    g_assert_true(ret[0] != strv0[0]);      // different pointer
     g_assert_cmpstr(ret[1], ==, strv3[0]);
-    g_assert(ret[1] != strv3[0]);
-    g_assert(ret[2] == NULL);
+    g_assert_true(ret[1] != strv3[0]);
+    g_assert_null(ret[2]);
 
     g_strfreev(ret);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]