From 51715e454c022413c141e3fb5a2aa570d33dc16e Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Fri, 23 Oct 2020 14:04:16 +0200 Subject: [PATCH 01/12] [auto-clear] Add tests --- libgnucash/app-utils/test/CMakeLists.txt | 18 +++ libgnucash/app-utils/test/test-autoclear.cpp | 124 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 libgnucash/app-utils/test/test-autoclear.cpp diff --git a/libgnucash/app-utils/test/CMakeLists.txt b/libgnucash/app-utils/test/CMakeLists.txt index b738202fd6..85b85679ef 100644 --- a/libgnucash/app-utils/test/CMakeLists.txt +++ b/libgnucash/app-utils/test/CMakeLists.txt @@ -85,3 +85,21 @@ set_dist_list(test_app_utils_DIST ${test_app_utils_scheme_SOURCES} ${test_app_utils_SOURCES} ) + + +set(test_autoclear_SOURCES + test-autoclear.cpp +) +set(test_autoclear_INCLUDE_DIRS + ${APP_UTILS_TEST_INCLUDE_DIRS} + ${GTEST_INCLUDE_DIR} +) +set(test_autoclear_LIBS + ${APP_UTILS_TEST_LIBS} + gtest +) + +gnc_add_test(test-autoclear "${test_autoclear_SOURCES}" + test_autoclear_INCLUDE_DIRS + test_autoclear_LIBS +) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp new file mode 100644 index 0000000000..834c77e915 --- /dev/null +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -0,0 +1,124 @@ +/******************************************************************** + * test-autoclear.c: test suite for Auto-Clear * + * Copyright 2020 Cristian Klein * + * * + * 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" + +// GoogleTest is written in C++, however, the function we test in C. +extern "C" { +#include "../gnc-ui-balances.h" +} +#include "Split.h" + +#include "gtest/gtest.h" + +const int DENOM = 100; //< Denomerator is always 100 for simplicity. + +struct SplitDatum { + const char *memo; + int amount; //< Numerator of amount. + bool cleared; +}; + +SplitDatum splitData[] = { + { "Memo 01", - 8234, true }, + { "Memo 02", -156326, true }, + { "Memo 03", - 4500, true }, + { "Memo 04", -694056, true }, + { "Memo 05", - 7358, true }, + { "Memo 06", - 11700, true }, + { "Memo 07", - 20497, true }, + { "Memo 08", - 11900, true }, + { "Memo 09", - 8275, true }, + { "Memo 10", - 58700, true }, + { "Memo 11", +100000, true }, + { "Memo 12", - 13881, true }, + { "Memo 13", - 5000, true }, + { "Memo 14", +200000, true }, + { "Memo 15", - 16800, true }, + { "Memo 16", -152000, true }, + { "Memo 17", +160000, false }, + { "Memo 18", - 63610, false }, + { "Memo 19", - 2700, false }, + { "Memo 20", - 15400, false }, + { "Memo 21", - 3900, false }, + { "Memo 22", - 22042, false }, + { "Memo 23", - 2900, false }, + { "Memo 24", - 10900, false }, + { "Memo 25", - 44400, false }, + { "Memo 26", - 9200, false }, + { "Memo 27", - 7900, false }, + { "Memo 28", - 1990, false }, + { "Memo 29", - 7900, false }, + { "Memo 30", - 61200, false }, +}; + +struct TestCase { + int amount; + const char *expectedErr; +}; + +TestCase testCases[] = { + { 0, "The selected amount cannot be cleared.", }, + { -869227, "Account is already at Auto-Clear Balance." }, // No splits need to be cleared. + { -877127, "Cannot uniquely clear splits. Found multiple possibilities." }, // Two splits need to be cleared. + { -891269, NULL }, // One split need to be cleared. + { -963269, NULL }, // All splits need to be cleared. +}; + +TEST(AutoClear, AutoClearAll) { + QofBook *book = qof_book_new (); + Account *account = xaccMallocAccount(book); + + for (auto &d : splitData) { + Split *split = xaccMallocSplit(book); + xaccSplitSetMemo(split, d.memo); + xaccSplitSetAmount(split, gnc_numeric_create(d.amount, DENOM)); + xaccSplitSetReconcile(split, d.cleared ? CREC : NREC); + + // This way of inserting a split, seems to actualy work best. :D + gnc_account_insert_split(account, split); + } + xaccAccountRecomputeBalance(account); + + for (auto &t : testCases) { + gnc_numeric amount_to_clear = gnc_numeric_create(t.amount, DENOM); + char *err; + + GList *splits_to_clear = gnc_account_get_autoclear_splits(account, amount_to_clear, &err); + + // Actually clear splits + for (GList *node = splits_to_clear; node; node = node->next) { + Split *split = (Split *)node->data; + xaccSplitSetReconcile(split, CREC); + } + + ASSERT_STREQ(err, t.expectedErr); + if (t.expectedErr == NULL) { + gnc_numeric c = xaccAccountGetClearedBalance(account); + ASSERT_EQ(c.num, t.amount); + ASSERT_EQ(c.denom, DENOM); + } + } + + qof_book_destroy(book); +} From a1806a2f9429b5f69403b812cd77fcae32371688 Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Fri, 23 Oct 2020 15:26:23 +0200 Subject: [PATCH 02/12] [auto-clear] Fix model --- libgnucash/app-utils/test/test-autoclear.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index 834c77e915..62fb188908 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -31,11 +31,11 @@ extern "C" { #include "gtest/gtest.h" -const int DENOM = 100; //< Denomerator is always 100 for simplicity. +const gint64 DENOM = 100; //< Denomerator is always 100 for simplicity. struct SplitDatum { const char *memo; - int amount; //< Numerator of amount. + gint64 amount; //< Numerator of amount. bool cleared; }; @@ -73,7 +73,7 @@ SplitDatum splitData[] = { }; struct TestCase { - int amount; + gint64 amount; const char *expectedErr; }; @@ -88,14 +88,15 @@ TestCase testCases[] = { TEST(AutoClear, AutoClearAll) { QofBook *book = qof_book_new (); Account *account = xaccMallocAccount(book); + xaccAccountSetName(account, "Test Account"); for (auto &d : splitData) { Split *split = xaccMallocSplit(book); xaccSplitSetMemo(split, d.memo); xaccSplitSetAmount(split, gnc_numeric_create(d.amount, DENOM)); xaccSplitSetReconcile(split, d.cleared ? CREC : NREC); + xaccSplitSetAccount(split, account); - // This way of inserting a split, seems to actualy work best. :D gnc_account_insert_split(account, split); } xaccAccountRecomputeBalance(account); From 2faecb207432027ebd9efd6c92b53c35bec44fa7 Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Fri, 23 Oct 2020 15:26:58 +0200 Subject: [PATCH 03/12] [auto-clear] Avoid testing ambigous auto-clear --- libgnucash/app-utils/test/test-autoclear.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index 62fb188908..cd193790bb 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -58,7 +58,7 @@ SplitDatum splitData[] = { { "Memo 16", -152000, true }, { "Memo 17", +160000, false }, { "Memo 18", - 63610, false }, - { "Memo 19", - 2700, false }, + { "Memo 19", - 2702, false }, { "Memo 20", - 15400, false }, { "Memo 21", - 3900, false }, { "Memo 22", - 22042, false }, @@ -68,7 +68,7 @@ SplitDatum splitData[] = { { "Memo 26", - 9200, false }, { "Memo 27", - 7900, false }, { "Memo 28", - 1990, false }, - { "Memo 29", - 7900, false }, + { "Memo 29", - 7901, false }, { "Memo 30", - 61200, false }, }; @@ -80,9 +80,9 @@ struct TestCase { TestCase testCases[] = { { 0, "The selected amount cannot be cleared.", }, { -869227, "Account is already at Auto-Clear Balance." }, // No splits need to be cleared. - { -877127, "Cannot uniquely clear splits. Found multiple possibilities." }, // Two splits need to be cleared. - { -891269, NULL }, // One split need to be cleared. - { -963269, NULL }, // All splits need to be cleared. + { -869300, "The selected amount cannot be cleared." }, + { -869230, NULL }, + { -963272, NULL }, // All splits need to be cleared. }; TEST(AutoClear, AutoClearAll) { From 5cd777b6739c14ccefa3f88996b63b4f1b1eff95 Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Fri, 23 Oct 2020 15:30:35 +0200 Subject: [PATCH 04/12] [auto-clear] Fix test: Add xaccAccountBeginEdit / xaccAccountCommitEdit --- libgnucash/app-utils/test/test-autoclear.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index cd193790bb..b6ba6ba196 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -90,6 +90,7 @@ TEST(AutoClear, AutoClearAll) { Account *account = xaccMallocAccount(book); xaccAccountSetName(account, "Test Account"); + xaccAccountBeginEdit(account); for (auto &d : splitData) { Split *split = xaccMallocSplit(book); xaccSplitSetMemo(split, d.memo); @@ -99,7 +100,7 @@ TEST(AutoClear, AutoClearAll) { gnc_account_insert_split(account, split); } - xaccAccountRecomputeBalance(account); + xaccAccountCommitEdit(account); for (auto &t : testCases) { gnc_numeric amount_to_clear = gnc_numeric_create(t.amount, DENOM); From 9bf550d38f19498f78562d2c4c8ed08ec784056a Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Fri, 23 Oct 2020 16:34:46 +0200 Subject: [PATCH 05/12] [auto-clear] Include test for ambiguous cases --- libgnucash/app-utils/test/test-autoclear.cpp | 53 ++++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index b6ba6ba196..73d9ae173e 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -39,7 +39,12 @@ struct SplitDatum { bool cleared; }; -SplitDatum splitData[] = { +struct TestCase { + gint64 amount; + const char *expectedErr; +}; + +std::vector easySplitData = { { "Memo 01", - 8234, true }, { "Memo 02", -156326, true }, { "Memo 03", - 4500, true }, @@ -72,12 +77,7 @@ SplitDatum splitData[] = { { "Memo 30", - 61200, false }, }; -struct TestCase { - gint64 amount; - const char *expectedErr; -}; - -TestCase testCases[] = { +std::vector easyTestCases = { { 0, "The selected amount cannot be cleared.", }, { -869227, "Account is already at Auto-Clear Balance." }, // No splits need to be cleared. { -869300, "The selected amount cannot be cleared." }, @@ -85,7 +85,33 @@ TestCase testCases[] = { { -963272, NULL }, // All splits need to be cleared. }; -TEST(AutoClear, AutoClearAll) { +std::vector ambiguousSplitData = { + { "Memo 01", -10, false }, + { "Memo 02", -10, false }, + { "Memo 03", -10, false }, +}; + +std::vector ambiguousTestCases = { + { -10, "Cannot uniquely clear splits. Found multiple possibilities." }, + { -20, "Cannot uniquely clear splits. Found multiple possibilities." }, + + // Commented out, auto-clear algorithm is not smart enough yet. + //{ -30, NULL }, +}; + +class AutoClearTest + : public testing::TestWithParam< + std::pair< + std::vector, + std::vector + > + > { +}; + +TEST_P(AutoClearTest, DoesAutoClear) { + auto splitData = GetParam().first; + auto testCase = GetParam().second; + QofBook *book = qof_book_new (); Account *account = xaccMallocAccount(book); xaccAccountSetName(account, "Test Account"); @@ -102,7 +128,7 @@ TEST(AutoClear, AutoClearAll) { } xaccAccountCommitEdit(account); - for (auto &t : testCases) { + for (auto &t : testCase) { gnc_numeric amount_to_clear = gnc_numeric_create(t.amount, DENOM); char *err; @@ -124,3 +150,12 @@ TEST(AutoClear, AutoClearAll) { qof_book_destroy(book); } + +INSTANTIATE_TEST_SUITE_P( + InstantiationAutoClearTest, + AutoClearTest, + testing::Values( + std::pair{ easySplitData, easyTestCases }, + std::pair{ ambiguousSplitData, ambiguousTestCases } + ) +); From 1cf7defee88db324e937a0e7c9d247c80a03603b Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Mon, 26 Oct 2020 12:33:13 +0100 Subject: [PATCH 06/12] [auto-clear] Address feedback --- libgnucash/app-utils/test/test-autoclear.cpp | 174 ++++++++++--------- 1 file changed, 90 insertions(+), 84 deletions(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index 73d9ae173e..780e67bb03 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -31,7 +31,7 @@ extern "C" { #include "gtest/gtest.h" -const gint64 DENOM = 100; //< Denomerator is always 100 for simplicity. +static const int64_t DENOM = 100; //< Denomerator is always 100 for simplicity. struct SplitDatum { const char *memo; @@ -39,100 +39,108 @@ struct SplitDatum { bool cleared; }; -struct TestCase { +struct Tests { gint64 amount; const char *expectedErr; }; -std::vector easySplitData = { - { "Memo 01", - 8234, true }, - { "Memo 02", -156326, true }, - { "Memo 03", - 4500, true }, - { "Memo 04", -694056, true }, - { "Memo 05", - 7358, true }, - { "Memo 06", - 11700, true }, - { "Memo 07", - 20497, true }, - { "Memo 08", - 11900, true }, - { "Memo 09", - 8275, true }, - { "Memo 10", - 58700, true }, - { "Memo 11", +100000, true }, - { "Memo 12", - 13881, true }, - { "Memo 13", - 5000, true }, - { "Memo 14", +200000, true }, - { "Memo 15", - 16800, true }, - { "Memo 16", -152000, true }, - { "Memo 17", +160000, false }, - { "Memo 18", - 63610, false }, - { "Memo 19", - 2702, false }, - { "Memo 20", - 15400, false }, - { "Memo 21", - 3900, false }, - { "Memo 22", - 22042, false }, - { "Memo 23", - 2900, false }, - { "Memo 24", - 10900, false }, - { "Memo 25", - 44400, false }, - { "Memo 26", - 9200, false }, - { "Memo 27", - 7900, false }, - { "Memo 28", - 1990, false }, - { "Memo 29", - 7901, false }, - { "Memo 30", - 61200, false }, -}; - -std::vector easyTestCases = { - { 0, "The selected amount cannot be cleared.", }, - { -869227, "Account is already at Auto-Clear Balance." }, // No splits need to be cleared. - { -869300, "The selected amount cannot be cleared." }, - { -869230, NULL }, - { -963272, NULL }, // All splits need to be cleared. +struct TestCase { + std::vector splits; + std::vector tests; }; -std::vector ambiguousSplitData = { - { "Memo 01", -10, false }, - { "Memo 02", -10, false }, - { "Memo 03", -10, false }, +TestCase easyTestCase = { + .splits = { + { "Memo 01", - 8234, true }, + { "Memo 02", -156326, true }, + { "Memo 03", - 4500, true }, + { "Memo 04", -694056, true }, + { "Memo 05", - 7358, true }, + { "Memo 06", - 11700, true }, + { "Memo 07", - 20497, true }, + { "Memo 08", - 11900, true }, + { "Memo 09", - 8275, true }, + { "Memo 10", - 58700, true }, + { "Memo 11", +100000, true }, + { "Memo 12", - 13881, true }, + { "Memo 13", - 5000, true }, + { "Memo 14", +200000, true }, + { "Memo 15", - 16800, true }, + { "Memo 16", -152000, true }, + { "Memo 17", +160000, false }, + { "Memo 18", - 63610, false }, + { "Memo 19", - 2702, false }, + { "Memo 20", - 15400, false }, + { "Memo 21", - 3900, false }, + { "Memo 22", - 22042, false }, + { "Memo 23", - 2900, false }, + { "Memo 24", - 10900, false }, + { "Memo 25", - 44400, false }, + { "Memo 26", - 9200, false }, + { "Memo 27", - 7900, false }, + { "Memo 28", - 1990, false }, + { "Memo 29", - 7901, false }, + { "Memo 30", - 61200, false }, + }, + .tests = { + { 0, "The selected amount cannot be cleared.", }, + { -869227, "Account is already at Auto-Clear Balance." }, // No splits need to be cleared. + { -869300, "The selected amount cannot be cleared." }, + { -869230, NULL }, + { -963272, NULL }, // All splits need to be cleared. + }, }; -std::vector ambiguousTestCases = { - { -10, "Cannot uniquely clear splits. Found multiple possibilities." }, - { -20, "Cannot uniquely clear splits. Found multiple possibilities." }, - - // Commented out, auto-clear algorithm is not smart enough yet. - //{ -30, NULL }, +TestCase ambiguousTestCase = { + .splits = { + { "Memo 01", -10, false }, + { "Memo 02", -10, false }, + { "Memo 03", -10, false }, + }, + .tests = { + { -10, "Cannot uniquely clear splits. Found multiple possibilities." }, + { -20, "Cannot uniquely clear splits. Found multiple possibilities." }, + + // Forbid auto-clear to be too smart. We expect the user to manually deal + // with such situations. + { -30, "Cannot uniquely clear splits. Found multiple possibilities." }, + }, }; -class AutoClearTest - : public testing::TestWithParam< - std::pair< - std::vector, - std::vector - > - > { +class AutoClearTest : public testing::TestWithParam { +protected: + std::shared_ptr book_; + Account *account_; // owned by book_ + TestCase testCase_; + +public: + AutoClearTest() : + book_(qof_book_new(), qof_book_destroy), + account_(xaccMallocAccount(book_.get())) + { + testCase_ = GetParam(); + + xaccAccountSetName(account_, "Test Account"); + xaccAccountBeginEdit(account_); + for (auto &d : testCase_.splits) { + Split *split = xaccMallocSplit(book_.get()); + xaccSplitSetMemo(split, d.memo); + xaccSplitSetAmount(split, gnc_numeric_create(d.amount, DENOM)); + xaccSplitSetReconcile(split, d.cleared ? CREC : NREC); + xaccSplitSetAccount(split, account_); + + gnc_account_insert_split(account_, split); + } + xaccAccountCommitEdit(account_); + } }; TEST_P(AutoClearTest, DoesAutoClear) { - auto splitData = GetParam().first; - auto testCase = GetParam().second; - - QofBook *book = qof_book_new (); - Account *account = xaccMallocAccount(book); - xaccAccountSetName(account, "Test Account"); - - xaccAccountBeginEdit(account); - for (auto &d : splitData) { - Split *split = xaccMallocSplit(book); - xaccSplitSetMemo(split, d.memo); - xaccSplitSetAmount(split, gnc_numeric_create(d.amount, DENOM)); - xaccSplitSetReconcile(split, d.cleared ? CREC : NREC); - xaccSplitSetAccount(split, account); - - gnc_account_insert_split(account, split); - } - xaccAccountCommitEdit(account); - - for (auto &t : testCase) { + for (auto &t : testCase_.tests) { gnc_numeric amount_to_clear = gnc_numeric_create(t.amount, DENOM); char *err; - GList *splits_to_clear = gnc_account_get_autoclear_splits(account, amount_to_clear, &err); + GList *splits_to_clear = gnc_account_get_autoclear_splits(account_, amount_to_clear, &err); // Actually clear splits for (GList *node = splits_to_clear; node; node = node->next) { @@ -142,20 +150,18 @@ TEST_P(AutoClearTest, DoesAutoClear) { ASSERT_STREQ(err, t.expectedErr); if (t.expectedErr == NULL) { - gnc_numeric c = xaccAccountGetClearedBalance(account); + gnc_numeric c = xaccAccountGetClearedBalance(account_); ASSERT_EQ(c.num, t.amount); ASSERT_EQ(c.denom, DENOM); } } - - qof_book_destroy(book); } INSTANTIATE_TEST_SUITE_P( InstantiationAutoClearTest, AutoClearTest, testing::Values( - std::pair{ easySplitData, easyTestCases }, - std::pair{ ambiguousSplitData, ambiguousTestCases } + easyTestCase, + ambiguousTestCase ) ); From 9d4c0b05dbd181734c687012573f1c51b11512fa Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Mon, 26 Oct 2020 12:35:09 +0100 Subject: [PATCH 07/12] [auto-clear] Improve #include style --- libgnucash/app-utils/test/test-autoclear.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index 780e67bb03..2696c2a9f8 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -27,9 +27,8 @@ extern "C" { #include "../gnc-ui-balances.h" } -#include "Split.h" - -#include "gtest/gtest.h" +#include +#include static const int64_t DENOM = 100; //< Denomerator is always 100 for simplicity. From 982cf86deb20f0b0c549e9f0efa9e0a24195ae2f Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Mon, 26 Oct 2020 14:48:20 +0100 Subject: [PATCH 08/12] [auto-clear] Fix test with googletest 1.8 --- libgnucash/app-utils/test/test-autoclear.cpp | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index 2696c2a9f8..f00b809765 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -27,6 +27,7 @@ extern "C" { #include "../gnc-ui-balances.h" } +#include #include #include @@ -106,19 +107,18 @@ TestCase ambiguousTestCase = { }, }; -class AutoClearTest : public testing::TestWithParam { +class AutoClearTest : public ::testing::TestWithParam { protected: std::shared_ptr book_; Account *account_; // owned by book_ - TestCase testCase_; + TestCase &testCase_; public: AutoClearTest() : book_(qof_book_new(), qof_book_destroy), - account_(xaccMallocAccount(book_.get())) + account_(xaccMallocAccount(book_.get())), + testCase_(*GetParam()) { - testCase_ = GetParam(); - xaccAccountSetName(account_, "Test Account"); xaccAccountBeginEdit(account_); for (auto &d : testCase_.splits) { @@ -156,11 +156,15 @@ TEST_P(AutoClearTest, DoesAutoClear) { } } -INSTANTIATE_TEST_SUITE_P( +// Silence "no previous declaration for" which is treated as error, due to -Werror +testing::internal::ParamGenerator gtest_InstantiationAutoClearTestAutoClearTest_EvalGenerator_(); +std::string gtest_InstantiationAutoClearTestAutoClearTest_EvalGenerateName_(const testing::TestParamInfo&); + +INSTANTIATE_TEST_CASE_P( InstantiationAutoClearTest, AutoClearTest, - testing::Values( - easyTestCase, - ambiguousTestCase + ::testing::Values( + &easyTestCase, + &ambiguousTestCase ) ); From e5378b204849dcdaca429e3ac2a57bedfd681877 Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Wed, 28 Oct 2020 22:25:56 +0100 Subject: [PATCH 09/12] [auto-clear] Make test more future-proof with GoogleTest --- libgnucash/app-utils/test/test-autoclear.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index f00b809765..b99b5d655e 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -156,11 +156,15 @@ TEST_P(AutoClearTest, DoesAutoClear) { } } +#ifndef INSTANTIATE_TEST_SUITE_P // Silence "no previous declaration for" which is treated as error, due to -Werror testing::internal::ParamGenerator gtest_InstantiationAutoClearTestAutoClearTest_EvalGenerator_(); std::string gtest_InstantiationAutoClearTestAutoClearTest_EvalGenerateName_(const testing::TestParamInfo&); INSTANTIATE_TEST_CASE_P( +#else // INSTANTIATE_TEST_SUITE_P +INSTANTIATE_TEST_SUITE_P( +#endif // INSTANTIATE_TEST_SUITE_P InstantiationAutoClearTest, AutoClearTest, ::testing::Values( From fa16a299480c583f6e8f8b37bb2f447e7311430d Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Wed, 28 Oct 2020 22:36:11 +0100 Subject: [PATCH 10/12] Add auto-clear to register page --- gnucash/ui/gnc-plugin-page-register-ui.xml | 1 + gnucash/ui/gnc-plugin-page-register2-ui.xml | 1 + 2 files changed, 2 insertions(+) diff --git a/gnucash/ui/gnc-plugin-page-register-ui.xml b/gnucash/ui/gnc-plugin-page-register-ui.xml index a32b3bd5ab..5a240a5b22 100644 --- a/gnucash/ui/gnc-plugin-page-register-ui.xml +++ b/gnucash/ui/gnc-plugin-page-register-ui.xml @@ -44,6 +44,7 @@ + diff --git a/gnucash/ui/gnc-plugin-page-register2-ui.xml b/gnucash/ui/gnc-plugin-page-register2-ui.xml index 3d903d9acf..8c4c4afaba 100644 --- a/gnucash/ui/gnc-plugin-page-register2-ui.xml +++ b/gnucash/ui/gnc-plugin-page-register2-ui.xml @@ -43,6 +43,7 @@ + From f26215feae0516f04670566dfe247018c0c3bcea Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Sun, 8 Nov 2020 09:58:17 +0100 Subject: [PATCH 11/12] [auto-tests] Fix misspelling --- libgnucash/app-utils/test/test-autoclear.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index b99b5d655e..4157458de2 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -31,7 +31,7 @@ extern "C" { #include #include -static const int64_t DENOM = 100; //< Denomerator is always 100 for simplicity. +static const int64_t DENOM = 100; //< Denominator is always 100 for simplicity. struct SplitDatum { const char *memo; From 69789b2b93253b1e41c10aab6a22dcc5c84043cd Mon Sep 17 00:00:00 2001 From: Cristian Klein Date: Sun, 8 Nov 2020 09:58:57 +0100 Subject: [PATCH 12/12] [auto-tests] Stick to CodingStandard for member variables --- libgnucash/app-utils/test/test-autoclear.cpp | 32 ++++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libgnucash/app-utils/test/test-autoclear.cpp b/libgnucash/app-utils/test/test-autoclear.cpp index 4157458de2..abb84e1c56 100644 --- a/libgnucash/app-utils/test/test-autoclear.cpp +++ b/libgnucash/app-utils/test/test-autoclear.cpp @@ -109,37 +109,37 @@ TestCase ambiguousTestCase = { class AutoClearTest : public ::testing::TestWithParam { protected: - std::shared_ptr book_; - Account *account_; // owned by book_ - TestCase &testCase_; + std::shared_ptr m_book; + Account *m_account; // owned by m_book + TestCase &m_testCase; public: AutoClearTest() : - book_(qof_book_new(), qof_book_destroy), - account_(xaccMallocAccount(book_.get())), - testCase_(*GetParam()) + m_book(qof_book_new(), qof_book_destroy), + m_account(xaccMallocAccount(m_book.get())), + m_testCase(*GetParam()) { - xaccAccountSetName(account_, "Test Account"); - xaccAccountBeginEdit(account_); - for (auto &d : testCase_.splits) { - Split *split = xaccMallocSplit(book_.get()); + xaccAccountSetName(m_account, "Test Account"); + xaccAccountBeginEdit(m_account); + for (auto &d : m_testCase.splits) { + Split *split = xaccMallocSplit(m_book.get()); xaccSplitSetMemo(split, d.memo); xaccSplitSetAmount(split, gnc_numeric_create(d.amount, DENOM)); xaccSplitSetReconcile(split, d.cleared ? CREC : NREC); - xaccSplitSetAccount(split, account_); + xaccSplitSetAccount(split, m_account); - gnc_account_insert_split(account_, split); + gnc_account_insert_split(m_account, split); } - xaccAccountCommitEdit(account_); + xaccAccountCommitEdit(m_account); } }; TEST_P(AutoClearTest, DoesAutoClear) { - for (auto &t : testCase_.tests) { + for (auto &t : m_testCase.tests) { gnc_numeric amount_to_clear = gnc_numeric_create(t.amount, DENOM); char *err; - GList *splits_to_clear = gnc_account_get_autoclear_splits(account_, amount_to_clear, &err); + GList *splits_to_clear = gnc_account_get_autoclear_splits(m_account, amount_to_clear, &err); // Actually clear splits for (GList *node = splits_to_clear; node; node = node->next) { @@ -149,7 +149,7 @@ TEST_P(AutoClearTest, DoesAutoClear) { ASSERT_STREQ(err, t.expectedErr); if (t.expectedErr == NULL) { - gnc_numeric c = xaccAccountGetClearedBalance(account_); + gnc_numeric c = xaccAccountGetClearedBalance(m_account); ASSERT_EQ(c.num, t.amount); ASSERT_EQ(c.denom, DENOM); }