2 *******************************************************************************
\r
3 * Copyright (C) 2007-2008, 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 com.ibm.icu.dev.test.TestFmwk;
\r
12 import com.ibm.icu.text.*;
\r
13 import com.ibm.icu.util.ULocale;
\r
15 import java.text.ParsePosition;
\r
18 * @author tschumann (Tim Schumann)
\r
21 public class PluralFormatUnitTest extends TestFmwk {
\r
22 public static void main(String[] args) throws Exception {
\r
23 new PluralFormatUnitTest().run(args);
\r
26 public void TestConstructor() {
\r
27 // Test correct formatting of numbers.
\r
28 PluralFormat plFmts[] = new PluralFormat[8];
\r
29 plFmts[0] = new PluralFormat();
\r
30 plFmts[0].applyPattern("other{#}");
\r
31 plFmts[1] = new PluralFormat(PluralRules.DEFAULT);
\r
32 plFmts[1].applyPattern("other{#}");
\r
33 plFmts[2] = new PluralFormat(PluralRules.DEFAULT, "other{#}");
\r
34 plFmts[3] = new PluralFormat("other{#}");
\r
35 plFmts[4] = new PluralFormat(ULocale.getDefault());
\r
36 plFmts[4].applyPattern("other{#}");
\r
37 plFmts[5] = new PluralFormat(ULocale.getDefault(), PluralRules.DEFAULT);
\r
38 plFmts[5].applyPattern("other{#}");
\r
39 plFmts[6] = new PluralFormat(ULocale.getDefault(),
\r
40 PluralRules.DEFAULT,
\r
42 plFmts[7] = new PluralFormat(ULocale.getDefault(), "other{#}");
\r
44 // These plural formats should produce the same output as a
\r
45 // NumberFormat for the default locale.
\r
46 NumberFormat numberFmt = NumberFormat.getInstance(ULocale.getDefault());
\r
47 for (int n = 1; n < 13; n++) {
\r
48 String result = numberFmt.format(n);
\r
49 for (int k = 0; k < plFmts.length; ++k) {
\r
50 this.assertEquals("PluralFormat's output is not as expected",
\r
51 result, plFmts[k].format(n));
\r
54 // Test some bigger numbers.
\r
55 for (int n = 100; n < 113; n++) {
\r
56 String result = numberFmt.format(n*n);
\r
57 for (int k = 0; k < plFmts.length; ++k) {
\r
58 this.assertEquals("PluralFormat's output is not as expected",
\r
59 result, plFmts[k].format(n*n));
\r
64 public void TestApplyPatternAndFormat() {
\r
65 // Create rules for testing.
\r
66 PluralRules oddAndEven = PluralRules.createRules("odd: n mod 2 is 1");
\r
68 // Test full specified case for testing RuleSet
\r
69 PluralFormat plfOddAndEven = new PluralFormat(oddAndEven);
\r
70 plfOddAndEven.applyPattern("odd{# is odd.} other{# is even.}");
\r
72 // Test fall back to other.
\r
73 PluralFormat plfOddOrEven = new PluralFormat(oddAndEven);
\r
74 plfOddOrEven.applyPattern("other{# is odd or even.}");
\r
76 NumberFormat numberFormat =
\r
77 NumberFormat.getInstance(ULocale.getDefault());
\r
78 for (int i = 0; i < 22; ++i) {
\r
79 assertEquals("Fallback to other gave wrong results",
\r
80 numberFormat.format(i) + " is odd or even.",
\r
81 plfOddOrEven.format(i));
\r
82 assertEquals("Fully specified PluralFormat gave wrong results",
\r
83 numberFormat.format(i) + ((i%2 == 1) ? " is odd."
\r
85 plfOddAndEven.format(i));
\r
88 // Check that double definition results in an exception.
\r
90 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
91 plFmt.applyPattern("odd{foo} odd{bar} other{foobar}");
\r
92 errln("Double definition of a plural case message should " +
\r
93 "provoke an exception but did not.");
\r
94 }catch (IllegalArgumentException e){}
\r
96 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
97 plFmt.applyPattern("odd{foo} other{bar} other{foobar}");
\r
98 errln("Double definition of a plural case message should " +
\r
99 "provoke an exception but did not.");
\r
100 }catch (IllegalArgumentException e){}
\r
102 // omit other keyword.
\r
104 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
105 plFmt.applyPattern("odd{foo}");
\r
106 errln("Not defining plural case other should result in an " +
\r
107 "exception but did not.");
\r
108 }catch (IllegalArgumentException e){}
\r
110 // Test unknown keyword.
\r
112 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
113 plFmt.applyPattern("otto{foo} other{bar}");
\r
114 errln("Defining a message for an unknown keyword should result in" +
\r
115 "an exception but did not.");
\r
116 }catch (IllegalArgumentException e){}
\r
118 // Test invalid keyword.
\r
120 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
121 plFmt.applyPattern("1odd{foo} other{bar}");
\r
122 errln("Defining a message for an invalid keyword should result in" +
\r
123 "an exception but did not.");
\r
124 }catch (IllegalArgumentException e){}
\r
126 // Test invalid syntax
\r
127 // -- comma between keyword{message} clauses
\r
128 // -- space in keywords
\r
129 // -- keyword{message1}{message2}
\r
131 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
132 plFmt.applyPattern("odd{foo},other{bar}");
\r
133 errln("Separating keyword{message} items with other characters " +
\r
134 "than space should provoke an exception but did not.");
\r
135 }catch (IllegalArgumentException e){}
\r
137 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
138 plFmt.applyPattern("od d{foo} other{bar}");
\r
139 errln("Spaces inside keywords should provoke an exception but " +
\r
141 }catch (IllegalArgumentException e){}
\r
143 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
144 plFmt.applyPattern("odd{foo}{foobar}other{foo}");
\r
145 errln("Defining multiple messages after a keyword should provoke " +
\r
146 "an exception but did not.");
\r
147 }catch (IllegalArgumentException e){}
\r
149 // Check that nested format is preserved.
\r
151 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
152 plFmt.applyPattern("odd{The number {0, number, #.#0} is odd.}" +
\r
153 "other{The number {0, number, #.#0} is even.}");
\r
154 for (int i = 1; i < 3; ++i) {
\r
155 assertEquals("format did not preserve a nested format string.",
\r
157 "The number {0, number, #.#0} is odd."
\r
158 : "The number {0, number, #.#0} is even."),
\r
163 // Check that a pound sign in curly braces is preserved.
\r
165 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
166 plFmt.applyPattern("odd{The number {#} is odd.}" +
\r
167 "other{The number {#} is even.}");
\r
168 for (int i = 1; i < 3; ++i) {
\r
169 assertEquals("format did not preserve # inside curly braces.",
\r
170 ((i % 2 == 1) ? "The number {#} is odd."
\r
171 : "The number {#} is even."),
\r
178 public void TestSetLocale() {
\r
179 // Create rules for testing.
\r
180 PluralRules oddAndEven = PluralRules.createRules("odd__: n mod 2 is 1");
\r
182 PluralFormat plFmt = new PluralFormat(oddAndEven);
\r
183 plFmt.applyPattern("odd__{odd} other{even}");
\r
184 plFmt.setLocale(ULocale.ENGLISH);
\r
186 // Check that pattern gets deleted.
\r
187 NumberFormat nrFmt = NumberFormat.getInstance(ULocale.ENGLISH);
\r
188 assertEquals("pattern was not resetted by setLocale() call.",
\r
192 // Check that rules got updated.
\r
194 plFmt.applyPattern("odd__{odd} other{even}");
\r
195 errln("SetLocale should reset rules but did not.");
\r
196 } catch (IllegalArgumentException e) {
\r
197 if (e.getMessage().indexOf("Unknown keyword") < 0){
\r
198 errln("Wrong exception thrown");
\r
201 plFmt.applyPattern("one{one} other{not one}");
\r
202 for (int i = 0; i < 20; ++i) {
\r
203 assertEquals("Wrong ruleset loaded by setLocale()",
\r
204 ((i==1) ? "one" : "not one"),
\r
209 public void TestParse() {
\r
210 PluralFormat plFmt = new PluralFormat("other{test}");
\r
212 plFmt.parse("test", new ParsePosition(0));
\r
213 errln("parse() should throw an UnsupportedOperationException but " +
\r
215 } catch (UnsupportedOperationException e) {
\r
218 plFmt = new PluralFormat("other{test}");
\r
220 plFmt.parseObject("test", new ParsePosition(0));
\r
221 errln("parse() should throw an UnsupportedOperationException but " +
\r
223 } catch (UnsupportedOperationException e) {
\r
227 public void TestPattern() {
\r
228 Object[] args = { "acme", null };
\r
231 PluralFormat pf = new PluralFormat(" one {one ''widget} other {# widgets} ");
\r
232 String pat = pf.toPattern();
\r
233 logln("pf pattern: '" + pat + "'");
\r
235 assertEquals("no leading spaces", "o", pat.substring(0, 1));
\r
236 assertEquals("no trailing spaces", "}", pat.substring(pat.length() - 1));
\r
239 MessageFormat pfmt = new MessageFormat("The disk ''{0}'' contains {1, plural, one {one ''''{1, number, #.0}'''' widget} other {# widgets}}.");
\r
240 System.out.println();
\r
241 for (int i = 0; i < 3; ++i) {
\r
242 args[1] = new Integer(i);
\r
243 logln(pfmt.format(args));
\r
245 PluralFormat pf = (PluralFormat)pfmt.getFormatsByArgumentIndex()[1];
\r
246 logln(pf.toPattern());
\r
247 logln(pfmt.toPattern());
\r
248 MessageFormat pfmt2 = new MessageFormat(pfmt.toPattern());
\r
249 assertEquals("message formats are equal", pfmt, pfmt2);
\r