2017-09-27 00:25:20 +00:00
|
|
|
|
// © 2017 and later: Unicode, Inc. and others.
|
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
|
2017-10-04 22:51:06 +00:00
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
2018-04-23 23:02:26 +00:00
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2017-09-27 05:31:57 +00:00
|
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
|
#include "numbertest.h"
|
|
|
|
|
#include "number_patternstring.h"
|
|
|
|
|
|
2018-03-14 10:41:27 +00:00
|
|
|
|
void PatternStringTest::runIndexedTest(int32_t index, UBool exec, const char*& name, char*) {
|
2017-09-27 02:16:44 +00:00
|
|
|
|
if (exec) {
|
|
|
|
|
logln("TestSuite PatternStringTest: ");
|
|
|
|
|
}
|
|
|
|
|
TESTCASE_AUTO_BEGIN;
|
2018-03-14 10:41:27 +00:00
|
|
|
|
TESTCASE_AUTO(testLocalized);
|
2017-09-27 02:16:44 +00:00
|
|
|
|
TESTCASE_AUTO(testToPatternSimple);
|
|
|
|
|
TESTCASE_AUTO(testExceptionOnInvalid);
|
|
|
|
|
TESTCASE_AUTO(testBug13117);
|
|
|
|
|
TESTCASE_AUTO_END;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 10:41:27 +00:00
|
|
|
|
void PatternStringTest::testLocalized() {
|
|
|
|
|
IcuTestErrorCode status(*this, "testLocalized");
|
|
|
|
|
DecimalFormatSymbols symbols(Locale::getEnglish(), status);
|
2018-06-01 01:19:38 +00:00
|
|
|
|
if (status.isFailure()) { return; }
|
2018-03-14 10:41:27 +00:00
|
|
|
|
symbols.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, u"a", status);
|
|
|
|
|
symbols.setSymbol(DecimalFormatSymbols::kPercentSymbol, u"b", status);
|
|
|
|
|
symbols.setSymbol(DecimalFormatSymbols::kMinusSignSymbol, u".", status);
|
|
|
|
|
symbols.setSymbol(DecimalFormatSymbols::kPlusSignSymbol, u"'", status);
|
|
|
|
|
|
|
|
|
|
UnicodeString standard = u"+-abcb''a''#,##0.0%'a%'";
|
|
|
|
|
UnicodeString localized = u"’.'ab'c'b''a'''#,##0a0b'a%'";
|
|
|
|
|
UnicodeString toStandard = u"+-'ab'c'b''a'''#,##0.0%'a%'";
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
"standard to localized",
|
|
|
|
|
localized,
|
|
|
|
|
PatternStringUtils::convertLocalized(standard, symbols, true, status));
|
|
|
|
|
assertEquals(
|
|
|
|
|
"localized to standard",
|
|
|
|
|
toStandard,
|
|
|
|
|
PatternStringUtils::convertLocalized(localized, symbols, false, status));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
|
void PatternStringTest::testToPatternSimple() {
|
2018-03-14 10:41:27 +00:00
|
|
|
|
const char16_t* cases[][2] = {{u"#", u"0"},
|
2017-09-27 00:25:20 +00:00
|
|
|
|
{u"0", u"0"},
|
|
|
|
|
{u"#0", u"0"},
|
|
|
|
|
{u"###", u"0"},
|
|
|
|
|
{u"0.##", u"0.##"},
|
|
|
|
|
{u"0.00", u"0.00"},
|
|
|
|
|
{u"0.00#", u"0.00#"},
|
2018-03-17 07:23:08 +00:00
|
|
|
|
{u"0.05", u"0.05"},
|
2017-09-27 00:25:20 +00:00
|
|
|
|
{u"#E0", u"#E0"},
|
|
|
|
|
{u"0E0", u"0E0"},
|
|
|
|
|
{u"#00E00", u"#00E00"},
|
|
|
|
|
{u"#,##0", u"#,##0"},
|
|
|
|
|
{u"#;#", u"0;0"},
|
|
|
|
|
// ignore a negative prefix pattern of '-' since that is the default:
|
|
|
|
|
{u"#;-#", u"0"},
|
2018-03-17 07:23:08 +00:00
|
|
|
|
{u"pp#,000;(#)", u"pp#,000;(#,000)"},
|
2017-09-27 00:25:20 +00:00
|
|
|
|
{u"**##0", u"**##0"},
|
|
|
|
|
{u"*'x'##0", u"*x##0"},
|
|
|
|
|
{u"a''b0", u"a''b0"},
|
|
|
|
|
{u"*''##0", u"*''##0"},
|
|
|
|
|
{u"*📺##0", u"*'📺'##0"},
|
|
|
|
|
{u"*'நி'##0", u"*'நி'##0"},};
|
|
|
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2018-03-14 10:41:27 +00:00
|
|
|
|
for (const char16_t** cas : cases) {
|
2017-09-27 00:25:20 +00:00
|
|
|
|
UnicodeString input(cas[0]);
|
|
|
|
|
UnicodeString output(cas[1]);
|
|
|
|
|
|
|
|
|
|
DecimalFormatProperties properties = PatternParser::parseToProperties(
|
2018-03-14 10:41:27 +00:00
|
|
|
|
input, IGNORE_ROUNDING_NEVER, status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
assertSuccess(input, status);
|
|
|
|
|
UnicodeString actual = PatternStringUtils::propertiesToPatternString(properties, status);
|
|
|
|
|
assertEquals(input, output, actual);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
|
void PatternStringTest::testExceptionOnInvalid() {
|
2018-03-14 10:41:27 +00:00
|
|
|
|
static const char16_t* invalidPatterns[] = {
|
2017-09-27 00:25:20 +00:00
|
|
|
|
u"#.#.#",
|
|
|
|
|
u"0#",
|
|
|
|
|
u"0#.",
|
|
|
|
|
u".#0",
|
|
|
|
|
u"0#.#0",
|
|
|
|
|
u"@0",
|
|
|
|
|
u"0@",
|
|
|
|
|
u"0,",
|
|
|
|
|
u"0,,",
|
|
|
|
|
u"0,,0",
|
|
|
|
|
u"0,,0,",
|
|
|
|
|
u"#,##0E0"};
|
|
|
|
|
|
|
|
|
|
for (auto pattern : invalidPatterns) {
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
ParsedPatternInfo patternInfo;
|
|
|
|
|
PatternParser::parseToPatternInfo(pattern, patternInfo, status);
|
|
|
|
|
assertTrue(pattern, U_FAILURE(status));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
|
void PatternStringTest::testBug13117() {
|
2017-09-27 00:25:20 +00:00
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
DecimalFormatProperties expected = PatternParser::parseToProperties(
|
2018-03-14 10:41:27 +00:00
|
|
|
|
u"0", IGNORE_ROUNDING_NEVER, status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
DecimalFormatProperties actual = PatternParser::parseToProperties(
|
2018-03-14 10:41:27 +00:00
|
|
|
|
u"0;", IGNORE_ROUNDING_NEVER, status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
assertSuccess("Spot 1", status);
|
|
|
|
|
assertTrue("Should not consume negative subpattern", expected == actual);
|
|
|
|
|
}
|
2017-09-27 05:31:57 +00:00
|
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|