[gjs/wip/3v1n0/toggle-queue-tests: 1/2] test: Move assert_equal definition into Gjs::Test and common header




commit f13ea428f6db9d147b1b8d814eb9073306d3d4ef
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Sun May 16 03:31:57 2021 +0200

    test: Move assert_equal definition into Gjs::Test and common header

 test/gjs-test-utils.h | 34 ++++++++++++++++++++++++++++++++++
 test/gjs-tests.cpp    | 19 ++-----------------
 2 files changed, 36 insertions(+), 17 deletions(-)
---
diff --git a/test/gjs-test-utils.h b/test/gjs-test-utils.h
index f44298a6..5ec140db 100644
--- a/test/gjs-test-utils.h
+++ b/test/gjs-test-utils.h
@@ -8,6 +8,13 @@
 
 #include <config.h>
 
+#include <glib.h>    // for g_assert_...
+#include <stdint.h>  // for uintptr_t
+#include <iterator>  // for pair
+#include <limits>    // for numeric_limits
+#include <string>
+#include <utility>  // IWYU pragma: keep
+
 #include "gjs/context.h"
 
 #include <js/TypeDecls.h>
@@ -32,4 +39,31 @@ void gjs_test_add_tests_for_rooting(void);
 
 void gjs_test_add_tests_for_jsapi_utils();
 
+namespace Gjs {
+namespace Test {
+
+template <typename T>
+constexpr void assert_equal(T a, T b) {
+    if constexpr (std::is_integral_v<T>) {
+        if constexpr (std::is_unsigned_v<T>)
+            g_assert_cmpuint(a, ==, b);
+        else
+            g_assert_cmpint(a, ==, b);
+    } else if constexpr (std::is_arithmetic_v<T>) {
+        g_assert_cmpfloat_with_epsilon(a, b, std::numeric_limits<T>::epsilon());
+    } else if constexpr (std::is_same_v<T, char*>) {
+        g_assert_cmpstr(a, ==, b);
+    } else if constexpr (std::is_same_v<T, std::string>) {
+        assert_equal(a.c_str(), b.c_str());
+    } else if constexpr (std::is_pointer_v<T>) {
+        assert_equal(reinterpret_cast<uintptr_t>(a),
+                     reinterpret_cast<uintptr_t>(b));
+    } else {
+        g_assert_true(a == b);
+    }
+}
+
+}  // namespace Test
+}  // namespace Gjs
+
 #endif  // TEST_GJS_TEST_UTILS_H_
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 325c967c..8eda4e27 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -47,6 +47,8 @@ void g_assertion_message(const char*, const char*, int, const char*,
 
 static unsigned cpp_random_seed = 0;
 
+using namespace Gjs::Test;  // NOLINT(build/namespaces)
+
 template <typename T>
 T get_random_number() {
     std::mt19937_64 gen(cpp_random_seed);
@@ -68,23 +70,6 @@ T get_random_number() {
     }
 }
 
-template <typename T>
-constexpr void assert_equal(T a, T b) {
-    if constexpr (std::is_integral_v<T>) {
-        if constexpr (std::is_unsigned_v<T>)
-            g_assert_cmpuint(a, ==, b);
-        else
-            g_assert_cmpint(a, ==, b);
-    } else if constexpr (std::is_arithmetic_v<T>) {
-        g_assert_cmpfloat_with_epsilon(a, b, std::numeric_limits<T>::epsilon());
-    } else if constexpr (std::is_same_v<T, char*>) {
-        g_assert_cmpstr(a, ==, b);
-    } else if constexpr (std::is_pointer_v<T>) {
-        assert_equal(reinterpret_cast<uintptr_t>(a),
-                     reinterpret_cast<uintptr_t>(b));
-    }
-}
-
 static void
 gjstest_test_func_gjs_context_construct_destroy(void)
 {


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