1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2004-2013, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.dev.test.format; 10 11 import java.util.Locale; 12 13 import org.junit.Test; 14 import org.junit.runner.RunWith; 15 import org.junit.runners.JUnit4; 16 17 import com.ibm.icu.dev.test.TestFmwk; 18 import com.ibm.icu.text.RuleBasedNumberFormat; 19 import com.ibm.icu.util.ULocale; 20 21 @RunWith(JUnit4.class) 22 public class RBNFParseTest extends TestFmwk { 23 @Test 24 public void TestParse() { 25 26 // these rules make no sense but behave rationally 27 String[] okrules = { 28 "random text", 29 "%foo:bar", 30 "%foo: bar", 31 "0:", 32 "0::", 33 "%%foo:;", 34 "-", 35 "-1", 36 "-:", 37 ".", 38 ".1", 39 "[", 40 "]", 41 "[]", 42 "[foo]", 43 "[[]", 44 "[]]", 45 "[[]]", 46 "[][]", 47 "<", 48 ">", 49 "=", 50 "==", 51 "===", 52 "=foo=", 53 }; 54 55 String[] exceptrules = { 56 "", 57 ";", 58 ";;", 59 ":", 60 "::", 61 ":1", 62 ":;", 63 ":;:;", 64 "<<", 65 "<<<", 66 "10:;9:;", 67 ">>", 68 ">>>", 69 "10:", // formatting any value with a one's digit will fail 70 "11: << x", // formating a multiple of 10 causes rollback rule to fail 71 "%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;", 72 }; 73 74 String[][] allrules = { 75 okrules, 76 exceptrules, 77 }; 78 79 for (int j = 0; j < allrules.length; ++j) { 80 String[] tests = allrules[j]; 81 boolean except = tests == exceptrules; 82 for (int i = 0; i < tests.length; ++i) { 83 logln("----------"); 84 logln("rules: '" + tests[i] + "'"); 85 boolean caughtException = false; 86 try { 87 RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(tests[i], Locale.US); 88 logln("1.23: " + fmt.format(20)); 89 logln("-123: " + fmt.format(-123)); 90 logln(".123: " + fmt.format(.123)); 91 logln(" 123: " + fmt.format(123)); 92 } 93 catch (Exception e) { 94 if (!except) { 95 errln("Unexpected exception: " + e.getMessage()); 96 } else { 97 caughtException = true; 98 } 99 } 100 if (except && !caughtException) { 101 errln("expected exception but didn't get one!"); 102 } 103 } 104 } 105 } 106 107 private void parseFormat(RuleBasedNumberFormat rbnf, String s, String target) { 108 try { 109 Number n = rbnf.parse(s); 110 String t = rbnf.format(n); 111 assertEquals(rbnf.getLocale(ULocale.ACTUAL_LOCALE) + ": " + s + " : " + n, target, t); 112 } catch (java.text.ParseException e){ 113 fail("exception:" + e); 114 } 115 } 116 117 private void parseList(RuleBasedNumberFormat rbnf_en, RuleBasedNumberFormat rbnf_fr, String[][] lists) { 118 for (int i = 0; i < lists.length; ++i) { 119 String[] list = lists[i]; 120 String s = list[0]; 121 String target_en = list[1]; 122 String target_fr = list[2]; 123 124 parseFormat(rbnf_en, s, target_en); 125 parseFormat(rbnf_fr, s, target_fr); 126 } 127 } 128 129 @Test 130 public void TestLenientParse() throws Exception { 131 RuleBasedNumberFormat rbnf_en, rbnf_fr; 132 133 // TODO: this still passes, but setLenientParseMode should have no effect now. 134 // Did it ever test what it was supposed to? 135 rbnf_en = new RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT); 136 rbnf_en.setLenientParseMode(true); 137 rbnf_fr = new RuleBasedNumberFormat(Locale.FRENCH, RuleBasedNumberFormat.SPELLOUT); 138 rbnf_fr.setLenientParseMode(true); 139 140 Number n = rbnf_en.parse("1,2 million"); 141 logln(n.toString()); 142 143 String[][] lists = { 144 { "1,2", "twelve", "un virgule deux" }, 145 { "1,2 million", "twelve million", "un virgule deux" }, 146 { "1,2 millions", "twelve million", "un million deux cent mille" }, 147 { "1.2", "one point two", "douze" }, 148 { "1.2 million", "one million two hundred thousand", "douze" }, 149 { "1.2 millions", "one million two hundred thousand", "douze millions" }, 150 }; 151 152 Locale.setDefault(Locale.FRANCE); 153 logln("Default locale:" + Locale.getDefault()); 154 logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName()); 155 logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName()); 156 parseList(rbnf_en, rbnf_fr, lists); 157 158 Locale.setDefault(Locale.US); 159 logln("Default locale:" + Locale.getDefault()); 160 logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName()); 161 logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName()); 162 parseList(rbnf_en, rbnf_fr, lists); 163 } 164 } 165