gnucash maint: [gtest-qofevent.cpp] comprehensive tests for qofevent

Christopher Lam clam at code.gnucash.org
Fri Nov 4 23:09:55 EDT 2022


Updated	 via  https://github.com/Gnucash/gnucash/commit/6f80c548 (commit)
	from  https://github.com/Gnucash/gnucash/commit/b87aa004 (commit)



commit 6f80c548e617308a082355a908e2a886dcbc2f79
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Mon Oct 31 09:43:46 2022 +0800

    [gtest-qofevent.cpp] comprehensive tests for qofevent

diff --git a/libgnucash/engine/test/CMakeLists.txt b/libgnucash/engine/test/CMakeLists.txt
index d1029bb6b..b1ff56f96 100644
--- a/libgnucash/engine/test/CMakeLists.txt
+++ b/libgnucash/engine/test/CMakeLists.txt
@@ -197,6 +197,11 @@ gtest-qofquerycore.cpp)
 gnc_add_test(test-qofquerycore "${test_qofquerycore_SOURCES}"
   gtest_engine_INCLUDES gtest_old_engine_LIBS)
 
+set(test_qofevent_SOURCES
+gtest-qofevent.cpp)
+gnc_add_test(test-qofevent "${test_qofevent_SOURCES}"
+  gtest_engine_INCLUDES gtest_old_engine_LIBS)
+
 
 set(test_engine_SOURCES_DIST
         dummy.cpp
@@ -208,6 +213,7 @@ set(test_engine_SOURCES_DIST
         gtest-gnc-datetime.cpp
         gtest-import-map.cpp
         gtest-qofquerycore.cpp
+        gtest-qofevent.cpp
         test-account-object.cpp
         test-address.c
         test-business.c
diff --git a/libgnucash/engine/test/gtest-qofevent.cpp b/libgnucash/engine/test/gtest-qofevent.cpp
new file mode 100644
index 000000000..a07ca27f8
--- /dev/null
+++ b/libgnucash/engine/test/gtest-qofevent.cpp
@@ -0,0 +1,137 @@
+/********************************************************************\
+ * gtest-qofevent.cpp -- Unit tests for qofevent.cpp                *
+ *                                                                  *
+ * Copyright 2022 Christopher Lam                                   *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+ \ *********************************************************************/
+
+#include <config.h>
+#include <glib.h>
+#include "../test-core/test-engine-stuff.h"
+#include "../qofevent.h"
+#include "../qofevent-p.h"
+#include <gtest/gtest.h>
+
+static void
+easy_handler (QofInstance *ent,  QofEventId event_type,
+            gpointer handler_data, gpointer event_data)
+{
+    int *data = static_cast<int*>(handler_data);
+    int increment = GPOINTER_TO_INT(event_data);
+    *data = *data + increment;
+}
+
+TEST (qofevent, events)
+{
+    QofInstance entity;         // qofevents needs a non-null entity.
+    int data = 1;
+
+    // initial setup. register first handler. id is 1. data is initialized at 1.
+    int id1 = qof_event_register_handler (easy_handler, &data);
+    EXPECT_EQ (id1, 1);
+    EXPECT_EQ (data, 1);
+
+    // call events. this increment data by 2. data is now 3.
+    qof_event_gen (&entity, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 3);
+
+    // entity is NULL, call events. data is unchanged.
+    qof_event_gen (NULL, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 3);
+
+    // event is QOF_EVENT_NONE, call events. data is unchanged.
+    qof_event_gen (&entity, QOF_EVENT_NONE, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 3);
+
+    // call events. this increment data by 2. data is now 5.
+    qof_event_gen (&entity, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 5);
+
+    // suspend, try calling events. data unchanged at 5.
+    qof_event_suspend ();
+    qof_event_gen (&entity, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 5);
+
+    // although suspended, force events. data changed to 7.
+    qof_event_force (&entity, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 7);
+
+    // resume, call events. data incremented by 2 to 9.
+    qof_event_resume ();
+    qof_event_gen (&entity, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 9);
+
+    qof_event_unregister_handler (id1);
+
+    // no more handler. running events means data is no longer updated.
+    qof_event_gen (&entity, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 9);
+
+    // test handler id is incremented to 2
+    int id2 = qof_event_register_handler (easy_handler, &data);
+    EXPECT_EQ (id2, 2);
+
+    // test handler id is incremented to 3
+    int id3 = qof_event_register_handler (easy_handler, &data);
+    EXPECT_EQ (id3, 3);
+    qof_event_unregister_handler (id3);
+    qof_event_unregister_handler (id2);
+
+    // handler_id 1 to 3 are now unregistered, the next handler_id is still 4.
+    int id4 = qof_event_register_handler (easy_handler, &data);
+    EXPECT_EQ (id4, 4);
+    qof_event_unregister_handler (id4);
+}
+
+static void
+compound_handler (QofInstance *ent,  QofEventId event_type,
+                  gpointer handler_data, gpointer event_data)
+{
+    QofInstance entity;
+    int *data = static_cast<int*>(handler_data);
+    int increment = GPOINTER_TO_INT(event_data);
+
+    int id = qof_event_register_handler (easy_handler, &data);
+    // this handler is immediately deregistered but is not deleted
+    // until all handlers have run
+    qof_event_unregister_handler (id);
+
+    *data = *data + increment;
+}
+
+TEST (qofevent, compound_events)
+{
+    QofInstance entity;         // qofevents needs a non-null entity.
+    int data = 1;
+
+    // initial setup. first handler_id is 1. data is initialized at 1.
+    int id5 = qof_event_register_handler (compound_handler, &data);
+    EXPECT_EQ (id5, 5);
+    EXPECT_EQ (data, 1);
+
+    // call handlers. this increments data by 2. calling
+    // compound_handler will cause the deletion to be effected after
+    // all the handlers have completed.
+    qof_event_gen (&entity, QOF_EVENT_ALL, GINT_TO_POINTER(2));
+    EXPECT_EQ (data, 3);
+
+    qof_event_unregister_handler (id5);
+}
+



Summary of changes:
 libgnucash/engine/test/CMakeLists.txt     |   6 ++
 libgnucash/engine/test/gtest-qofevent.cpp | 137 ++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+)
 create mode 100644 libgnucash/engine/test/gtest-qofevent.cpp



More information about the gnucash-changes mailing list