1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
8 #include "numbertest.h"
9 #include "number_patternstring.h"
11 void PatternStringTest::runIndexedTest(int32_t index
, UBool exec
, const char*& name
, char*) {
13 logln("TestSuite PatternStringTest: ");
16 TESTCASE_AUTO(testLocalized
);
17 TESTCASE_AUTO(testToPatternSimple
);
18 TESTCASE_AUTO(testExceptionOnInvalid
);
19 TESTCASE_AUTO(testBug13117
);
23 void PatternStringTest::testLocalized() {
24 IcuTestErrorCode
status(*this, "testLocalized");
25 DecimalFormatSymbols
symbols(Locale::getEnglish(), status
);
26 if (status
.isFailure()) { return; }
27 symbols
.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
, u
"a", status
);
28 symbols
.setSymbol(DecimalFormatSymbols::kPercentSymbol
, u
"b", status
);
29 symbols
.setSymbol(DecimalFormatSymbols::kMinusSignSymbol
, u
".", status
);
30 symbols
.setSymbol(DecimalFormatSymbols::kPlusSignSymbol
, u
"'", status
);
32 UnicodeString standard
= u
"+-abcb''a''#,##0.0%'a%'";
33 UnicodeString localized
= u
"’.'ab'c'b''a'''#,##0a0b'a%'";
34 UnicodeString toStandard
= u
"+-'ab'c'b''a'''#,##0.0%'a%'";
37 "standard to localized",
39 PatternStringUtils::convertLocalized(standard
, symbols
, true, status
));
41 "localized to standard",
43 PatternStringUtils::convertLocalized(localized
, symbols
, false, status
));
46 void PatternStringTest::testToPatternSimple() {
47 const char16_t* cases
[][2] = {{u
"#", u
"0"},
57 {u
"#00E00", u
"#00E00"},
60 // ignore a negative prefix pattern of '-' since that is the default:
62 {u
"pp#,000;(#)", u
"pp#,000;(#,000)"},
64 {u
"*'x'##0", u
"*x##0"},
66 {u
"*''##0", u
"*''##0"},
67 {u
"*📺##0", u
"*'📺'##0"},
68 {u
"*'நி'##0", u
"*'நி'##0"},};
70 UErrorCode status
= U_ZERO_ERROR
;
71 for (const char16_t** cas
: cases
) {
72 UnicodeString
input(cas
[0]);
73 UnicodeString
output(cas
[1]);
75 DecimalFormatProperties properties
= PatternParser::parseToProperties(
76 input
, IGNORE_ROUNDING_NEVER
, status
);
77 assertSuccess(input
, status
);
78 UnicodeString actual
= PatternStringUtils::propertiesToPatternString(properties
, status
);
79 assertEquals(input
, output
, actual
);
83 void PatternStringTest::testExceptionOnInvalid() {
84 static const char16_t* invalidPatterns
[] = {
98 for (auto pattern
: invalidPatterns
) {
99 UErrorCode status
= U_ZERO_ERROR
;
100 ParsedPatternInfo patternInfo
;
101 PatternParser::parseToPatternInfo(pattern
, patternInfo
, status
);
102 assertTrue(pattern
, U_FAILURE(status
));
106 void PatternStringTest::testBug13117() {
107 UErrorCode status
= U_ZERO_ERROR
;
108 DecimalFormatProperties expected
= PatternParser::parseToProperties(
109 u
"0", IGNORE_ROUNDING_NEVER
, status
);
110 DecimalFormatProperties actual
= PatternParser::parseToProperties(
111 u
"0;", IGNORE_ROUNDING_NEVER
, status
);
112 assertSuccess("Spot 1", status
);
113 assertTrue("Should not consume negative subpattern", expected
== actual
);
116 #endif /* #if !UCONFIG_NO_FORMATTING */