Skip to content

Commit

Permalink
[test-tree-container.cpp] Comprehensive tests for gnc-tree-container.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Aug 20, 2023
1 parent 8d0bac9 commit 9768112
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gnucash/gnome-utils/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_tree_container_SOURCES}
${test_autoclear_SOURCES})
122 changes: 122 additions & 0 deletions gnucash/gnome-utils/test/test-tree-container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/********************************************************************
* test-tree-container.cpp: *
* *
* 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 [email protected] *
********************************************************************/

#include "config.h"
#include <glib.h>
#include <gtk/gtk.h>
#include "../gnc-tree-container.hpp"
#include <gtest/gtest.h>
#include <string>

enum {
COLUMN_STRING,
COLUMN_INT,
COLUMN_BOOLEAN,
N_COLUMNS
};


TEST(GncTreeContainer, Equality)
{
auto store1 = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
auto store2 = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);

GncTreeContainer container1{GTK_TREE_MODEL(store1)};
GncTreeContainer container2{GTK_TREE_MODEL(store2)};

// these are null tests
EXPECT_TRUE (container1.begin() == container1.begin());
EXPECT_TRUE (container1.end() == container1.end());

EXPECT_TRUE (container1.begin() == container1.end());
EXPECT_TRUE (container1.size() == 0);
EXPECT_TRUE (container2.size() == 0);
EXPECT_TRUE (container1.empty());
EXPECT_TRUE (container1.empty());

EXPECT_FALSE (container1.begin() == container2.begin());
EXPECT_FALSE (container1.end() == container2.end());

// both containers have identical contents
container1.append()->set_column (COLUMN_STRING, "1");
container2.append()->set_column (COLUMN_STRING, "1");

// the containers are now no longer empty
EXPECT_FALSE (container1.begin() == container1.end());
EXPECT_FALSE (container2.begin() == container2.end());
EXPECT_TRUE (container1.size() == 1);
EXPECT_TRUE (container2.size() == 1);

// however the iterators behave as expected -- iterators from
// store1 must differ from iterators from store2
EXPECT_FALSE (container1.begin() == container2.begin());
EXPECT_FALSE (container1.end() == container2.end());

g_object_unref (store1);
g_object_unref (store2);
}

TEST(GncTreeContainer, Basic)
{
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 ();
iter->set_column (COLUMN_STRING, str);
iter->set_column (COLUMN_INT, i);
iter->set_column (COLUMN_BOOLEAN, false);
}

// test non-empty
EXPECT_FALSE (container.empty());

// test size
EXPECT_TRUE (10 == container.size());

auto int_is_five = [](auto it){ return it.get_int(COLUMN_INT) == 5; };
auto iter_found = std::find_if (container.begin(), container.end(), int_is_five);
EXPECT_TRUE (iter_found.has_value());
EXPECT_EQ ("string 5", iter_found->get_string (COLUMN_STRING));

g_object_unref (store);
}

int main(int argc, char** argv)
{
if (gtk_init_check (nullptr, nullptr))
std::cout << "gtk init completed!" << std::endl;
else
std::cout << "no display present!" << std::endl;

// Initialize the Google Test framework
::testing::InitGoogleTest(&argc, argv);

// Run tests
return RUN_ALL_TESTS();
}

0 comments on commit 9768112

Please sign in to comment.