-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test-tree-container.cpp] Comprehensive tests for gnc-tree-container.hpp
- Loading branch information
1 parent
8d0bac9
commit 9768112
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |