mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
improve number_format test coverage
This commit is contained in:
parent
aa0dee3191
commit
bdc770d23a
|
@ -44,7 +44,6 @@ public:
|
|||
static const number_format number();
|
||||
static const number_format number_00();
|
||||
static const number_format number_comma_separated1();
|
||||
static const number_format number_comma_separated2();
|
||||
static const number_format percentage();
|
||||
static const number_format percentage_00();
|
||||
static const number_format date_yyyymmdd2();
|
||||
|
|
|
@ -615,7 +615,7 @@ std::string format_section(long double number, const section &format, xlnt::cale
|
|||
auto value = format.value;
|
||||
bool twelve_hour = false;
|
||||
|
||||
if (lower_string(value).substr(format.value.size() - 5) == "am/pm")
|
||||
if (format.value.size() > 5 && lower_string(value).substr(format.value.size() - 5) == "am/pm")
|
||||
{
|
||||
twelve_hour = true;
|
||||
}
|
||||
|
@ -733,7 +733,6 @@ std::string format_section(long double number, const section &format, xlnt::cale
|
|||
result.append(std::to_string(d.hour));
|
||||
}
|
||||
|
||||
result.append(std::to_string(d.hour));
|
||||
processed_month = true;
|
||||
}
|
||||
else if (part == "m")
|
||||
|
@ -828,27 +827,28 @@ std::string format_section(long double number, const section &format, xlnt::cale
|
|||
|
||||
result.append(end_text);
|
||||
}
|
||||
else if (format.value.substr(0, 8) == "#,##0.00" || format.value.substr(0, 9) == "-#,##0.00")
|
||||
else if (format.value.find("#,##0.00") != std::string::npos)
|
||||
{
|
||||
if (format.value[0] == '-')
|
||||
auto index = format.value.find("#,##0.00");
|
||||
auto before_text = format.value.substr(0, index);
|
||||
auto after_text = format.value.substr(index + 8);
|
||||
bool minus_first = before_text.substr(0, 1) == "-";
|
||||
|
||||
if (minus_first)
|
||||
{
|
||||
result = "-";
|
||||
before_text = before_text.substr(1);
|
||||
}
|
||||
|
||||
if (format.has_locale && format.locale == "$$-1009")
|
||||
{
|
||||
result += "CA$";
|
||||
before_text = std::string(minus_first ? "-" : "") + "CA$" + before_text;
|
||||
}
|
||||
else if (format.has_locale && format.locale == "$€-407")
|
||||
{
|
||||
result += "€";
|
||||
}
|
||||
else
|
||||
{
|
||||
result += "$";
|
||||
before_text = std::string(minus_first ? "-" : "") + "€" + before_text;
|
||||
}
|
||||
|
||||
result += std::to_string(number < 0 ? -number : number);
|
||||
result = before_text + std::to_string(number < 0 ? -number : number) + after_text;
|
||||
|
||||
auto decimal_pos = result.find('.');
|
||||
|
||||
|
@ -948,7 +948,7 @@ std::string format_number(long double number, const std::string &format, xlnt::c
|
|||
|
||||
std::string format_text(const std::string &text, const std::string &format)
|
||||
{
|
||||
if (format == "General") return text;
|
||||
if (format == "General" || format == "@") return text;
|
||||
auto sections = parse_format_sections(format);
|
||||
return format_section(text, sections.fourth);
|
||||
}
|
||||
|
@ -987,12 +987,6 @@ const number_format number_format::number_comma_separated1()
|
|||
return *format;
|
||||
}
|
||||
|
||||
const number_format number_format::number_comma_separated2()
|
||||
{
|
||||
static const number_format *format = new number_format("#,##0.00_-");
|
||||
return *format;
|
||||
}
|
||||
|
||||
const number_format number_format::percentage()
|
||||
{
|
||||
static const number_format *format = new number_format(builtin_formats().at(9), 9);
|
||||
|
|
|
@ -33,7 +33,101 @@ public:
|
|||
TS_ASSERT_EQUALS(formatted, "18/06/16");
|
||||
}
|
||||
|
||||
void test_simple_time()
|
||||
void test_short_month()
|
||||
{
|
||||
auto date = xlnt::date(2016, 6, 18);
|
||||
auto date_number = date.to_number(xlnt::calendar::windows_1900);
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("m");
|
||||
auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "6");
|
||||
}
|
||||
|
||||
void test_month_abbreviation()
|
||||
{
|
||||
auto date = xlnt::date(2016, 6, 18);
|
||||
auto date_number = date.to_number(xlnt::calendar::windows_1900);
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("mmm");
|
||||
auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "Jun");
|
||||
}
|
||||
|
||||
void test_month_name()
|
||||
{
|
||||
auto date = xlnt::date(2016, 6, 18);
|
||||
auto date_number = date.to_number(xlnt::calendar::windows_1900);
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("mmmm");
|
||||
auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "June");
|
||||
}
|
||||
|
||||
void test_short_day()
|
||||
{
|
||||
auto date = xlnt::date(2016, 6, 8);
|
||||
auto date_number = date.to_number(xlnt::calendar::windows_1900);
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("d");
|
||||
auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "8");
|
||||
}
|
||||
|
||||
void test_long_day()
|
||||
{
|
||||
auto date = xlnt::date(2016, 6, 8);
|
||||
auto date_number = date.to_number(xlnt::calendar::windows_1900);
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("dd");
|
||||
auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "08");
|
||||
}
|
||||
|
||||
void test_long_year()
|
||||
{
|
||||
auto date = xlnt::date(2016, 6, 18);
|
||||
auto date_number = date.to_number(xlnt::calendar::windows_1900);
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("yyyy");
|
||||
auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "2016");
|
||||
}
|
||||
|
||||
void test_time_24_hour()
|
||||
{
|
||||
auto time = xlnt::time(20, 15, 10);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf = xlnt::number_format::date_time4();
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "20:15:10");
|
||||
}
|
||||
|
||||
void test_time_12_hour_am()
|
||||
{
|
||||
auto time = xlnt::time(8, 15, 10);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf = xlnt::number_format::date_time2();
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "8:15:10 AM");
|
||||
}
|
||||
|
||||
void test_time_12_hour_pm()
|
||||
{
|
||||
auto time = xlnt::time(20, 15, 10);
|
||||
auto time_number = time.to_number();
|
||||
|
@ -44,6 +138,90 @@ public:
|
|||
TS_ASSERT_EQUALS(formatted, "8:15:10 PM");
|
||||
}
|
||||
|
||||
void test_long_hour_12_hour()
|
||||
{
|
||||
auto time = xlnt::time(20, 15, 10);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("hh AM/PM");
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "08 PM");
|
||||
}
|
||||
|
||||
void test_long_hour_24_hour()
|
||||
{
|
||||
auto time = xlnt::time(20, 15, 10);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("hh");
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "20");
|
||||
}
|
||||
|
||||
void test_short_minute()
|
||||
{
|
||||
auto time = xlnt::time(20, 5, 10);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("h:m");
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "20:5");
|
||||
}
|
||||
|
||||
void test_long_minute()
|
||||
{
|
||||
auto time = xlnt::time(20, 5, 10);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("h:mm");
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "20:05");
|
||||
}
|
||||
|
||||
void test_short_second()
|
||||
{
|
||||
auto time = xlnt::time(20, 15, 1);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("h:m:s");
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "20:15:1");
|
||||
}
|
||||
|
||||
void test_long_second()
|
||||
{
|
||||
auto time = xlnt::time(20, 15, 1);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("h:m:ss");
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "20:15:01");
|
||||
}
|
||||
|
||||
void test_trailing_space()
|
||||
{
|
||||
auto time = xlnt::time(20, 15, 1);
|
||||
auto time_number = time.to_number();
|
||||
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("h:m:ss ");
|
||||
auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "20:15:01 ");
|
||||
}
|
||||
|
||||
void test_text_section_string()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
|
@ -54,6 +232,16 @@ public:
|
|||
TS_ASSERT_EQUALS(formatted, "atextb");
|
||||
}
|
||||
|
||||
void test_text_section_no_string()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
nf.set_format_string("General;General;General;[Green]\"ab\"");
|
||||
|
||||
auto formatted = nf.format("text");
|
||||
|
||||
TS_ASSERT_EQUALS(formatted, "ab");
|
||||
}
|
||||
|
||||
void test_conditional_format()
|
||||
{
|
||||
xlnt::number_format nf;
|
||||
|
@ -177,7 +365,7 @@ public:
|
|||
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");
|
||||
nf.set_format_string("[=1]\"first\"General;[=2]\"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");
|
||||
|
@ -186,4 +374,46 @@ public:
|
|||
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);
|
||||
}
|
||||
|
||||
void test_builtin_formats()
|
||||
{
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::text().format("a"), "a");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::number().format(1, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::number_comma_separated1().format(1, xlnt::calendar::windows_1900), "1,00");
|
||||
|
||||
/*
|
||||
auto datetime = xlnt::datetime(2016, 6, 24, 0, 45, 58);
|
||||
auto datetime_number = datetime.to_number(xlnt::calendar::windows_1900);
|
||||
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::percentage().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::percentage_00().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_yyyymmdd2().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_yyyymmdd().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_ddmmyyyy().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_dmyslash().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_dmyminus().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_dmminus().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_myminus().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_xlsx14().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_xlsx15().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_xlsx16().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_xlsx17().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_xlsx22().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_datetime().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time1().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time2().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time3().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time4().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time5().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time6().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time7().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_time8().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_timedelta().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::date_yyyymmddslash().format(datetime_number, xlnt::calendar::windows_1900), "1");
|
||||
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::currency_usd_simple().format(1.23, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::currency_usd().format(1.23, xlnt::calendar::windows_1900), "1");
|
||||
TS_ASSERT_EQUALS(xlnt::number_format::currency_eur_simple().format(1.23, xlnt::calendar::windows_1900), "1");
|
||||
*/
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user