01: /*
02: *******************************************************************************
03: * Copyright (C) 2004, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *******************************************************************************
06: */
07: package com.ibm.icu.dev.test.format;
08:
09: import com.ibm.icu.text.RuleBasedNumberFormat;
10: import com.ibm.icu.dev.test.TestFmwk;
11:
12: import java.util.Locale;
13:
14: public class RBNFParseTest extends TestFmwk {
15: public static void main(String[] args) {
16: new RBNFParseTest().run(args);
17: }
18:
19: public void TestParse() {
20:
21: // these rules make no sense but behave rationally
22: String[] okrules = { "random text", "%foo:bar", "%foo: bar",
23: "0:", "0::", "%%foo:;", "-", "-1", "-:", ".", ".1",
24: "[", "]", "[]", "[foo]", "[[]", "[]]", "[[]]", "[][]",
25: "<", ">", "=", "==", "===", "=foo=", };
26:
27: String[] exceptrules = {
28: "",
29: ";",
30: ";;",
31: ":",
32: "::",
33: ":1",
34: ":;",
35: ":;:;",
36: "<<",
37: "<<<",
38: "10:;9:;",
39: ">>",
40: ">>>",
41: "10:", // formatting any value with a one's digit will fail
42: "11: << x", // formating a multiple of 10 causes rollback rule to fail
43: "%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;", };
44:
45: String[][] allrules = { okrules, exceptrules, };
46:
47: for (int j = 0; j < allrules.length; ++j) {
48: String[] tests = allrules[j];
49: boolean except = tests == exceptrules;
50: for (int i = 0; i < tests.length; ++i) {
51: logln("----------");
52: logln("rules: '" + tests[i] + "'");
53: boolean caughtException = false;
54: try {
55: RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(
56: tests[i], Locale.US);
57: logln("1.23: " + fmt.format(20));
58: logln("-123: " + fmt.format(-123));
59: logln(".123: " + fmt.format(.123));
60: logln(" 123: " + fmt.format(123));
61: } catch (Exception e) {
62: if (!except) {
63: errln("Unexpected exception: " + e.getMessage());
64: } else {
65: caughtException = true;
66: }
67: }
68: if (except && !caughtException) {
69: errln("expected exception but didn't get one!");
70: }
71: }
72: }
73: }
74: }
|