OLD | NEW |
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.AffixPatternUtils; | 10 import com.ibm.icu.impl.number.AffixPatternUtils; |
| 11 import com.ibm.icu.impl.number.AffixPatternUtils.SymbolProvider; |
11 import com.ibm.icu.impl.number.NumberStringBuilder; | 12 import com.ibm.icu.impl.number.NumberStringBuilder; |
12 import com.ibm.icu.text.DecimalFormatSymbols; | 13 import com.ibm.icu.text.DecimalFormatSymbols; |
13 import com.ibm.icu.util.ULocale; | 14 import com.ibm.icu.util.ULocale; |
14 | 15 |
15 public class AffixPatternUtilsTest { | 16 public class AffixPatternUtilsTest { |
16 | 17 |
17 @Test | 18 @Test |
18 public void testEscape() { | 19 public void testEscape() { |
19 Object[][] cases = { | 20 Object[][] cases = { |
20 {"", ""}, | 21 {"", ""}, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 {"'--''-'-x", false, 6, "--'-−x"}, | 57 {"'--''-'-x", false, 6, "--'-−x"}, |
57 {"''", false, 1, "'"}, | 58 {"''", false, 1, "'"}, |
58 {"''''", false, 2, "''"}, | 59 {"''''", false, 2, "''"}, |
59 {"''''''", false, 3, "'''"}, | 60 {"''''''", false, 3, "'''"}, |
60 {"''x''", false, 3, "'x'"}, | 61 {"''x''", false, 3, "'x'"}, |
61 {"¤", true, 1, "$"}, | 62 {"¤", true, 1, "$"}, |
62 {"¤¤", true, 2, "XXX"}, | 63 {"¤¤", true, 2, "XXX"}, |
63 {"¤¤¤", true, 3, "long name"}, | 64 {"¤¤¤", true, 3, "long name"}, |
64 {"¤¤¤¤", true, 4, "\uFFFD"}, | 65 {"¤¤¤¤", true, 4, "\uFFFD"}, |
65 {"¤¤¤¤¤", true, 5, "\uFFFD"}, | 66 {"¤¤¤¤¤", true, 5, "\uFFFD"}, |
| 67 {"¤¤¤¤¤¤", true, 6, "\uFFFD"}, |
66 {"¤¤¤a¤¤¤¤", true, 8, "long namea\uFFFD"}, | 68 {"¤¤¤a¤¤¤¤", true, 8, "long namea\uFFFD"}, |
67 {"a¤¤¤¤b¤¤¤¤¤c", true, 12, "a\uFFFDb\uFFFDc"}, | 69 {"a¤¤¤¤b¤¤¤¤¤c", true, 12, "a\uFFFDb\uFFFDc"}, |
68 {"¤!", true, 2, "$!"}, | 70 {"¤!", true, 2, "$!"}, |
69 {"¤¤!", true, 3, "XXX!"}, | 71 {"¤¤!", true, 3, "XXX!"}, |
70 {"¤¤¤!", true, 4, "long name!"}, | 72 {"¤¤¤!", true, 4, "long name!"}, |
71 {"-¤¤", true, 3, "−XXX"}, | 73 {"-¤¤", true, 3, "−XXX"}, |
72 {"¤¤-", true, 3, "XXX−"}, | 74 {"¤¤-", true, 3, "XXX−"}, |
73 {"'¤'", false, 1, "¤"}, | 75 {"'¤'", false, 1, "¤"}, |
74 {"%", false, 1, "٪\u061C"}, | 76 {"%", false, 1, "٪\u061C"}, |
75 {"'%'", false, 1, "%"}, | 77 {"'%'", false, 1, "%"}, |
76 {"¤'-'%", true, 3, "$-٪\u061C"} | 78 {"¤'-'%", true, 3, "$-٪\u061C"}, |
| 79 {"#0#@#*#;#", false, 9, "#0#@#*#;#"} |
77 }; | 80 }; |
78 | 81 |
79 // ar_SA has an interesting percent sign and various Arabic letter marks | 82 // ar_SA has an interesting percent sign and various Arabic letter marks |
80 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(new ULocale(
"ar_SA")); | 83 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(new ULocale(
"ar_SA")); |
81 NumberStringBuilder sb = new NumberStringBuilder(); | 84 NumberStringBuilder sb = new NumberStringBuilder(); |
82 | 85 |
83 for (Object[] cas : cases) { | 86 for (Object[] cas : cases) { |
84 String input = (String) cas[0]; | 87 String input = (String) cas[0]; |
85 boolean curr = (Boolean) cas[1]; | 88 boolean curr = (Boolean) cas[1]; |
86 int length = (Integer) cas[2]; | 89 int length = (Integer) cas[2]; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // OK | 147 // OK |
145 } | 148 } |
146 try { | 149 try { |
147 AffixPatternUtils.unescape(str, symbols, "$", "XXX", "long name", "−", s
b); | 150 AffixPatternUtils.unescape(str, symbols, "$", "XXX", "long name", "−", s
b); |
148 fail("No exception was thrown on an invalid string"); | 151 fail("No exception was thrown on an invalid string"); |
149 } catch (IllegalArgumentException e) { | 152 } catch (IllegalArgumentException e) { |
150 // OK | 153 // OK |
151 } | 154 } |
152 } | 155 } |
153 } | 156 } |
| 157 |
| 158 @Test |
| 159 public void testUnescapeWithSymbolProvider() { |
| 160 String[][] cases = { |
| 161 {"", ""}, |
| 162 {"-", "1"}, |
| 163 {"'-'", "-"}, |
| 164 {"- + % ‰ ¤ ¤¤ ¤¤¤ ¤¤¤¤ ¤¤¤¤¤", "1 2 3 4 5 6 7 8 9"}, |
| 165 {"'¤¤¤¤¤¤'", "¤¤¤¤¤¤"}, |
| 166 {"¤¤¤¤¤¤", "\uFFFD"} |
| 167 }; |
| 168 |
| 169 SymbolProvider provider = |
| 170 new SymbolProvider() { |
| 171 @Override |
| 172 public CharSequence getSymbol(int type) { |
| 173 return Integer.toString(Math.abs(type)); |
| 174 } |
| 175 }; |
| 176 |
| 177 NumberStringBuilder sb = new NumberStringBuilder(); |
| 178 for (String[] cas : cases) { |
| 179 String input = cas[0]; |
| 180 String expected = cas[1]; |
| 181 sb.clear(); |
| 182 AffixPatternUtils.unescape(input, sb, 0, provider); |
| 183 assertEquals("With symbol provider on <" + input + ">", expected, sb.toStr
ing()); |
| 184 } |
| 185 |
| 186 // Test insertion position |
| 187 sb.clear(); |
| 188 sb.append("abcdefg", null); |
| 189 AffixPatternUtils.unescape("-+%", sb, 4, provider); |
| 190 assertEquals("Symbol provider into middle", "abcd123efg", sb.toString()); |
| 191 } |
154 } | 192 } |
OLD | NEW |