2 *******************************************************************************
\r
3 * Copyright (C) 2007-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.dev.test.format;
\r
10 import java.text.ParsePosition;
\r
12 import com.ibm.icu.dev.test.TestFmwk;
\r
13 import com.ibm.icu.text.MessageFormat;
\r
14 import com.ibm.icu.text.NumberFormat;
\r
15 import com.ibm.icu.text.PluralFormat;
\r
16 import com.ibm.icu.text.PluralRules;
\r
17 import com.ibm.icu.util.ULocale;
\r
20 * @author tschumann (Tim Schumann)
\r
23 public class PluralFormatUnitTest extends TestFmwk {
\r
24 public static void main(String[] args) throws Exception {
\r
25 new PluralFormatUnitTest().run(args);
\r
28 public void TestConstructor() {
\r
29 // Test correct formatting of numbers.
\r
30 PluralFormat plFmts[] = new PluralFormat[8];
\r
31 plFmts[0] = new PluralFormat();
\r
32 plFmts[0].applyPattern("other{#}");
\r
33 plFmts[1] = new PluralFormat(PluralRules.DEFAULT);
\r
34 plFmts[1].applyPattern("other{#}");
\r
35 plFmts[2] = new PluralFormat(PluralRules.DEFAULT, "other{#}");
\r
36 plFmts[3] = new PluralFormat("other{#}");
\r
37 plFmts[4] = new PluralFormat(ULocale.getDefault());
\r
38 plFmts[4].applyPattern("other{#}");
\r
39 plFmts[5] = new PluralFormat(ULocale.getDefault(), PluralRules.DEFAULT);
\r
40 plFmts[5].applyPattern("other{#}");
\r
41 plFmts[6] = new PluralFormat(ULocale.getDefault(),
\r
42 PluralRules.DEFAULT,
\r
44 plFmts[7] = new PluralFormat(ULocale.getDefault(), "other{#}");
\r
46 // These plural formats should produce the same output as a
\r
47 // NumberFormat for the default locale.
\r
48 NumberFormat numberFmt = NumberFormat.getInstance(ULocale.getDefault());
\r
49 for (int n = 1; n < 13; n++) {
\r
50 String result = numberFmt.format(n);
\r
51 for (int k = 0; k < plFmts.length; ++k) {
\r
52 this.assertEquals("PluralFormat's output is not as expected",
\r
53 result, plFmts[k].format(n));
\r
56 // Test some bigger numbers.
\r
57 for (int n = 100; n < 113; n++) {
\r
58 String result = numberFmt.format(n*n);
\r
59 for (int k = 0; k < plFmts.length; ++k) {
\r
60 this.assertEquals("PluralFormat's output is not as expected",
\r
61 result, plFmts[k].format(n*n));
\r
66 public void TestApplyPatternAndFormat() {
\r
67 // Create rules for testing.
\r
68 PluralRules oddAndEven = PluralRules.createRules("odd: n mod 2 is 1");
\r
70 // Test full specified case for testing RuleSet
\r
71 PluralFormat plfOddAndEven = new PluralFormat(oddAndEven);
\r
72 plfOddAndEven.applyPattern("odd{# is odd.} other{# is even.}");
\r
74 // Test fall back to other.
\r
75 PluralFormat plfOddOrEven = new PluralFormat(oddAndEven);
\r
76 plfOddOrEven.applyPattern("other{# is odd or even.}");
\r
78 NumberFormat numberFormat =
\r
79 NumberFormat.getInstance(ULocale.getDefault());
\r
80 for (int i = 0; i < 22; ++i) {
\r
81 assertEquals("Fallback to other gave wrong results",
\r
82 numberFormat.format(i) + " is odd or even.",
\r
83 plfOddOrEven.format(i));
\r
84 assertEquals("Fully specified PluralFormat gave wrong results",
\r
85 numberFormat.format(i) + ((i%2 == 1) ? " is odd."
\r
87 plfOddAndEven.format(i));
\r
90 // Check that double definition results in an exception.
\r
92 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
93 plFmt.applyPattern("odd{foo} odd{bar} other{foobar}");
\r
94 errln("Double definition of a plural case message should " +
\r
95 "provoke an exception but did not.");
\r
96 }catch (IllegalArgumentException e){}
\r
98 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
99 plFmt.applyPattern("odd{foo} other{bar} other{foobar}");
\r
100 errln("Double definition of a plural case message should " +
\r
101 "provoke an exception but did not.");
\r
102 }catch (IllegalArgumentException e){}
\r
104 // omit other keyword.
\r
106 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
107 plFmt.applyPattern("odd{foo}");
\r
108 errln("Not defining plural case other should result in an " +
\r
109 "exception but did not.");
\r
110 }catch (IllegalArgumentException e){}
\r
112 // Test unknown keyword.
\r
114 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
115 plFmt.applyPattern("otto{foo} other{bar}");
\r
116 errln("Defining a message for an unknown keyword should result in" +
\r
117 "an exception but did not.");
\r
118 }catch (IllegalArgumentException e){}
\r
120 // Test invalid keyword.
\r
122 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
123 plFmt.applyPattern("1odd{foo} other{bar}");
\r
124 errln("Defining a message for an invalid keyword should result in" +
\r
125 "an exception but did not.");
\r
126 }catch (IllegalArgumentException e){}
\r
128 // Test invalid syntax
\r
129 // -- comma between keyword{message} clauses
\r
130 // -- space in keywords
\r
131 // -- keyword{message1}{message2}
\r
133 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
134 plFmt.applyPattern("odd{foo},other{bar}");
\r
135 errln("Separating keyword{message} items with other characters " +
\r
136 "than space should provoke an exception but did not.");
\r
137 }catch (IllegalArgumentException e){}
\r
139 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
140 plFmt.applyPattern("od d{foo} other{bar}");
\r
141 errln("Spaces inside keywords should provoke an exception but " +
\r
143 }catch (IllegalArgumentException e){}
\r
145 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
146 plFmt.applyPattern("odd{foo}{foobar}other{foo}");
\r
147 errln("Defining multiple messages after a keyword should provoke " +
\r
148 "an exception but did not.");
\r
149 }catch (IllegalArgumentException e){}
\r
151 // Check that nested format is preserved.
\r
153 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
154 plFmt.applyPattern("odd{The number {0, number, #.#0} is odd.}" +
\r
155 "other{The number {0, number, #.#0} is even.}");
\r
156 for (int i = 1; i < 3; ++i) {
\r
157 assertEquals("format did not preserve a nested format string.",
\r
159 "The number {0, number, #.#0} is odd."
\r
160 : "The number {0, number, #.#0} is even."),
\r
165 // Check that a pound sign in curly braces is preserved.
\r
167 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
168 plFmt.applyPattern("odd{The number {#} is odd.}" +
\r
169 "other{The number {#} is even.}");
\r
170 for (int i = 1; i < 3; ++i) {
\r
171 assertEquals("format did not preserve # inside curly braces.",
\r
172 ((i % 2 == 1) ? "The number {#} is odd."
\r
173 : "The number {#} is even."),
\r
180 public void TestSetLocale() {
\r
181 // Create rules for testing.
\r
182 PluralRules oddAndEven = PluralRules.createRules("odd__: n mod 2 is 1");
\r
184 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
185 plFmt.applyPattern("odd__{odd} other{even}");
\r
186 plFmt.setLocale(ULocale.ENGLISH);
\r
188 // Check that pattern gets deleted.
\r
189 NumberFormat nrFmt = NumberFormat.getInstance(ULocale.ENGLISH);
\r
190 assertEquals("pattern was not resetted by setLocale() call.",
\r
194 // Check that rules got updated.
\r
196 plFmt.applyPattern("odd__{odd} other{even}");
\r
197 errln("SetLocale should reset rules but did not.");
\r
198 } catch (IllegalArgumentException e) {
\r
199 if (e.getMessage().indexOf("Unknown keyword") < 0){
\r
200 errln("Wrong exception thrown");
\r
203 plFmt.applyPattern("one{one} other{not one}");
\r
204 for (int i = 0; i < 20; ++i) {
\r
205 assertEquals("Wrong ruleset loaded by setLocale()",
\r
206 ((i==1) ? "one" : "not one"),
\r
211 public void TestParse() {
\r
212 PluralFormat plFmt = new PluralFormat("other{test}");
\r
214 plFmt.parse("test", new ParsePosition(0));
\r
215 errln("parse() should throw an UnsupportedOperationException but " +
\r
217 } catch (UnsupportedOperationException e) {
\r
220 plFmt = new PluralFormat("other{test}");
\r
222 plFmt.parseObject("test", new ParsePosition(0));
\r
223 errln("parse() should throw an UnsupportedOperationException but " +
\r
225 } catch (UnsupportedOperationException e) {
\r
229 public void TestPattern() {
\r
230 Object[] args = { "acme", null };
\r
233 PluralFormat pf = new PluralFormat(" one {one ''widget} other {# widgets} ");
\r
234 String pat = pf.toPattern();
\r
235 logln("pf pattern: '" + pat + "'");
\r
237 assertEquals("no leading spaces", "o", pat.substring(0, 1));
\r
238 assertEquals("no trailing spaces", "}", pat.substring(pat.length() - 1));
\r
241 MessageFormat pfmt = new MessageFormat("The disk ''{0}'' contains {1, plural, one {one ''''{1, number, #.0}'''' widget} other {# widgets}}.");
\r
243 for (int i = 0; i < 3; ++i) {
\r
244 args[1] = new Integer(i);
\r
245 logln(pfmt.format(args));
\r
247 PluralFormat pf = (PluralFormat)pfmt.getFormatsByArgumentIndex()[1];
\r
248 logln(pf.toPattern());
\r
249 logln(pfmt.toPattern());
\r
250 MessageFormat pfmt2 = new MessageFormat(pfmt.toPattern());
\r
251 assertEquals("message formats are equal", pfmt, pfmt2);
\r