• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2017 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 package ohos.global.icu.dev.test.number;
5 
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.fail;
8 
9 import org.junit.Test;
10 
11 import ohos.global.icu.impl.number.DecimalFormatProperties;
12 import ohos.global.icu.impl.number.PatternStringParser;
13 import ohos.global.icu.impl.number.PatternStringUtils;
14 import ohos.global.icu.text.DecimalFormatSymbols;
15 import ohos.global.icu.util.ULocale;
16 
17 
18 /** @author sffc */
19 
20 public class PatternStringTest {
21 
22     @Test
testLocalized()23     public void testLocalized() {
24         DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(ULocale.ENGLISH);
25         symbols.setDecimalSeparatorString("a");
26         symbols.setPercentString("b");
27         symbols.setMinusSignString(".");
28         symbols.setPlusSignString("'");
29 
30         String standard = "+-abcb''a''#,##0.0%'a%'";
31         String localized = "’.'ab'c'b''a'''#,##0a0b'a%'";
32         String toStandard = "+-'ab'c'b''a'''#,##0.0%'a%'";
33 
34         assertEquals(localized, PatternStringUtils.convertLocalized(standard, symbols, true));
35         assertEquals(toStandard, PatternStringUtils.convertLocalized(localized, symbols, false));
36     }
37 
38     @Test
testToPatternSimple()39     public void testToPatternSimple() {
40         String[][] cases = {
41                 { "#", "0" },
42                 { "0", "0" },
43                 { "#0", "0" },
44                 { "###", "0" },
45                 { "0.##", "0.##" },
46                 { "0.00", "0.00" },
47                 { "0.00#", "0.00#" },
48                 { "0.05", "0.05" },
49                 { "#E0", "#E0" },
50                 { "0E0", "0E0" },
51                 { "#00E00", "#00E00" },
52                 { "#,##0", "#,##0" },
53                 { "#;#", "0;0" },
54                 { "#;-#", "0" }, // ignore a negative prefix pattern of '-' since that is the default
55                 { "pp#,000;(#)", "pp#,000;(#,000)" },
56                 { "**##0", "**##0" },
57                 { "*'x'##0", "*x##0" },
58                 { "a''b0", "a''b0" },
59                 { "*''##0", "*''##0" },
60                 { "*��##0", "*'��'##0" },
61                 { "*'நி'##0", "*'நி'##0" }, };
62 
63         for (String[] cas : cases) {
64             String input = cas[0];
65             String output = cas[1];
66 
67             DecimalFormatProperties properties = PatternStringParser.parseToProperties(input);
68             String actual = PatternStringUtils.propertiesToPatternString(properties);
69             assertEquals("Failed on input pattern '" + input + "', properties " + properties,
70                     output,
71                     actual);
72         }
73     }
74 
75     @Test
testToPatternWithProperties()76     public void testToPatternWithProperties() {
77         Object[][] cases = {
78                 { new DecimalFormatProperties().setPositivePrefix("abc"), "abc#;-#" },
79                 { new DecimalFormatProperties().setPositiveSuffix("abc"), "#abc;-#" },
80                 { new DecimalFormatProperties().setPositivePrefixPattern("abc"), "abc#" },
81                 { new DecimalFormatProperties().setPositiveSuffixPattern("abc"), "#abc" },
82                 { new DecimalFormatProperties().setNegativePrefix("abc"), "#;abc#" },
83                 { new DecimalFormatProperties().setNegativeSuffix("abc"), "#;-#abc" },
84                 { new DecimalFormatProperties().setNegativePrefixPattern("abc"), "#;abc#" },
85                 { new DecimalFormatProperties().setNegativeSuffixPattern("abc"), "#;-#abc" },
86                 { new DecimalFormatProperties().setPositivePrefix("+"), "'+'#;-#" },
87                 { new DecimalFormatProperties().setPositivePrefixPattern("+"), "+#" },
88                 { new DecimalFormatProperties().setPositivePrefix("+'"), "'+'''#;-#" },
89                 { new DecimalFormatProperties().setPositivePrefix("'+"), "'''+'#;-#" },
90                 { new DecimalFormatProperties().setPositivePrefix("'"), "''#;-#" },
91                 { new DecimalFormatProperties().setPositivePrefixPattern("+''"), "+''#" }, };
92 
93         for (Object[] cas : cases) {
94             DecimalFormatProperties input = (DecimalFormatProperties) cas[0];
95             String output = (String) cas[1];
96 
97             String actual = PatternStringUtils.propertiesToPatternString(input);
98             assertEquals("Failed on input properties " + input, output, actual);
99         }
100     }
101 
102     @Test
testExceptionOnInvalid()103     public void testExceptionOnInvalid() {
104         String[] invalidPatterns = {
105                 "#.#.#",
106                 "0#",
107                 "0#.",
108                 ".#0",
109                 "0#.#0",
110                 "@0",
111                 "0@",
112                 "0,",
113                 "0,,",
114                 "0,,0",
115                 "0,,0,",
116                 "#,##0E0" };
117 
118         for (String pattern : invalidPatterns) {
119             try {
120                 PatternStringParser.parseToProperties(pattern);
121                 fail("Didn't throw IllegalArgumentException when parsing pattern: " + pattern);
122             } catch (IllegalArgumentException e) {
123             }
124         }
125     }
126 
127     @Test
testBug13117()128     public void testBug13117() {
129         DecimalFormatProperties expected = PatternStringParser.parseToProperties("0");
130         DecimalFormatProperties actual = PatternStringParser.parseToProperties("0;");
131         assertEquals("Should not consume negative subpattern", expected, actual);
132     }
133 }
134