mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
add more tests for complex number formats
This commit is contained in:
parent
c7bc216026
commit
aa0dee3191
|
@ -274,9 +274,7 @@ bool parse_condition(const std::string &string, section &s)
|
|||
|
||||
bool is_hex(char c)
|
||||
{
|
||||
if (c >= 'A' || c <= 'F') return true;
|
||||
if (c >= '0' || c <= '9') return true;
|
||||
return false;
|
||||
return (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9');
|
||||
}
|
||||
|
||||
const std::unordered_map<int, std::string> known_locales()
|
||||
|
@ -446,6 +444,11 @@ bool is_valid_locale(const std::string &locale_string)
|
|||
|
||||
section parse_section(const std::string §ion_string)
|
||||
{
|
||||
if (section_string.empty())
|
||||
{
|
||||
throw std::runtime_error("empty format");
|
||||
}
|
||||
|
||||
section s;
|
||||
|
||||
std::string format_part;
|
||||
|
@ -496,7 +499,7 @@ section parse_section(const std::string §ion_string)
|
|||
|
||||
if (is_valid_color(bracket_part))
|
||||
{
|
||||
if (s.color.empty())
|
||||
if (!s.has_color)
|
||||
{
|
||||
s.color = bracket_part;
|
||||
s.has_color = true;
|
||||
|
@ -508,7 +511,7 @@ section parse_section(const std::string §ion_string)
|
|||
}
|
||||
else if (is_valid_locale(bracket_part))
|
||||
{
|
||||
if (s.locale.empty())
|
||||
if (!s.has_locale)
|
||||
{
|
||||
s.locale = bracket_part;
|
||||
s.has_locale = true;
|
||||
|
@ -547,12 +550,6 @@ format_sections parse_format_sections(const std::string &combined)
|
|||
format_sections result = {};
|
||||
|
||||
auto split = split_string(combined, ';');
|
||||
|
||||
if (split.empty())
|
||||
{
|
||||
throw std::runtime_error("empty string");
|
||||
}
|
||||
|
||||
result.first = parse_section(split[0]);
|
||||
|
||||
if (!result.first.has_condition)
|
||||
|
@ -806,15 +803,20 @@ std::string format_section(long double number, const section &format, xlnt::cale
|
|||
}
|
||||
}
|
||||
|
||||
if (number < 0)
|
||||
{
|
||||
before_text = std::string("-") + before_text;
|
||||
}
|
||||
|
||||
result = before_text;
|
||||
|
||||
if (number == static_cast<long long int>(number))
|
||||
{
|
||||
result.append(std::to_string(static_cast<long long int>(number)));
|
||||
result.append(std::to_string(static_cast<long long int>(std::abs(number))));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto number_string = std::to_string(number);
|
||||
auto number_string = std::to_string(std::abs(number));
|
||||
|
||||
while (number_string.find('.') != std::string::npos && number_string.back() == '0' && number_string.find('.') < number_string.size() - 1)
|
||||
{
|
||||
|
|
|
@ -9,6 +9,19 @@
|
|||
class test_number_format : public CxxTest::TestSuite
|
||||
{
|
||||
public:
|
||||
void test_simple_format()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
|
||||
nf.set_format_string("\"positive\"General;\"negative\"General");
|
||||
auto formatted = nf.format(3.14, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "positive3.14");
|
||||
|
||||
nf.set_format_string("\"positive\"General;\"negative\"General");
|
||||
formatted = nf.format(-3.14, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "-negative3.14");
|
||||
}
|
||||
|
||||
void test_simple_date()
|
||||
{
|
||||
auto date = xlnt::date(2016, 6, 18);
|
||||
|
@ -45,9 +58,9 @@ public:
|
|||
{
|
||||
xlnt::number_format nf;
|
||||
|
||||
nf.set_format_string("[>5]\"first\"General;[>3]\"second\"General;\"third\"General");
|
||||
nf.set_format_string("[>5]General\"first\";[>3]\"second\"General;\"third\"General");
|
||||
auto formatted = nf.format(6, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "first6");
|
||||
TS_ASSERT_EQUALS(formatted, "6first");
|
||||
formatted = nf.format(4, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "second4");
|
||||
formatted = nf.format(5, xlnt::calendar::windows_1900);
|
||||
|
@ -81,7 +94,7 @@ public:
|
|||
|
||||
nf.set_format_string("[<=1]\"first\"General;[<=5]\"second\"General;\"third\"General");
|
||||
formatted = nf.format(-1000, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "first-1000");
|
||||
TS_ASSERT_EQUALS(formatted, "-first1000");
|
||||
formatted = nf.format(0, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "first0");
|
||||
formatted = nf.format(1, xlnt::calendar::windows_1900);
|
||||
|
@ -109,9 +122,68 @@ public:
|
|||
void test_locale_currency()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("[$€-407]#,##0.00");
|
||||
|
||||
|
||||
nf.set_format_string("[$€-407]-#,##0.00");
|
||||
auto formatted = nf.format(1.2, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "€1,20");
|
||||
TS_ASSERT_EQUALS(formatted, "-€1,20");
|
||||
|
||||
nf.set_format_string("[$$-1009]#,##0.00");
|
||||
formatted = nf.format(1.2, xlnt::calendar::windows_1900);
|
||||
TS_ASSERT_EQUALS(formatted, "CA$1,20");
|
||||
|
||||
}
|
||||
|
||||
void test_bad_country()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
|
||||
nf.set_format_string("[$-]#,##0.00");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[$-G]#,##0.00");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[$-4002]#,##0.00");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[-4001]#,##0.00");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
}
|
||||
|
||||
void test_duplicate_bracket_sections()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
|
||||
nf.set_format_string("[Red][Green]#,##0.00");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[$-403][$-4001]#,##0.00");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[>3][>4]#,##0.00");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
}
|
||||
|
||||
void test_bad_format()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
|
||||
nf.set_format_string("");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string(";;;");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[=1]\"first\"General;\"second\"General;\"third\"General");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[=1]\"first\"General;\"second\"General;[=3]\"third\"General");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("[=1]\"first\"General;[=2]\"second\"General;\"third\"General;\"fourth\"General");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
|
||||
nf.set_format_string("\"first\"General;\"second\"General;\"third\"General;\"fourth\"General;\"fifth\"General");
|
||||
TS_ASSERT_THROWS(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user