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