diff --git a/libgnucash/app-utils/gnc-option.cpp b/libgnucash/app-utils/gnc-option.cpp index 9492d7d313..1ced4cdde7 100644 --- a/libgnucash/app-utils/gnc-option.cpp +++ b/libgnucash/app-utils/gnc-option.cpp @@ -113,7 +113,7 @@ GncOptionDateValue::get_value() const m_period == RelativeDatePeriod::END_PREV_YEAR || m_period == RelativeDatePeriod::START_PREV_QUARTER || m_period == RelativeDatePeriod::END_PREV_QUARTER; - + if (period.tm_mon == now.tm_mon && period.tm_mday == now.tm_mday) { //No set accounting period, use the calendar year @@ -217,13 +217,14 @@ GncOptionDateValue::in_stream(std::istream& iss) QofInstance* qof_instance_from_string(const std::string& str, GncOptionUIType type) { - auto guid{static_cast(gnc::GUID::from_string(str))}; QofIdType qof_type; + bool commodity_type{false}; switch(type) { case GncOptionUIType::CURRENCY: case GncOptionUIType::COMMODITY: qof_type = "Commodity"; + commodity_type = true; break; case GncOptionUIType::BUDGET: qof_type = "Budget"; @@ -256,6 +257,17 @@ qof_instance_from_string(const std::string& str, GncOptionUIType type) break; } auto book{gnc_get_current_book()}; + if (commodity_type) + { + auto sep{str.find(":")}; + auto name_space{str.substr(0, sep)}; + auto mnemonic{str.substr(sep + 1, -1)}; + auto table = gnc_commodity_table_get_table(book); + return QOF_INSTANCE(gnc_commodity_table_lookup(table, + name_space.c_str(), + mnemonic.c_str())); + } + auto guid{static_cast(gnc::GUID::from_string(str))}; auto col{qof_book_get_collection(book, qof_type)}; return QOF_INSTANCE(qof_collection_lookup_entity(col, &guid)); } diff --git a/libgnucash/app-utils/gnc-optiondb.i b/libgnucash/app-utils/gnc-optiondb.i index 98cbc908a1..8ad19c7f40 100644 --- a/libgnucash/app-utils/gnc-optiondb.i +++ b/libgnucash/app-utils/gnc-optiondb.i @@ -184,6 +184,9 @@ using Account = struct account_s; %ignore GncOptionDateValue(GncOptionDateValue&&); %ignore GncOptionDateValue::operator=(const GncOptionDateValue&); %ignore GncOptionDateValue::operator=(GncOptionDateValue&&); +%ignore operator<<(std::ostream&, const GncOption&); +%ignore operator>>(std::istream&, GncOption&); + %rename(absolute) RelativeDatePeriod::ABSOLUTE; %rename(today) RelativeDatePeriod::TODAY; %rename(start_this_month) RelativeDatePeriod::START_THIS_MONTH; diff --git a/libgnucash/app-utils/test/gtest-gnc-option.cpp b/libgnucash/app-utils/test/gtest-gnc-option.cpp index fb2038d9fd..2f4a9afd21 100644 --- a/libgnucash/app-utils/test/gtest-gnc-option.cpp +++ b/libgnucash/app-utils/test/gtest-gnc-option.cpp @@ -260,50 +260,90 @@ TEST_F(GncOptionCommodityTest, test_currency_validator) EXPECT_FALSE(option.validate(QOF_INSTANCE(m_aapl))); } +static inline std::string make_currency_str(gnc_commodity* cur) +{ + std::string cur_str{gnc_commodity_get_mnemonic(cur)}; + return cur_str; +} + +static inline std::string make_commodity_str(gnc_commodity* com) +{ + std::string com_str{gnc_commodity_get_namespace(com)}; + com_str += " "; + com_str += gnc_commodity_get_mnemonic(com); + return com_str; } -TEST_F(GncOptionTest, test_qofinstance_out) +static inline std::string make_currency_SCM_str(gnc_commodity* cur) { - auto table = gnc_commodity_table_new(); - qof_book_set_data(m_book, GNC_COMMODITY_TABLE, table); - auto eur = gnc_commodity_new(m_book, "Euro", "ISO4217", "EUR", NULL, 100); - auto option = make_currency_option("foo", "bar", "baz", "Phony Option", eur, true); + std::string cur_str{gnc_commodity_get_mnemonic(cur)}; + cur_str.insert(0, "\""); + cur_str += "\""; + return cur_str; +} + +static inline std::string make_commodity_SCM_str(gnc_commodity* com) +{ + std::string com_str{commodity_scm_intro}; + com_str += "\""; + com_str += gnc_commodity_get_namespace(com); + com_str += "\" \""; + com_str += gnc_commodity_get_mnemonic(com); + com_str += "\")"; + return com_str; +} - std::string eur_guid{gnc::GUID{*qof_instance_get_guid(eur)}.to_string()}; +TEST_F(GncOptionCommodityTest, test_currency_out) +{ + auto option = make_currency_option("foo", "bar", "baz", "Phony Option", + m_eur, true); + + std::string eur_str{make_currency_str(m_eur)}; std::ostringstream oss; oss << option; - EXPECT_EQ(eur_guid, oss.str()); - gnc_commodity_destroy(eur); - qof_book_set_data(m_book, GNC_COMMODITY_TABLE, nullptr); - gnc_commodity_table_destroy(table); + EXPECT_EQ(eur_str, oss.str()); } -TEST_F(GncOptionTest, test_qofinstance_in) +TEST_F(GncOptionCommodityTest, test_commodity_out) { - auto table = gnc_commodity_table_new(); - qof_book_set_data(m_book, GNC_COMMODITY_TABLE, table); - auto eur = gnc_commodity_new(m_book, "Euro", "ISO4217", "EUR", NULL, 100); - auto usd = gnc_commodity_new(m_book, "United States Dollar", - "CURRENCY", "USD", NULL, 100); - auto hpe = gnc_commodity_new(m_book, "Hewlett Packard Enterprise, Inc.", - "NYSE", "HPE", NULL, 1); - auto option = make_currency_option("foo", "bar", "baz", "Phony Option", eur, true); + GncOption option{"foo", "bar", "baz", "Phony Option", QOF_INSTANCE(m_hpe), + GncOptionUIType::COMMODITY}; + std::string hpe_str{make_commodity_str(m_hpe)}; + std::ostringstream oss; + oss << option; + EXPECT_EQ(hpe_str, oss.str()); +} + +TEST_F(GncOptionCommodityTest, test_currency_in) +{ + + auto option = make_currency_option("foo", "bar", "baz", "Phony Option", + m_eur, true); EXPECT_THROW({ - std::string hpe_guid{gnc::GUID{*qof_instance_get_guid(hpe)}.to_string()}; - std::istringstream iss{hpe_guid}; + std::string hpe_str{make_commodity_str(m_hpe)}; + std::istringstream iss{hpe_str}; iss >> option; }, std::invalid_argument); EXPECT_NO_THROW({ - std::string usd_guid{gnc::GUID{*qof_instance_get_guid(usd)}.to_string()}; - std::istringstream iss{usd_guid}; + std::string usd_str{make_currency_str(m_usd)}; + std::istringstream iss{usd_str}; iss >> option; - EXPECT_EQ(QOF_INSTANCE(usd), option.get_value()); + EXPECT_EQ(QOF_INSTANCE(m_usd), option.get_value()); }); - gnc_commodity_destroy(eur); - gnc_commodity_destroy(usd); - qof_book_set_data(m_book, GNC_COMMODITY_TABLE, nullptr); - gnc_commodity_table_destroy(table); +} + +TEST_F(GncOptionCommodityTest, test_commodity_in) +{ + GncOption option{"foo", "bar", "baz", "Phony Option", QOF_INSTANCE(m_aapl), + GncOptionUIType::COMMODITY}; + + std::string hpe_str{make_commodity_str(m_hpe)}; + std::istringstream iss{hpe_str}; + iss >> option; + EXPECT_EQ(QOF_INSTANCE(m_hpe), option.get_value()); +} + } class GncUIItem