First test for gnc-trans-props-tx

Start with test for parse_monetary
- a few basic tests
- extra tests for recent bug fixes, like
  - allow blank as thousands separator
  - double minus sign equals positive number
This commit is contained in:
Geert Janssens 2023-02-28 15:50:48 +01:00
parent d75ab275f1
commit 37e0f407e9
2 changed files with 95 additions and 1 deletions

View File

@ -28,6 +28,26 @@ if (NOT WIN32)
# gtest_csv_imp_INCLUDES gtest_csv_imp_LIBS)
endif()
set(GNC_IMP_PROPS_TX_TEST_INCLUDE_DIRS
${CMAKE_BINARY_DIR}/common # for config.h
${CMAKE_SOURCE_DIR}/gnucash/import-export/csv-imp
${GTEST_INCLUDE_DIR}
)
set(GNC_IMP_PROPS_TX_TEST_LIBS
gnc-engine
test-core
gnc-generic-import
gtest)
set(GNC_IMP_PROPS_TX_TEST_SOURCES
../gnc-imp-props-tx.cpp
gtest-gnc-imp-props-tx.cpp
)
gnc_add_test(test-gnc-imp-props-tx "${GNC_IMP_PROPS_TX_TEST_SOURCES}"
GNC_IMP_PROPS_TX_TEST_INCLUDE_DIRS GNC_IMP_PROPS_TX_TEST_LIBS)
set_dist_list(test_csv_import_DIST CMakeLists.txt
test-tx-import.cpp test-tokenizer.cpp
test-tx-import.cpp test-tokenizer.cpp gtest-gnc-imp-props-tx.cpp
sample1.csv ${test_csv_imp_SOURCES})

View File

@ -0,0 +1,74 @@
#include <gtest/gtest.h>
#include <config.h>
#include <gnc-datetime.hpp>
#include <gnc-imp-props-tx.hpp>
#include <qofbook.h>
#include <engine-helpers.h>
#include <gnc-ui-util.h>
// Test fixture for tests without bayesian matching
class GncImpPropsTxTest : public testing::Test
{
protected:
void SetUp()
{
}
void TearDown()
{
}
};
/* Tests using fixture GncImpPropsTxTest */
extern GncNumeric parse_monetary (const std::string &str, int currency_format);
//! Test for function parse_monetary (const std::string &str, int currency_format)
TEST_F(GncImpPropsTxTest, ParseMonetary)
{
using namespace testing;
/* Empty string */
auto value = parse_monetary ("", 1);
EXPECT_EQ (value, (GncNumeric {0, 1}));
/* No digits in string */
EXPECT_THROW (parse_monetary ("abc", 1), std::invalid_argument);
/* Tests using currency_format "Period" (1) */
value = parse_monetary ("1,000.00", 1);
EXPECT_EQ (value, (GncNumeric {100000, 100}));
value = parse_monetary ("-1,001.00", 1);
EXPECT_EQ (value, (GncNumeric {-100100, 100}));
value = parse_monetary ("-1,002.00$", 1);
EXPECT_EQ (value, (GncNumeric {-100200, 100}));
// 798334 - Importing transactions from CSV with space as thousand separator
value = parse_monetary ("1 000 003.00", 1);
EXPECT_EQ (value, (GncNumeric {100000300, 100}));
auto input = "1\u202F000\u202F004.00"; // Using Polish thousand separator
value = parse_monetary (input, 1);
EXPECT_EQ (value, (GncNumeric {100000400, 100}));
// Bug 798572 - Parse numbers with two minus signs as a positive numbers
value = parse_monetary ("--1,005.00", 1);
EXPECT_EQ (value, (GncNumeric {100500, 100}));
/* Tests using currency_format "Comma" (2) */
value = parse_monetary ("2.000,00", 2);
EXPECT_EQ (value, (GncNumeric {200000, 100}));
// 798334 - Importing transactions from CSV with space as thousand separator
value = parse_monetary ("2 000 001,00", 2);
EXPECT_EQ (value, (GncNumeric {200000100, 100}));
input = "2\u202F000\u202F002,00"; // Using Polish thousand separator
value = parse_monetary (input, 2);
EXPECT_EQ (value, (GncNumeric {200000200, 100}));
/* Things that will throw */
EXPECT_THROW (parse_monetary ("3000.00.01", 1), std::invalid_argument);
};