From 90cfc55c08e70e3c10d961cffb1a1e4910a0bd0f Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 19 Aug 2023 08:00:00 +0800 Subject: [PATCH] [test-tree-container.cpp] some tests --- gnucash/gnome-utils/test/CMakeLists.txt | 20 ++++- .../gnome-utils/test/test-tree-container.cpp | 73 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 gnucash/gnome-utils/test/test-tree-container.cpp diff --git a/gnucash/gnome-utils/test/CMakeLists.txt b/gnucash/gnome-utils/test/CMakeLists.txt index 1540c20707e..2a14f0d8fc9 100644 --- a/gnucash/gnome-utils/test/CMakeLists.txt +++ b/gnucash/gnome-utils/test/CMakeLists.txt @@ -39,13 +39,31 @@ set(test_autoclear_LIBS gtest ) +set(test_tree_container_SOURCES + test-tree-container.cpp +) + +set(test_tree_container_INCLUDE_DIRS +) + +set(test_tree_container_LIBS + gnc-gnome-utils + gtest +) + gnc_add_test(test-autoclear "${test_autoclear_SOURCES}" test_autoclear_INCLUDE_DIRS test_autoclear_LIBS ) +gnc_add_test(test-tree-container "${test_tree_container_SOURCES}" + test_tree_container_INCLUDE_DIRS + test_tree_container_LIBS +) + gnc_add_scheme_tests(test-load-gnome-utils-module.scm) set_dist_list(test_gnome_utils_DIST CMakeLists.txt test-gnc-recurrence.c test-load-gnome-utils-module.scm - ${test_autoclear_SOURCES}) + ${test_autoclear_SOURCES} + ${test_tree_container_SOURCES}) diff --git a/gnucash/gnome-utils/test/test-tree-container.cpp b/gnucash/gnome-utils/test/test-tree-container.cpp new file mode 100644 index 00000000000..4eb5bb3c887 --- /dev/null +++ b/gnucash/gnome-utils/test/test-tree-container.cpp @@ -0,0 +1,73 @@ +/******************************************************************** + * test-assistant-stock-transaction.cpp: * + * Copyright 2023 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, you can retrieve it from * + * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html * + * or 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@gnu.org * + ********************************************************************/ +#include "config.h" +#include +#include +#include "../gnc-tree-container.hpp" +#include +#include + +enum { + COLUMN_STRING, + COLUMN_INT, + COLUMN_BOOLEAN, + N_COLUMNS +}; + +TEST(GncTreeContainer, Basic) { + if (!gtk_init_check (nullptr, nullptr)) + std::cout << "no display present!" << std::endl; + + std::cout << "Init successful" << '\n'; + + auto store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN); + GncTreeContainer container{GTK_TREE_MODEL(store)}; + + // test empty + EXPECT_TRUE (container.empty()); + + for (size_t i = 0; i < 10; i++) + { + auto str = std::string("string ") + std::to_string(i); + auto iter = container.append (); + gtk_list_store_set (store, &iter->get_iter(), + COLUMN_STRING, str.c_str(), + COLUMN_INT, i, + COLUMN_BOOLEAN, false, + -1); + } + + // test non-empty + EXPECT_EQ(false, container.empty()); + + EXPECT_TRUE (10 == container.size()); + + auto is_five = [](auto it){ return it.get_int(COLUMN_INT) == 5; }; + auto iter = std::find_if (container.begin(), container.end(), is_five); + EXPECT_FALSE (iter == container.end()); + EXPECT_EQ ("string 5", iter->get_string (COLUMN_STRING)); + + g_object_unref (store); + std::cout << "Tests complete" << '\n'; +}