r21431 - gnucash/trunk - Add first unittest for glibmm C++ wrapper of QofBook.

Christian Stimming cstim at code.gnucash.org
Mon Oct 17 16:10:56 EDT 2011


Author: cstim
Date: 2011-10-17 16:10:56 -0400 (Mon, 17 Oct 2011)
New Revision: 21431
Trac: http://svn.gnucash.org/trac/changeset/21431

Added:
   gnucash/trunk/src/optional/gtkmm/test/
   gnucash/trunk/src/optional/gtkmm/test/Makefile.am
   gnucash/trunk/src/optional/gtkmm/test/test-book.cpp
   gnucash/trunk/src/optional/gtkmm/test/test-gtkmm.cpp
Modified:
   gnucash/trunk/configure.ac
   gnucash/trunk/src/optional/gtkmm/Makefile.am
Log:
Add first unittest for glibmm C++ wrapper of QofBook.

Unfortunately, this reveals the C++ wrappers will only work if the GObject
system is used properly, that is, including ref() and unref() and such.
This is not what I'd expect throughout gnucash.

Modified: gnucash/trunk/configure.ac
===================================================================
--- gnucash/trunk/configure.ac	2011-10-17 19:07:41 UTC (rev 21430)
+++ gnucash/trunk/configure.ac	2011-10-17 20:10:56 UTC (rev 21431)
@@ -1304,6 +1304,7 @@
   src/libqof/qof/test/Makefile
   src/optional/Makefile
   src/optional/gtkmm/Makefile
+  src/optional/gtkmm/test/Makefile
   src/optional/python-bindings/Makefile
   src/optional/python-bindings/tests/Makefile
   src/pixmaps/Makefile

Modified: gnucash/trunk/src/optional/gtkmm/Makefile.am
===================================================================
--- gnucash/trunk/src/optional/gtkmm/Makefile.am	2011-10-17 19:07:41 UTC (rev 21430)
+++ gnucash/trunk/src/optional/gtkmm/Makefile.am	2011-10-17 20:10:56 UTC (rev 21431)
@@ -1,4 +1,4 @@
-SUBDIRS =
+SUBDIRS = test
 
 pkglib_LTLIBRARIES = libgncmod-gtkmm.la
 

Added: gnucash/trunk/src/optional/gtkmm/test/Makefile.am
===================================================================
--- gnucash/trunk/src/optional/gtkmm/test/Makefile.am	                        (rev 0)
+++ gnucash/trunk/src/optional/gtkmm/test/Makefile.am	2011-10-17 20:10:56 UTC (rev 21431)
@@ -0,0 +1,44 @@
+# A template Makefile.am for GLib g_test-based test directories.
+# Copyright 2011 John Ralls <jralls at ceridwen.us>
+
+include $(top_srcdir)/test-templates/Makefile.decl
+
+
+#You will only need one of these: It points to the module directory
+#after $(top_srcdir) or $(top_builddir):
+MODULEPATH = src/optional/gtkmm
+
+test_gtkmm_SOURCES = \
+	test-gtkmm.cpp \
+	test-book.cpp
+
+test_gtkmm_HEADERSS = \
+	$(top_srcdir)/${MODULEPATH}/gncmm/Book.hpp
+
+TEST_PROGS += test-gtkmm
+
+noinst_PROGRAMS = ${TEST_PROGS}
+
+#The tests might require more libraries, but try to keep them
+#as independent as possible.
+test_gtkmm_LDADD = ${top_builddir}/${MODULEPATH}/libgncmod-gtkmm.la \
+  ${top_builddir}/src/libqof/qof/libgnc-qof.la \
+  ${GTKMM_LIBS} \
+  ${GNOME_LIBS} \
+  ${GLADE_LIBS} \
+  ${GLIB_LIBS}
+
+AM_CPPFLAGS = \
+	${DEFAULT_INCLUDES} \
+	-DTESTPROG=test_gtkmm \
+  -I${top_srcdir}/src \
+  -I${top_srcdir}/src/test-core \
+  -I${top_srcdir}/src/libqof/qof \
+  -I${top_srcdir}/src/gnc-module \
+  -I${top_srcdir}/src/core-utils \
+  -I${top_srcdir}/src/engine \
+  -I${top_srcdir}/src/optional/gtkmm \
+  ${GTKMM_CFLAGS} \
+  ${GNOME_CFLAGS} \
+  ${GLADE_CFLAGS} \
+  ${GLIB_CFLAGS}

Added: gnucash/trunk/src/optional/gtkmm/test/test-book.cpp
===================================================================
--- gnucash/trunk/src/optional/gtkmm/test/test-book.cpp	                        (rev 0)
+++ gnucash/trunk/src/optional/gtkmm/test/test-book.cpp	2011-10-17 20:10:56 UTC (rev 21431)
@@ -0,0 +1,156 @@
+/********************************************************************
+ * test-book.cpp: GLib g_test test suite for gnc::Book.		    *
+ * Copyright 2011 Christian Stimming
+ * Copyright 2011 John Ralls <jralls at ceridwen.us>		    *
+ *                                                                  *
+ * 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 <string.h>
+#include <glib.h>
+
+#include "gncmm/Book.hpp"
+
+extern "C" {
+#include "qofbook-p.h"
+#include "qofbookslots.h"
+#include "test-stuff.h"
+}
+
+static const gchar *suitename = "/optional/gtkmm";
+void test_suite_gtkmm_book();
+
+typedef struct
+{
+    QofBook *book;
+} Fixture;
+
+static struct
+{
+    guint param;
+    gpointer data;
+    gboolean called;
+    gchar* msg;
+} test_struct;
+
+static struct
+{
+    gboolean col1_called;
+    gboolean col2_called;
+    gpointer data;
+} col_struct;
+
+static void
+setup( Fixture *fixture, gconstpointer pData )
+{
+    fixture->book = qof_book_new();
+    g_test_message("Book ref counter: is floating=%d",
+                   g_object_is_floating(G_OBJECT(fixture->book)));
+}
+
+static void
+teardown( Fixture *fixture, gconstpointer pData )
+{
+    g_test_message("Book ref counter: is floating=%d",
+                   g_object_is_floating(G_OBJECT(fixture->book)));
+    qof_book_destroy( fixture->book );
+}
+
+/* use g_free on test_struct.msg after this function been called */
+static gboolean
+handle_faults ( const char * log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
+{
+    test_struct.msg = (gchar *) g_strdup( msg );
+    return FALSE;
+}
+
+/* mock dirty callback function */
+static void
+mock_dirty_cb (QofBook *book, gboolean dirty, gpointer user_data)
+{
+    test_struct.called = TRUE;
+    g_test_message( "Checking if book is valid" );
+    g_assert( book );
+    g_assert( QOF_IS_BOOK( book ) );
+    g_test_message( "Checking parameters" );
+    g_assert( dirty );
+    g_assert( user_data == test_struct.data );
+}
+
+/* mock callback for qof_book_foreach_collection testing */
+static void
+mock_foreach_collection (QofCollection *col, gpointer user_data)
+{
+    g_test_message( "Checking if collection and data passed correctly" );
+    g_assert( col );
+    g_assert( user_data == col_struct.data );
+    if ( g_strcmp0( qof_collection_get_type(col), "my_type" ) == 0 )
+        col_struct.col1_called = TRUE;
+    else if ( g_strcmp0( qof_collection_get_type(col), "my_type2" ) == 0 )
+        col_struct.col2_called = TRUE;
+}
+
+/* mock final callback function */
+static void
+mock_final_cb (QofBook *book, gpointer key, gpointer user_data)
+{
+    test_struct.called = TRUE;
+    g_assert( book );
+    g_assert( QOF_IS_BOOK( book ) );
+    g_test_message( "Checking parameters" );
+    g_assert_cmpstr( (gchar*)key, == , "key" );
+    g_assert_cmpstr( (gchar*)user_data, == , "data" );
+}
+
+static void
+test_book_readonly( Fixture *fixture, gconstpointer pData )
+{
+    g_assert( fixture->book != NULL );
+    g_assert( !qof_book_is_readonly( fixture->book ) );
+    qof_book_mark_readonly( fixture->book );
+    g_assert( qof_book_is_readonly( fixture->book ) );
+}
+static void
+test_book_wrap( Fixture *fixture, gconstpointer pData )
+{
+    g_assert( fixture->book != NULL );
+    {
+        Glib::RefPtr<gnc::Book> book = Glib::wrap(fixture->book);
+        g_assert( book->gobj() == fixture->book );
+        g_assert(G_IS_OBJECT(fixture->book));
+    }
+    g_assert(G_IS_OBJECT(fixture->book)); // uh oh.
+    // Due to the missing ref counting in setup() and teardown(), the
+    // Glib::wrap pointer is the first reference. Once it gets out of
+    // scope, the underlying object will be deleted as nobody owns a
+    // ref() anymore. Oops.
+
+//     g_test_message("Book ref counter: is floating=%d",
+//                    g_object_is_floating(G_OBJECT(fixture->book)));
+
+    qof_book_mark_readonly( fixture->book ); // this will crash due to the deleted book
+    g_assert( qof_book_is_readonly( fixture->book ) );
+}
+
+void test_suite_gtkmm_book()
+{
+    GNC_TEST_ADD( suitename, "readonly", Fixture, NULL, setup, test_book_readonly, teardown );
+    GNC_TEST_ADD( suitename, "wrap", Fixture, NULL, setup, test_book_wrap, teardown );
+}


Property changes on: gnucash/trunk/src/optional/gtkmm/test/test-book.cpp
___________________________________________________________________
Added: svn:eol-style
   + LF

Added: gnucash/trunk/src/optional/gtkmm/test/test-gtkmm.cpp
===================================================================
--- gnucash/trunk/src/optional/gtkmm/test/test-gtkmm.cpp	                        (rev 0)
+++ gnucash/trunk/src/optional/gtkmm/test/test-gtkmm.cpp	2011-10-17 20:10:56 UTC (rev 21431)
@@ -0,0 +1,59 @@
+/********************************************************************
+ * testmain.c: GLib g_test test execution file.			    *
+ * Copyright 2011 Christian Stimming
+ * Copyright 2011 John Ralls <jralls at ceridwen.us>		    *
+ *                                                                  *
+ * 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 <glibmm.h>
+extern "C" {
+#include "qof.h"
+
+    gint libgncmod_gtkmm_gnc_module_init(gint refcount);
+}
+// c++ includes
+#include <gtkmm.h>
+// And our own plugin
+#include "gncmm/wrap_init.hpp"
+
+
+extern void test_suite_gtkmm_book();
+
+int
+main (int   argc,
+      char *argv[])
+{
+    g_type_init(); 			/* Initialize the GObject system */
+    g_test_init ( &argc, &argv, NULL ); 	/* initialize test program */
+    qof_log_init_filename_special("stderr"); /* Init the log system */
+    g_test_bug_base("https://bugzilla.gnome.org/show_bug.cgi?id="); /* init the bugzilla URL */
+
+    Gtk::Main::init_gtkmm_internals();
+    gnc::wrap_init();
+    //libgncmod_gtkmm_gnc_module_init(1);
+
+    test_suite_gtkmm_book();
+
+    return g_test_run( );
+}
+
+


Property changes on: gnucash/trunk/src/optional/gtkmm/test/test-gtkmm.cpp
___________________________________________________________________
Added: svn:eol-style
   + LF



More information about the gnucash-changes mailing list