[gjs/wip/3v1n0/toggle-queue-tests: 2/4] tests: Add unit tests for ToggleQueue and ObjectInstance usage of it




commit 49ff2c0011b50c2491b8693a3e9fc4b3fc60b166
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Sat May 1 22:31:19 2021 +0200

    tests: Add unit tests for ToggleQueue and ObjectInstance usage of it
    
    Instead of relying only on (at times unpredictible) JS tests for toggle
    queue handling and Objects memory management, add unit tests in which we
    manually handle an ObjectInstance and we simulate toggle events on it.

 gi/object.h                 |  6 ++++++
 gi/toggle.h                 |  6 ++++++
 test/gjs-test-utils.h       | 10 +++++++++-
 test/gjs-tests-internal.cpp |  1 +
 test/meson.build            |  1 +
 5 files changed, 23 insertions(+), 1 deletion(-)
---
diff --git a/gi/object.h b/gi/object.h
index abe2655b..af9766d1 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -41,6 +41,11 @@ class CallArgs;
 namespace js {
 class SystemAllocPolicy;
 }
+namespace Gjs {
+namespace Test {
+struct ObjectInstance;
+}
+}
 class ObjectInstance;
 class ObjectPrototype;
 
@@ -294,6 +299,7 @@ class ObjectInstance : public GIWrapperInstance<ObjectBase, ObjectPrototype,
                                    GObject>;
     friend class GIWrapperBase<ObjectBase, ObjectPrototype, ObjectInstance>;
     friend class ObjectBase;  // for add_property, prop_getter, etc.
+    friend struct Gjs::Test::ObjectInstance;
 
     // GIWrapperInstance::m_ptr may be null in ObjectInstance.
 
diff --git a/gi/toggle.h b/gi/toggle.h
index 17f22c06..141df552 100644
--- a/gi/toggle.h
+++ b/gi/toggle.h
@@ -17,6 +17,11 @@
 #include <utility>  // for pair
 
 class ObjectInstance;
+namespace Gjs {
+namespace Test {
+struct ToggleQueue;
+}
+}
 
 /* Thread-safe queue for enqueueing toggle-up or toggle-down events on GObjects
  * from any thread. For more information, see object.cpp, comments near
@@ -31,6 +36,7 @@ public:
     using Handler = void (*)(ObjectInstance*, Direction);
 
  private:
+    friend Gjs::Test::ToggleQueue;
     struct Item {
         Item() {}
         Item(ObjectInstance* o, Direction d) : object(o), direction(d) {}
diff --git a/test/gjs-test-utils.h b/test/gjs-test-utils.h
index 5ec140db..2fe5356a 100644
--- a/test/gjs-test-utils.h
+++ b/test/gjs-test-utils.h
@@ -42,9 +42,11 @@ void gjs_test_add_tests_for_jsapi_utils();
 namespace Gjs {
 namespace Test {
 
+void add_tests_for_toggle_queue();
+
 template <typename T>
 constexpr void assert_equal(T a, T b) {
-    if constexpr (std::is_integral_v<T>) {
+    if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) {
         if constexpr (std::is_unsigned_v<T>)
             g_assert_cmpuint(a, ==, b);
         else
@@ -63,6 +65,12 @@ constexpr void assert_equal(T a, T b) {
     }
 }
 
+template <typename T, typename U>
+constexpr void assert_equal(std::pair<T, U> const& pair, T first, U second) {
+    assert_equal(pair.first, first);
+    assert_equal(pair.second, second);
+}
+
 }  // namespace Test
 }  // namespace Gjs
 
diff --git a/test/gjs-tests-internal.cpp b/test/gjs-tests-internal.cpp
index 4f8f74ba..3346935d 100644
--- a/test/gjs-tests-internal.cpp
+++ b/test/gjs-tests-internal.cpp
@@ -18,6 +18,7 @@ int main(int argc, char** argv) {
     gjs_test_add_tests_for_rooting();
     gjs_test_add_tests_for_parse_call_args();
     gjs_test_add_tests_for_jsapi_utils();
+    Gjs::Test::add_tests_for_toggle_queue();
 
     g_test_run();
 
diff --git a/test/meson.build b/test/meson.build
index dd902877..0e35979f 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -36,6 +36,7 @@ gjs_tests_internal = executable('gjs-tests-internal',
         'gjs-test-call-args.cpp',
         'gjs-test-rooting.cpp',
         'gjs-test-jsapi-utils.cpp',
+        'gjs-test-toggle-queue.cpp',
         module_resource_srcs,
     ],
     include_directories: top_include,


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