1 | define(["doh/main", "../_base/array", "../number", "../i18n"], function(doh, array, number, i18n){ |
---|
2 | var tests= {number:{}}; |
---|
3 | /** |
---|
4 | * Refer to ICU4J's NumberFormatTest.expect(...) |
---|
5 | */ |
---|
6 | tests.number.check=function(t,options,sourceInput,expectResult){ |
---|
7 | tests.number.checkFormatParseCycle(t, t,options,sourceInput,expectResult,false); |
---|
8 | tests.number.checkParse(t, t,options,expectResult,sourceInput); |
---|
9 | }; |
---|
10 | |
---|
11 | /** |
---|
12 | * Perform a single formatting check or a backward check |
---|
13 | * backward check:number1 -(formatted)-> string1 -(parsed)-> number2 -(formated)-> string2 |
---|
14 | * then number should == number2; string1 should == string2 |
---|
15 | */ |
---|
16 | tests.number.checkFormatParseCycle=function(t,options,sourceInput,expectResult, |
---|
17 | backwardCheck/*boolean,indicates whether need a backward chain check,like formate->parse->format*/){ |
---|
18 | if(null != options){ |
---|
19 | var pattern = options.pattern; |
---|
20 | var locale = options.locale; |
---|
21 | //TODO: add more fields |
---|
22 | } |
---|
23 | |
---|
24 | //print("\n"); |
---|
25 | var str = null==pattern?"default":pattern; |
---|
26 | //print("pattern:" + str + "| locale:" + locale); |
---|
27 | //print("input:" + sourceInput); |
---|
28 | var result = number.format(sourceInput,options); |
---|
29 | //print("result:" + result); |
---|
30 | if(null != expectResult){ |
---|
31 | t.is(expectResult,result); |
---|
32 | } |
---|
33 | if(backwardCheck){ |
---|
34 | var resultParsed = number.parse(result,options); |
---|
35 | //print("resultParsed:" + resultParsed); |
---|
36 | if(!tests.number._decimalNumberDiff(sourceInput,resultParsed)){ |
---|
37 | t.is(sourceInput,resultParsed); |
---|
38 | } |
---|
39 | var resultParsedReformatted = number.format(resultParsed,options); |
---|
40 | //print("resultParsedReformatted:" + resultParsedReformatted); |
---|
41 | if(!tests.number._decimalNumberDiff(result,resultParsedReformatted)){ |
---|
42 | t.is(result,resultParsedReformatted); |
---|
43 | } |
---|
44 | } |
---|
45 | }; |
---|
46 | |
---|
47 | /** |
---|
48 | * Perform a single parsing check |
---|
49 | */ |
---|
50 | tests.number.checkParse=function(t,options,sourceInput,expectResult){ |
---|
51 | var str = "default"; |
---|
52 | if(null != options && null != options.pattern){ |
---|
53 | str = options.pattern; |
---|
54 | } |
---|
55 | //print("input:" + sourceInput); |
---|
56 | var result = number.parse(sourceInput,options); |
---|
57 | //print("result :" + result); |
---|
58 | if(null != expectResult){ |
---|
59 | t.is(expectResult,result); |
---|
60 | } |
---|
61 | }; |
---|
62 | |
---|
63 | /** |
---|
64 | * //TODO:Round a given number |
---|
65 | */ |
---|
66 | tests.number.rounding = function(t,num,maxFractionDigits,expected){ |
---|
67 | var pattern="#0."; |
---|
68 | for(var i=0; i<maxFractionDigits; i++){pattern += "#";} |
---|
69 | var result = number.format(num,{locale:tests.number.locale, pattern:pattern}); |
---|
70 | t.is(expected,result); |
---|
71 | }; |
---|
72 | |
---|
73 | /** |
---|
74 | * Run a batch parsing |
---|
75 | */ |
---|
76 | function runBatchParse(options,dataArray/*array*/,pass/*boolean*/){ |
---|
77 | var exception = null; |
---|
78 | var result; |
---|
79 | var i=0; |
---|
80 | var str = (null==options.pattern)?"default":options.pattern; |
---|
81 | |
---|
82 | //print("\n"); |
---|
83 | for(; i<dataArray.length; i++){ |
---|
84 | try{ |
---|
85 | //print("["+i+"]"+"input:"+dataArray[i]); |
---|
86 | result = number.parse(dataArray[i],options); |
---|
87 | if(isNaN(result)){ |
---|
88 | throw "\"" + dataArray[i] + "\" is parsed to NaN with pattern " + str; |
---|
89 | } |
---|
90 | //print("["+i+"]"+"output:"+result); |
---|
91 | }catch(e){ |
---|
92 | exception = e; |
---|
93 | break; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | if(!pass && (exception == null)){ |
---|
98 | throw "runBatchParse() - stric parse failed, no exception when parsing illegal data"; |
---|
99 | }else if(exception != null){ |
---|
100 | if(!pass && i == 0){ |
---|
101 | //strict parsing should fail for all the dataArray elements as expected |
---|
102 | //pass condition for strict parsing |
---|
103 | return; |
---|
104 | } |
---|
105 | throw "runBatchParse() failed: " + exception; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Check whether the given two numbers differ under the decimal bound |
---|
111 | * |
---|
112 | */ |
---|
113 | tests.number._decimalNumberDiff = function(num1,num2){ |
---|
114 | //TODO: should be more accurate when dojo.number finish rounding in the future |
---|
115 | var diffBound = 1e-3; |
---|
116 | var diff = num1 - num2; |
---|
117 | //print("Math.abs(diff) " + Math.abs(diff)); |
---|
118 | if(Math.abs(diff) < diffBound ){ |
---|
119 | return true; |
---|
120 | }else if(isNaN(Math.abs(diff))){ |
---|
121 | var s = num1.toString().split(num2); |
---|
122 | s[1] = s[1].replace(",","0"); |
---|
123 | s[1] = s[1].replace('\u066b','0'); |
---|
124 | return (new Number(s[1])< diffBound); |
---|
125 | } |
---|
126 | return false; |
---|
127 | }; |
---|
128 | |
---|
129 | doh.register("tests.number", |
---|
130 | [ |
---|
131 | { |
---|
132 | // Test formatting and parsing of currencies in various locales pre-built in dojo.cldr |
---|
133 | // NOTE: we can't set djConfig.extraLocale before bootstrapping unit tests, so directly |
---|
134 | // load resources here for specific locales: |
---|
135 | |
---|
136 | name: "number", |
---|
137 | runTest: function(t){ |
---|
138 | var partLocaleList = ["en-us", "fr-fr", "de-de"]; |
---|
139 | tests.number.locale = "en-us"; |
---|
140 | if(require.async){ |
---|
141 | var |
---|
142 | def = new doh.Deferred(), |
---|
143 | deps = []; |
---|
144 | array.forEach(partLocaleList, function(locale){ |
---|
145 | deps.push(dojo.getL10nName("dojo/cldr", "number", locale)); |
---|
146 | }); |
---|
147 | require(deps, function(){ |
---|
148 | def.callback(true); |
---|
149 | }); |
---|
150 | return def; |
---|
151 | }else{ // tests for the v1.x loader/i18n machinery |
---|
152 | for(var i = 0 ; i < partLocaleList.length; i ++){ |
---|
153 | i18n.getLocalization("dojo.cldr","number",partLocaleList[i]); |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | }, |
---|
158 | { |
---|
159 | name: "invalid", |
---|
160 | runTest: function(t){ |
---|
161 | t.t(null === number.format(NaN)); |
---|
162 | t.t(null === number.format(Number.NaN)); |
---|
163 | t.t(null === number.format(Infinity)); |
---|
164 | t.t(null === number.format(-Infinity)); |
---|
165 | } |
---|
166 | }, |
---|
167 | { |
---|
168 | name: "round", |
---|
169 | runTest: function(t){ |
---|
170 | t.is(0, number.round(0)); |
---|
171 | t.is(1, number.round(0.5)); |
---|
172 | t.is(-1, number.round(-0.5)); |
---|
173 | t.is(0.1, number.round(0.05, 1)); |
---|
174 | t.is(0.1, number.round(0.09, 1)); |
---|
175 | t.is(0.0, number.round(0.04999999, 1)); |
---|
176 | t.is(0.1, number.round(0.09499999, 1)); |
---|
177 | t.is(0.1, number.round(0.095, 1)); |
---|
178 | t.is(0.1, number.round(0.09999999, 1)); |
---|
179 | t.is(-0.1, number.round(-0.05, 1)); |
---|
180 | t.is(1.1, number.round(1.05, 1)); |
---|
181 | t.is(-1.1, number.round(-1.05, 1)); |
---|
182 | // t.is(-162.29, number.round(-162.295, 2)); // see ticket #7930, dojox.math.round |
---|
183 | // t.is(162.29, number.round(162.295, 2)); // ibid |
---|
184 | } |
---|
185 | }, |
---|
186 | { |
---|
187 | name: "round_multiple", |
---|
188 | runTest: function(t){ |
---|
189 | t.is("123.455", number.round(123.4525, 2, 5)); |
---|
190 | t.is("123.45", number.round(123.452, 2, 5)); |
---|
191 | t.is("123.455", number.round(123.454, 2, 5)); |
---|
192 | t.is("123.455", number.round(123.456, 2, 5)); |
---|
193 | t.is("-123.45", number.round(-123.452, 2, 5)); |
---|
194 | t.is("-123.455", number.round(-123.4525, 2, 5)); |
---|
195 | t.is("-123.455", number.round(-123.454, 2, 5)); |
---|
196 | t.is("-123.455", number.round(-123.456, 2, 5)); |
---|
197 | } |
---|
198 | }, |
---|
199 | { |
---|
200 | name: "round_speleotrove", |
---|
201 | runTest: function(t){ |
---|
202 | // submitted Mike Cowlishaw (IBM, CCLA), see http://speleotrove.com/decimal/#testcases |
---|
203 | t.is(12345, number.round(12345 + -0.1), "radx200"); |
---|
204 | t.is(12345, number.round(12345 + -0.01), "radx201"); |
---|
205 | t.is(12345, number.round(12345 + -0.001), "radx202"); |
---|
206 | t.is(12345, number.round(12345 + -0.00001), "radx203"); |
---|
207 | t.is(12345, number.round(12345 + -0.000001), "radx204"); |
---|
208 | t.is(12345, number.round(12345 + -0.0000001), "radx205"); |
---|
209 | t.is(12345, number.round(12345 + 0), "radx206"); |
---|
210 | t.is(12345, number.round(12345 + 0.0000001), "radx207"); |
---|
211 | t.is(12345, number.round(12345 + 0.000001), "radx208"); |
---|
212 | t.is(12345, number.round(12345 + 0.00001), "radx209"); |
---|
213 | t.is(12345, number.round(12345 + 0.0001), "radx210"); |
---|
214 | t.is(12345, number.round(12345 + 0.001), "radx211"); |
---|
215 | t.is(12345, number.round(12345 + 0.01), "radx212"); |
---|
216 | t.is(12345, number.round(12345 + 0.1), "radx213"); |
---|
217 | |
---|
218 | t.is(12346, number.round(12346 + 0.49999), "radx215"); |
---|
219 | t.is(12347, number.round(12346 + 0.5), "radx216"); |
---|
220 | t.is(12347, number.round(12346 + 0.50001), "radx217"); |
---|
221 | |
---|
222 | t.is(12345, number.round(12345 + 0.4), "radx220"); |
---|
223 | t.is(12345, number.round(12345 + 0.49), "radx221"); |
---|
224 | t.is(12345, number.round(12345 + 0.499), "radx222"); |
---|
225 | t.is(12345, number.round(12345 + 0.49999), "radx223"); |
---|
226 | t.is(12346, number.round(12345 + 0.5), "radx224"); |
---|
227 | t.is(12346, number.round(12345 + 0.50001), "radx225"); |
---|
228 | t.is(12346, number.round(12345 + 0.5001), "radx226"); |
---|
229 | t.is(12346, number.round(12345 + 0.501), "radx227"); |
---|
230 | t.is(12346, number.round(12345 + 0.51), "radx228"); |
---|
231 | t.is(12346, number.round(12345 + 0.6), "radx229"); |
---|
232 | |
---|
233 | //negatives |
---|
234 | t.is(-12345, number.round(-12345 + -0.1), "rsux200"); |
---|
235 | t.is(-12345, number.round(-12345 + -0.01), "rsux201"); |
---|
236 | t.is(-12345, number.round(-12345 + -0.001), "rsux202"); |
---|
237 | t.is(-12345, number.round(-12345 + -0.00001), "rsux203"); |
---|
238 | t.is(-12345, number.round(-12345 + -0.000001), "rsux204"); |
---|
239 | t.is(-12345, number.round(-12345 + -0.0000001), "rsux205"); |
---|
240 | t.is(-12345, number.round(-12345 + 0), "rsux206"); |
---|
241 | t.is(-12345, number.round(-12345 + 0.0000001), "rsux207"); |
---|
242 | t.is(-12345, number.round(-12345 + 0.000001), "rsux208"); |
---|
243 | t.is(-12345, number.round(-12345 + 0.00001), "rsux209"); |
---|
244 | t.is(-12345, number.round(-12345 + 0.0001), "rsux210"); |
---|
245 | t.is(-12345, number.round(-12345 + 0.001), "rsux211"); |
---|
246 | t.is(-12345, number.round(-12345 + 0.01), "rsux212"); |
---|
247 | t.is(-12345, number.round(-12345 + 0.1), "rsux213"); |
---|
248 | |
---|
249 | t.is(-12346, number.round(-12346 + 0.49999), "rsux215"); |
---|
250 | t.is(-12346, number.round(-12346 + 0.5), "rsux216"); |
---|
251 | t.is(-12345, number.round(-12346 + 0.50001 ), "rsux217"); |
---|
252 | |
---|
253 | t.is(-12345, number.round(-12345 + 0.4), "rsux220"); |
---|
254 | t.is(-12345, number.round(-12345 + 0.49), "rsux221"); |
---|
255 | t.is(-12345, number.round(-12345 + 0.499), "rsux222"); |
---|
256 | t.is(-12345, number.round(-12345 + 0.49999), "rsux223"); |
---|
257 | t.is(-12345, number.round(-12345 + 0.5), "rsux224"); |
---|
258 | t.is(-12344, number.round(-12345 + 0.50001), "rsux225"); |
---|
259 | t.is(-12344, number.round(-12345 + 0.5001), "rsux226"); |
---|
260 | t.is(-12344, number.round(-12345 + 0.501), "rsux227"); |
---|
261 | t.is(-12344, number.round(-12345 + 0.51), "rsux228"); |
---|
262 | t.is(-12344, number.round(-12345 + 0.6), "rsux229"); |
---|
263 | |
---|
264 | t.is(12345, number.round( 12345 / 1), "rdvx401"); |
---|
265 | t.is(12344, number.round( 12345 / 1.0001), "rdvx402"); |
---|
266 | t.is(12333, number.round( 12345 / 1.001), "rdvx403"); |
---|
267 | t.is(12223, number.round( 12345 / 1.01), "rdvx404"); |
---|
268 | t.is(11223, number.round( 12345 / 1.1), "rdvx405"); |
---|
269 | |
---|
270 | t.is(3088.8, number.round( 12355 / 4, 1), "rdvx406"); |
---|
271 | t.is(3086.3, number.round( 12345 / 4, 1), "rdvx407"); |
---|
272 | t.is(3088.7, number.round( 12355 / 4.0001, 1), "rdvx408"); |
---|
273 | t.is(3086.2, number.round( 12345 / 4.0001, 1), "rdvx409"); |
---|
274 | t.is(2519.4, number.round( 12345 / 4.9, 1), "rdvx410"); |
---|
275 | t.is(2473.9, number.round( 12345 / 4.99, 1), "rdvx411"); |
---|
276 | t.is(2469.5, number.round( 12345 / 4.999, 1), "rdvx412"); |
---|
277 | t.is(2469.0, number.round( 12345 / 4.9999, 1), "rdvx413"); |
---|
278 | t.is(2469, number.round( 12345 / 5, 1), "rdvx414"); |
---|
279 | t.is(2469.0, number.round( 12345 / 5.0001, 1), "rdvx415"); |
---|
280 | t.is(2468.5, number.round( 12345 / 5.001, 1), "rdvx416"); |
---|
281 | t.is(2464.1, number.round( 12345 / 5.01, 1), "rdvx417"); |
---|
282 | t.is(2420.6, number.round( 12345 / 5.1, 1), "rdvx418"); |
---|
283 | |
---|
284 | t.is(12345, number.round( 12345 * 1), "rmux401"); |
---|
285 | t.is(12346, number.round( 12345 * 1.0001), "rmux402"); |
---|
286 | t.is(12357, number.round( 12345 * 1.001), "rmux403"); |
---|
287 | t.is(12468, number.round( 12345 * 1.01), "rmux404"); |
---|
288 | t.is(13580, number.round( 12345 * 1.1), "rmux405"); |
---|
289 | t.is(49380, number.round( 12345 * 4), "rmux406"); |
---|
290 | t.is(49381, number.round( 12345 * 4.0001), "rmux407"); |
---|
291 | t.is(60491, number.round( 12345 * 4.9), "rmux408"); |
---|
292 | t.is(61602, number.round( 12345 * 4.99), "rmux409"); |
---|
293 | t.is(61713, number.round( 12345 * 4.999), "rmux410"); |
---|
294 | t.is(61724, number.round( 12345 * 4.9999), "rmux411"); |
---|
295 | t.is(61725, number.round( 12345 * 5), "rmux412"); |
---|
296 | t.is(61726, number.round( 12345 * 5.0001), "rmux413"); |
---|
297 | t.is(61737, number.round( 12345 * 5.001), "rmux414"); |
---|
298 | t.is(61848, number.round( 12345 * 5.01), "rmux415"); |
---|
299 | /* |
---|
300 | t.is(1.4814E+5, number.round( 12345 * 12), "rmux416"); |
---|
301 | t.is(1.6049E+5, number.round( 12345 * 13), "rmux417"); |
---|
302 | t.is(1.4826E+5, number.round( 12355 * 12), "rmux418"); |
---|
303 | t.is(1.6062E+5, number.round( 12355 * 13), "rmux419"); |
---|
304 | */ |
---|
305 | } |
---|
306 | }, |
---|
307 | { |
---|
308 | name: "format", // old tests |
---|
309 | runTest: function(t){ |
---|
310 | |
---|
311 | t.is("0123", number.format(123, {pattern: "0000"})); |
---|
312 | t.is("-12,34,567.890", number.format(-1234567.89, {pattern: "#,##,##0.000##", locale: 'en-us'})); |
---|
313 | t.is("-12,34,567.89012", number.format(-1234567.890123, {pattern: "#,##,##0.000##", locale: 'en-us'})); |
---|
314 | t.is("(1,234,567.89012)", number.format(-1234567.890123, {pattern: "#,##0.000##;(#,##0.000##)", locale: 'en-us'})); |
---|
315 | t.is("(1,234,567.89012)", number.format(-1234567.890123, {pattern: "#,##0.000##;(#)", locale: 'en-us'})); |
---|
316 | t.is("50.1%", number.format(0.501, {pattern: "#0.#%", locale: 'en-us'})); |
---|
317 | t.is("98", number.format(1998, {pattern: "00"})); |
---|
318 | t.is("01998", number.format(1998, {pattern: "00000"})); |
---|
319 | t.is("0.13", number.format(0.125, {pattern: "0.##", locale: 'en-us'})); //NOTE: expects round_half_up, not round_half_even |
---|
320 | t.is("0.1250", number.format(0.125, {pattern: "0.0000", locale: 'en-us'})); |
---|
321 | t.is("0.1", number.format(0.100004, {pattern: "0.####", locale: 'en-us'})); |
---|
322 | |
---|
323 | t.is("-12", number.format(-12.3, {places:0, locale: "en-us"})); |
---|
324 | t.is("-1,234,567.89", number.format(-1234567.89, {locale: "en-us"})); |
---|
325 | // t.is("-12,34,567.89", number.format(-1234567.89, {locale: "en-in"})); |
---|
326 | t.is("-1,234,568", number.format(-1234567.89, {places:0, locale: "en-us"})); |
---|
327 | // t.is("-12,34,568", number.format(-1234567.89, {places:0, locale: "en-in"})); |
---|
328 | t.is("-1\xa0000,10", number.format(-1000.1, {places:2, locale: "fr-fr"})); |
---|
329 | t.is("-1,000.10", number.format(-1000.1, {places:2, locale: "en-us"})); |
---|
330 | t.is("-1\xa0000,10", number.format(-1000.1, {places:2, locale: "fr-fr"})); |
---|
331 | t.is("-1.234,56", number.format(-1234.56, {places:2, locale: "de-de"})); |
---|
332 | t.is("-1,000.10", number.format(-1000.1, {places:2, locale: "en-us"})); |
---|
333 | t.is("123.46%", number.format(1.23456, {places:2, locale: "en-us", type: "percent"})); |
---|
334 | t.is("123.4", number.format(123.4, {places:'1,3', locale: 'en-us'})); |
---|
335 | t.is("123.45", number.format(123.45, {places:'1,3', locale: 'en-us'})); |
---|
336 | t.is("123.456", number.format(123.456, {places:'1,3', locale: 'en-us'})); |
---|
337 | |
---|
338 | //rounding |
---|
339 | t.is("-1,234,568", number.format(-1234567.89, {places:0, locale: "en-us"})); |
---|
340 | // t.is("-12,34,568", number.format(-1234567.89, {places:0, locale: "en-in"})); |
---|
341 | t.is("-1,000.11", number.format(-1000.114, {places:2, locale: "en-us"})); |
---|
342 | t.is("-1,000.12", number.format(-1000.115, {places:2, locale: "en-us"})); |
---|
343 | t.is("-1,000.12", number.format(-1000.116, {places:2, locale: "en-us"})); |
---|
344 | t.is("-0.00", number.format(-0.0001, {places:2, locale: "en-us"})); |
---|
345 | t.is("0.00", number.format(0, {places:2, locale: "en-us"})); |
---|
346 | |
---|
347 | //change decimal places |
---|
348 | t.is("-1\xa0000,100", number.format(-1000.1, {places:3, locale: "fr-fr"})); |
---|
349 | t.is("-1,000.100", number.format(-1000.1, {places:3, locale: "en-us"})); |
---|
350 | } |
---|
351 | }, |
---|
352 | { |
---|
353 | name: "parse", // old tests |
---|
354 | runTest: function(t){ |
---|
355 | t.is(1000, number.parse("1000", {locale: "en-us"})); |
---|
356 | t.is(1000.123, number.parse("1000.123", {locale: "en-us"})); |
---|
357 | t.is(1000, number.parse("1,000", {locale: "en-us"})); |
---|
358 | t.is(-1000, number.parse("-1000", {locale: "en-us"})); |
---|
359 | t.is(-1000.123, number.parse("-1000.123", {locale: "en-us"})); |
---|
360 | t.is(-1234567.89, number.parse("-1,234,567.89", {locale: "en-us"})); |
---|
361 | t.is(-1234567.89, number.parse("-1 234 567,89", {locale: "fr-fr"})); |
---|
362 | t.t(isNaN(number.parse("-1 234 567,89", {locale: "en-us"}))); |
---|
363 | |
---|
364 | t.is(123, number.parse("0123", {pattern: "0000"})); |
---|
365 | |
---|
366 | t.t(isNaN(number.parse("10,00", {locale: "en-us"}))); |
---|
367 | t.t(isNaN(number.parse("1000.1", {locale: "fr-fr"}))); |
---|
368 | |
---|
369 | t.t(isNaN(number.parse(""))); |
---|
370 | t.t(isNaN(number.parse("abcd"))); |
---|
371 | |
---|
372 | // should allow unlimited precision, by default |
---|
373 | t.is(1.23456789, number.parse("1.23456789", {locale: "en-us"})); |
---|
374 | |
---|
375 | //test whitespace |
---|
376 | // t.is(-1234567, number.parse(" -1,234,567 ", {locale: "en-us"})); |
---|
377 | |
---|
378 | // t.t(number.parse("9.1093826E-31")); |
---|
379 | t.is(1.23, number.parse("123%", {locale: "en-us", type: "percent"})); |
---|
380 | t.is(1.23, number.parse("123%", {places:0, locale: "en-us", type: "percent"})); |
---|
381 | t.t(isNaN(number.parse("123.46%", {places:0, locale: "en-us", type: "percent"}))); |
---|
382 | t.is(1.2346, number.parse("123.46%", {places:2, locale: "en-us", type: "percent"})); |
---|
383 | t.is(0.501, number.parse("50.1%", {pattern: "#0.#%", locale: 'en-us'})); |
---|
384 | |
---|
385 | t.is(123.4, number.parse("123.4", {pattern: "#0.#", locale: 'en-us'})); |
---|
386 | t.is(-123.4, number.parse("-123.4", {pattern: "#0.#", locale: 'en-us'})); |
---|
387 | t.is(123.4, number.parse("123.4", {pattern: "#0.#;(#0.#)", locale: 'en-us'})); |
---|
388 | t.is(-123.4, number.parse("(123.4)", {pattern: "#0.#;(#0.#)", locale: 'en-us'})); |
---|
389 | |
---|
390 | t.is(null, number.format("abcd", {pattern: "0000"})); |
---|
391 | |
---|
392 | t.is(123, number.parse("123", {places:0})); |
---|
393 | t.is(123, number.parse("123", {places:'0'})); |
---|
394 | t.is(123.4, number.parse("123.4", {places:1, locale: 'en-us'})); |
---|
395 | t.is(123.45, number.parse("123.45", {places:'1,3', locale: 'en-us'})); |
---|
396 | t.is(123.45, number.parse("123.45", {places:'0,2', locale: 'en-us'})); |
---|
397 | } |
---|
398 | }, |
---|
399 | { |
---|
400 | name: "format_icu4j3_6", |
---|
401 | runTest: function(t){ |
---|
402 | |
---|
403 | /************************************************************************************************* |
---|
404 | * Evan:The following test cases are referred from ICU4J 3.6 (NumberFormatTest etc.) |
---|
405 | * see http://icu.sourceforge.net/download/3.6.html#ICU4J |
---|
406 | *************************************************************************************************/ |
---|
407 | |
---|
408 | |
---|
409 | /** |
---|
410 | * In ICU4J, testing logic for NumberFormat.format() is seperated into |
---|
411 | * differernt single tese cases. So part of these logic are |
---|
412 | * collected together in this single method. |
---|
413 | * |
---|
414 | * !!Failed cases are as follows: |
---|
415 | * 1.1234567890987654321234567890987654321 should be formatted as |
---|
416 | * 1,234,567,890,987,654,321,234,567,890,987,654,321 with all the default parameters, |
---|
417 | * but got 1.234 instead, may due to the unimplemeted exponent. |
---|
418 | * 2.\u00a4 and ' are not replaced |
---|
419 | * with pattern "'*&'' '\u00a4' ''&*' #,##0.00" |
---|
420 | * 1.0 should be formatted to "*&' Re. '&* 1.00",but got "'*&'' '\u00a4' ''&*' 1.00" instead |
---|
421 | * etc. |
---|
422 | * |
---|
423 | */ |
---|
424 | //print("test_number_format_icu4j3_6() start.............."); |
---|
425 | /* !!Failed case, 1.234 returned instead |
---|
426 | //refer to ICU4J's NumberFormatTest.TestCoverage() |
---|
427 | var bigNum = 1234567890987654321234567890987654321; |
---|
428 | var expectResult = "1,234,567,890,987,654,321,234,567,890,987,654,321"; |
---|
429 | tests.number.checkFormatParseCycle(t, null,bigNum,expectResult,false); |
---|
430 | */ |
---|
431 | |
---|
432 | //in icu4j should throw out an exception when formatting a string, |
---|
433 | //but it seems number.format can deal with strings |
---|
434 | //return 123,456,789 |
---|
435 | number.format("123456789"); |
---|
436 | |
---|
437 | //!!Failed case, \u00a4 and ' are not replaced |
---|
438 | /* |
---|
439 | var options = {pattern:"'*&'' '\u00a4' ''&*' #,##0.00",locale:"en-us"}; |
---|
440 | tests.number.check(t, options,1.0, "*&' Re. '&* 1.00"); |
---|
441 | tests.number.check(t, options,-2.0, "-*&' Rs. '&* 2.00"); |
---|
442 | |
---|
443 | options = {pattern:"#,##0.00 '*&'' '\u00a4' ''&*'",locale:"en-us"}; |
---|
444 | tests.number.check(t, options,1.0,"1.00 *&' Re. '&*"); |
---|
445 | tests.number.check(t, options,-2.0,"-2.00 *&' Rs. '&*"); |
---|
446 | */ |
---|
447 | //print("test_number_format_icu4j3_6() end..............\n"); |
---|
448 | } |
---|
449 | }, |
---|
450 | { |
---|
451 | name: "format_patterns", |
---|
452 | runTest: function(t){ |
---|
453 | |
---|
454 | /** |
---|
455 | * Refer to ICU4J's NumberFormatTest.TestPatterns() which now only coveres us locale |
---|
456 | */ |
---|
457 | //print("test_number_format_Patterns() start.............."); |
---|
458 | var patterns = (["#0.#", "#0.", "#.0", "#"]); |
---|
459 | var patternsLength = patterns.length; |
---|
460 | var num = (["0","0", "0.0", "0"]); |
---|
461 | var options; |
---|
462 | //icu4j result seems doesn't work as: |
---|
463 | //var num = (["0","0.", ".0", "0"]); |
---|
464 | for (var i=0; i<patternsLength; ++i) |
---|
465 | { |
---|
466 | options = {pattern:patterns[i], locale: 'en-us'}; |
---|
467 | tests.number.checkFormatParseCycle(t, options,0,num[i],false); |
---|
468 | } |
---|
469 | |
---|
470 | //!!Failed case |
---|
471 | //In ICU4J: |
---|
472 | // unquoted special characters in the suffix are illegal |
---|
473 | // so "000.000|###" is illegal; "000.000'|###'" is legal |
---|
474 | //number.format: |
---|
475 | // when formatting 1.2 with illegal pattern "000.000|###" |
---|
476 | // no exception was thrown but got "001.200|###" instead. |
---|
477 | |
---|
478 | /* |
---|
479 | patterns = (["000.000|###","000.000'|###'"]); |
---|
480 | var exception = false; |
---|
481 | var result; |
---|
482 | for(var i = 0; i < patterns.length; i ++){ |
---|
483 | try{ |
---|
484 | //"001.200'|###'" is return for "000.000'|###'" |
---|
485 | //"001.200|###" is return for "000.000|###" |
---|
486 | result = number.format(1.2,{pattern:patterns[i]}); |
---|
487 | print("["+i+"] 1.2 is formatted to " + result + " with pattern " + patterns[i]); |
---|
488 | }catch(e){ |
---|
489 | exception = true; |
---|
490 | } |
---|
491 | if(exception && i==1){ |
---|
492 | throw "["+i+"]Failed when formatting 1.2 using legal pattern " + patterns[i]; |
---|
493 | }else if(!exception && i==0){ |
---|
494 | throw "["+i+"]Failed when formatting 1.2 using illegal pattern " + patterns[i]; |
---|
495 | } |
---|
496 | }*/ |
---|
497 | //print("test_number_format_Patterns() end..............\n"); |
---|
498 | } |
---|
499 | }, |
---|
500 | { |
---|
501 | name: "exponential", |
---|
502 | runTest: function(t){ |
---|
503 | /** |
---|
504 | * TODO: For dojo.number future version |
---|
505 | * Refer to ICU4J's NumberFormatTest.TestExponential() |
---|
506 | */ |
---|
507 | } |
---|
508 | }, |
---|
509 | { |
---|
510 | name: "format_quotes", |
---|
511 | runTest: function(t){ |
---|
512 | /** |
---|
513 | * TODO: Failed case |
---|
514 | * Refer to ICU4J's NumberFormatTest.TestQuotes() |
---|
515 | */ |
---|
516 | //print("test_number_format_Quotes() start.............."); |
---|
517 | //TODO: add more locales |
---|
518 | |
---|
519 | //TODO:!!Failed case |
---|
520 | //Pattern "s'aa''s'c#" should format 6666 to "saa'sc6666", but got s'aa''s'c6666 instead |
---|
521 | // is this case necessary? |
---|
522 | /* |
---|
523 | var pattern = "s'aa''s'c#"; |
---|
524 | var result = number.format(6666,{pattern:pattern,locale:"en-us"}); |
---|
525 | var expectResult = "saa'sc6666"; |
---|
526 | t.is(expectResult,result); |
---|
527 | */ |
---|
528 | //print("test_number_format_Quotes() end.............."); |
---|
529 | } |
---|
530 | }, |
---|
531 | { |
---|
532 | name: "format_rounding", |
---|
533 | runTest: function(t){ |
---|
534 | /** |
---|
535 | * Refer to ICU4J's NumberFormatTest.TestRounding487() and NumberFormatTest.TestRounding() |
---|
536 | */ |
---|
537 | //print("test_number_format_rounding() start.............."); |
---|
538 | tests.number.rounding(t,0.000179999, 5, "0.00018"); |
---|
539 | tests.number.rounding(t,0.00099, 4, "0.001"); |
---|
540 | tests.number.rounding(t,17.6995, 3, "17.7"); |
---|
541 | tests.number.rounding(t,15.3999, 0, "15"); |
---|
542 | tests.number.rounding(t,-29.6, 0, "-30"); |
---|
543 | |
---|
544 | //TODO refer to NumberFormatTest.TestRounding() |
---|
545 | |
---|
546 | //print("test_number_format_rounding() end.............."); |
---|
547 | } |
---|
548 | }, |
---|
549 | { |
---|
550 | name: "format_scientific", |
---|
551 | runTest: function(t){ |
---|
552 | /** |
---|
553 | * TODO: For dojo.number future version |
---|
554 | * Refer to ICU4J's NumberFormatTest.TestScientific()- Exponential testing |
---|
555 | * Refer to ICU4J's NumberFormatTest.TestScientific2() |
---|
556 | * Refer to ICU4J's NumberFormatTest.TestScientificGrouping() |
---|
557 | */ |
---|
558 | } |
---|
559 | }, |
---|
560 | { |
---|
561 | name: "format_perMill", |
---|
562 | runTest: function(t){ |
---|
563 | /** |
---|
564 | * TODO: Failed case |
---|
565 | * Refer to ICU4J's NumberFormatTest.TestPerMill() |
---|
566 | */ |
---|
567 | //print("test_number_format_PerMill() start.............."); |
---|
568 | var pattern; |
---|
569 | var result; |
---|
570 | var expectResult; |
---|
571 | |
---|
572 | //TODO: !!Failed case - ###.###\u2030(\u2030 is â°) |
---|
573 | //Pattern ###.###\u2030 should format 0.4857 as 485.7\u2030,but got 485.700\u2030 instead |
---|
574 | pattern = "###.###\u2030"; |
---|
575 | expectResult = "485.7\u2030"; |
---|
576 | result = number.format(0.4857,{pattern:pattern, locale: 'en-us'}); |
---|
577 | t.is(expectResult,result); |
---|
578 | |
---|
579 | //TODO: !!Failed mile percent case - ###.###m |
---|
580 | //Pattern "###.###m" should format 0.4857 to 485.7m, but got 0.485m instead |
---|
581 | /* |
---|
582 | pattern = "###.###m"; |
---|
583 | expectResult = "485.7m"; |
---|
584 | result = number.format(0.4857,{pattern:pattern,locale:"en"}); |
---|
585 | t.is(expectResult,result); |
---|
586 | */ |
---|
587 | //print("test_number_format_PerMill() end..............\n"); |
---|
588 | } |
---|
589 | }, |
---|
590 | { |
---|
591 | name: "format_grouping", |
---|
592 | runTest: function(t){ |
---|
593 | /** |
---|
594 | * Only test en-us and en-in |
---|
595 | * Refer to ICU4J's NumberFormatTest.TestSecondaryGrouping() |
---|
596 | */ |
---|
597 | //print("test_number_format_Grouping() start.............."); |
---|
598 | //primary grouping |
---|
599 | var sourceInput = 123456789; |
---|
600 | var expectResult = "12,34,56,789"; |
---|
601 | var options = {pattern:"#,##,###",locale:"en-us"}; |
---|
602 | |
---|
603 | //step1: 123456789 formated=> 12,34,56,789 |
---|
604 | //step2:12,34,56,789 parsed=> 123456789 => formated => 12,34,56,789 |
---|
605 | tests.number.checkFormatParseCycle(t, options,sourceInput,expectResult,true); |
---|
606 | |
---|
607 | //TODO: sencondary grouping not implemented yet ? |
---|
608 | //Pattern "#,###" and secondaryGroupingSize=4 should format 123456789 to "12,3456,789" |
---|
609 | |
---|
610 | //Special case for "en-in" locale |
---|
611 | //1876543210 should be formated as 1,87,65,43,210 in "en-in" (India) |
---|
612 | /* |
---|
613 | sourceInput = 1876543210; |
---|
614 | expectResult = "1,87,65,43,210"; |
---|
615 | var result = number.format(sourceInput,{locale:"en-in"}); |
---|
616 | t.is(expectResult,result); |
---|
617 | */ |
---|
618 | //print("test_number_format_Grouping() end..............\n"); |
---|
619 | } |
---|
620 | }, |
---|
621 | { |
---|
622 | name: "format_pad", |
---|
623 | runTest: function(t){ |
---|
624 | /** |
---|
625 | * TODO:!!Failed cases: |
---|
626 | * According to ICU4J test criteria: |
---|
627 | * 1.with pattern "*^##.##": |
---|
628 | * 0 should be formatted to "^^^^0",but got "*^0" instead, |
---|
629 | * -1.3 should be formatted to "^-1.3",but got "-*^1.3" instead. |
---|
630 | * |
---|
631 | * 2.with pattern "##0.0####*_ 'g-m/s^2'" : |
---|
632 | * 0 should be formatted to "0.0______ g-m/s^2",but got ":0.0*_ 'g-m/s^2'" instead |
---|
633 | * 1.0/3 should be formatted to "0.33333__ g-m/s^2",but got "0.33333*_ 'g-m/s^2'" instead |
---|
634 | * |
---|
635 | * 3.with pattern "*x#,###,###,##0.0#;*x(###,###,##0.0#)": |
---|
636 | * -10 should be formatted to "xxxxxxxxxx(10.0)",but got "*x(10.0)" instead. |
---|
637 | * 10 should be formatted to "xxxxxxxxxxxx10.0",but got "*x10.0" instead. |
---|
638 | * ...... |
---|
639 | * -1120456.37 should be formatted to "xx(1,120,456.37)",but got "*x(1,120,456.37)" instead. |
---|
640 | * 1120456.37 should be formatted to "xxxx1,120,456.37",but got "*x1,120,456.37" instead. |
---|
641 | * -1252045600.37 should be formatted to "(1,252,045,600.37)",but got "*x(1,252,045,600.37)" instead. |
---|
642 | * 1252045600.37 should be formatted to "10,252,045,600.37",but got "*x10,252,045,600.37" instead. |
---|
643 | * |
---|
644 | * 4.with pattern "#,###,###,##0.0#*x;(###,###,##0.0#*x)" |
---|
645 | * -10 should be formatted to (10.0xxxxxxxxxx),but got "(10.0*x)" instead. |
---|
646 | * 10 should be formatted to "10.0xxxxxxxxxxxx",but got "10.0*x" instead. |
---|
647 | * ...... |
---|
648 | * -1120456.37 should be formatted to "(1,120,456.37xx)",but got "(1,120,456.37*x)" instead. |
---|
649 | * 1120456.37 should be formatted to "xxxx1,120,456.37",but got "1,120,456.37*x" instead. |
---|
650 | * -1252045600.37 should be formatted to "(1,252,045,600.37)",but got "(1,252,045,600.37*x)" instead. |
---|
651 | * 1252045600.37 should be formatted to ""10,252,045,600.37"",but got "10,252,045,600.37*x" instead.* |
---|
652 | * |
---|
653 | * Refer to ICU4J's NumberFormatTest.TestPad() |
---|
654 | */ |
---|
655 | /* |
---|
656 | function test_number_format_pad(){ |
---|
657 | var locale = "en-us"; |
---|
658 | print("test_number_format_Pad() start.............."); |
---|
659 | var options = {pattern:"*^##.##",locale:locale}; |
---|
660 | |
---|
661 | tests.number.check(t, options,0,"^^^^0"); |
---|
662 | tests.number.check(t, options,-1.3,"^-1.3"); |
---|
663 | |
---|
664 | |
---|
665 | options = {pattern:"##0.0####*_ 'g-m/s^2'",locale:locale}; |
---|
666 | tests.number.check(t, options,0,"0.0______ g-m/s^2"); |
---|
667 | tests.number.checkFormatParseCycle(t, options,1.0/3,"0.33333__ g-m/s^2",true); |
---|
668 | |
---|
669 | //exponent not implemented |
---|
670 | //options = {pattern:"##0.0####E0*_ 'g-m/s^2'",locale:locale}; |
---|
671 | //tests.number.check(t, options,0,"0.0E0______ g-m/s^2"); |
---|
672 | //tests.number.checkFormatParseCycle(t, options,1.0/3,"333.333E-3_ g-m/s^2",true); |
---|
673 | |
---|
674 | // Test padding before a sign |
---|
675 | options = {pattern:"*x#,###,###,##0.0#;*x(###,###,##0.0#)",locale:locale}; |
---|
676 | |
---|
677 | tests.number.check(t, options,-10,"xxxxxxxxxx(10.0)"); |
---|
678 | tests.number.check(t, options,-1000, "xxxxxxx(1,000.0)"); |
---|
679 | tests.number.check(t, options,-1000000, "xxx(1,000,000.0)"); |
---|
680 | tests.number.check(t, options,-100.37, "xxxxxxxx(100.37)"); |
---|
681 | tests.number.check(t, options,-10456.37, "xxxxx(10,456.37)"); |
---|
682 | tests.number.check(t, options,-1120456.37, "xx(1,120,456.37)"); |
---|
683 | tests.number.check(t, options,-112045600.37, "(112,045,600.37)"); |
---|
684 | tests.number.check(t, options,-1252045600.37, "(1,252,045,600.37)"); |
---|
685 | |
---|
686 | |
---|
687 | tests.number.check(t, options,10, "xxxxxxxxxxxx10.0"); |
---|
688 | tests.number.check(t, options,1000, "xxxxxxxxx1,000.0"); |
---|
689 | tests.number.check(t, options,1000000, "xxxxx1,000,000.0"); |
---|
690 | tests.number.check(t, options,100.37, "xxxxxxxxxx100.37"); |
---|
691 | tests.number.check(t, options,10456.37, "xxxxxxx10,456.37"); |
---|
692 | tests.number.check(t, options,1120456.37, "xxxx1,120,456.37"); |
---|
693 | tests.number.check(t, options,112045600.37, "xx112,045,600.37"); |
---|
694 | tests.number.check(t, options,10252045600.37, "10,252,045,600.37"); |
---|
695 | |
---|
696 | // Test padding between a sign and a number |
---|
697 | options = {pattern:"#,###,###,##0.0#*x;(###,###,##0.0#*x)",locale:locale}; |
---|
698 | tests.number.check(t, options, -10, "(10.0xxxxxxxxxx)"); |
---|
699 | tests.number.check(t, options, -1000, "(1,000.0xxxxxxx)"); |
---|
700 | tests.number.check(t, options, -1000000, "(1,000,000.0xxx)"); |
---|
701 | tests.number.check(t, options, -100.37, "(100.37xxxxxxxx)"); |
---|
702 | tests.number.check(t, options, -10456.37, "(10,456.37xxxxx)"); |
---|
703 | tests.number.check(t, options, -1120456.37, "(1,120,456.37xx)"); |
---|
704 | tests.number.check(t, options, -112045600.37, "(112,045,600.37)"); |
---|
705 | tests.number.check(t, options, -1252045600.37, "(1,252,045,600.37)"); |
---|
706 | |
---|
707 | tests.number.check(t, options, 10, "10.0xxxxxxxxxxxx"); |
---|
708 | tests.number.check(t, options, 1000, "1,000.0xxxxxxxxx"); |
---|
709 | tests.number.check(t, options, 1000000, "1,000,000.0xxxxx"); |
---|
710 | tests.number.check(t, options, 100.37, "100.37xxxxxxxxxx"); |
---|
711 | tests.number.check(t, options, 10456.37, "10,456.37xxxxxxx"); |
---|
712 | tests.number.check(t, options, 1120456.37, "1,120,456.37xxxx"); |
---|
713 | tests.number.check(t, options, 112045600.37, "112,045,600.37xx"); |
---|
714 | tests.number.check(t, options, 10252045600.37, "10,252,045,600.37"); |
---|
715 | |
---|
716 | //Not implemented yet,refer to NumberFormatTest.TestPatterns2() |
---|
717 | //For future use - maily test pad patterns |
---|
718 | print("test_number_format_Pad() end.............."); |
---|
719 | } |
---|
720 | */ |
---|
721 | } |
---|
722 | }, |
---|
723 | { |
---|
724 | name: "parse_icu4j3_6", |
---|
725 | runTest: function(t){ |
---|
726 | /** |
---|
727 | * In ICU4J, testing logic for NumberFormat.parse() is seperated into |
---|
728 | * differernt single tese cases. So part of these logic are |
---|
729 | * collected together in this test case. * |
---|
730 | */ |
---|
731 | //print("test_number_parse_icu4j3_6() start.............."); |
---|
732 | //Refer to ICU4J's NumberFormatTest.TestParse() which is only a rudimentary version |
---|
733 | var pattern = "00"; |
---|
734 | var str = "0.0"; |
---|
735 | var result = number.parse(str,{pattern:pattern, locale: 'en-us'}); |
---|
736 | //TODO: add more locales |
---|
737 | //FIXME: is this a valid test? |
---|
738 | // t.is(0,result); |
---|
739 | |
---|
740 | /**************************************** tolerant parse ***************************************** |
---|
741 | * refers to ICU4J's NumberFormatTest.TestStrictParse()?? |
---|
742 | * TODO: Seems dojo.number parses string in a tolerant way. |
---|
743 | */ |
---|
744 | var options = {locale:"en-us"}; |
---|
745 | /* |
---|
746 | * TODO: !!Failed case,Should all pass, |
---|
747 | * but the following elements failed (all parsed to NaN): |
---|
748 | * [1]-"0 ",[2]-"0.",[3]-"0,",[5]-"0. ",[6]-"0.100,5", |
---|
749 | * [7]-".00",[9]-"12345, ",[10]-"1,234, ",[12]-"0E" |
---|
750 | */ |
---|
751 | var passData = ([ |
---|
752 | "0", //[0] single zero before end of text is not leading |
---|
753 | //"0 ", //[1] single zero at end of number is not leading |
---|
754 | //"0.", //[2] single zero before period (or decimal, it's ambiguous) is not leading |
---|
755 | //"0,", //[3] single zero before comma (not group separator) is not leading |
---|
756 | "0.0", //[4] single zero before decimal followed by digit is not leading |
---|
757 | //"0. ", //[5] same as above before period (or decimal) is not leading |
---|
758 | //"0.100,5", //[6] comma stops parse of decimal (no grouping) |
---|
759 | //".00", //[7] leading decimal is ok, even with zeros |
---|
760 | "1234567", //[8] group separators are not required |
---|
761 | //"12345, ", //[9] comma not followed by digit is not a group separator, but end of number |
---|
762 | //"1,234, ", //[10] if group separator is present, group sizes must be appropriate |
---|
763 | "1,234,567" //[11] ...secondary too |
---|
764 | //,"0E" //[12]not implemented yet,an exponnent not followed by zero or digits is not an exponent |
---|
765 | ]); |
---|
766 | runBatchParse(options,passData,true/*tolerant parse*/); |
---|
767 | |
---|
768 | /* |
---|
769 | * TODO:!!Failed case,should all pass, |
---|
770 | * but the following failed, |
---|
771 | * [10]-"1,45 that" implies that we partially parse input |
---|
772 | */ |
---|
773 | var failData = ([ |
---|
774 | // leading zeros without separators are tolerated #6933 |
---|
775 | // "00", //[0] leading zero before zero |
---|
776 | // "012", //[1] leading zero before digit |
---|
777 | "0,456", //[2] leading zero before group separator |
---|
778 | "1,2", //[3] wrong number of digits after group separator |
---|
779 | ",0", //[4] leading group separator before zero |
---|
780 | ",1", //[5] leading group separator before digit |
---|
781 | ",.02", //[6] leading group separator before decimal |
---|
782 | "1,.02", //[7] group separator before decimal |
---|
783 | "1,,200", //[8] multiple group separators |
---|
784 | "1,45", //[9] wrong number of digits in primary group |
---|
785 | //"1,45 that", //[10] wrong number of digits in primary group |
---|
786 | "1,45.34", //[11] wrong number of digits in primary group |
---|
787 | "1234,567", //[12] wrong number of digits in secondary group |
---|
788 | "12,34,567", //[13] wrong number of digits in secondary group |
---|
789 | "1,23,456,7890" //[14] wrong number of digits in primary and secondary groups |
---|
790 | ]); |
---|
791 | runBatchParse(options,failData,false); |
---|
792 | |
---|
793 | options = {pattern:"#,##,##0.#",locale:"en-us"}; |
---|
794 | /* |
---|
795 | * TODO:!!Failed case,shoudl all pass. |
---|
796 | |
---|
797 | * but [1] [2] and [3] failed |
---|
798 | * should be parsed to 1234567,but NaN instead |
---|
799 | */ |
---|
800 | var mixedPassData = ([ |
---|
801 | "12,34,567" //[0] |
---|
802 | //,"12,34,567," //[1] |
---|
803 | //"12,34,567, that",//[2] |
---|
804 | //"12,34,567 that" //[3] |
---|
805 | ]); |
---|
806 | runBatchParse(options,mixedPassData,true/*tolerant parse*/); |
---|
807 | |
---|
808 | /* |
---|
809 | * TODO:!!Failed case,should all pass, |
---|
810 | * but actually mixedFailData[2] and mixedFailData[3] passed. |
---|
811 | * "12,34,56, that " and [3]-"12,34,56 that" should be parsed to 123456,but NaN instead |
---|
812 | */ |
---|
813 | var mixedFailData = ([ |
---|
814 | "12,34,56", //[0] |
---|
815 | "12,34,56," //[1] |
---|
816 | //,"12,34,56, that ",//[2] |
---|
817 | //"12,34,56 that", //[3] |
---|
818 | ]); |
---|
819 | runBatchParse(options,mixedFailData,false); |
---|
820 | |
---|
821 | |
---|
822 | /**************************************** strict parse ****************************************** |
---|
823 | * TODO:May need to test strict parsing in the future? |
---|
824 | * e.g. A strict parsing like (with pattern "#,##0.#") |
---|
825 | * 1.Leading zeros |
---|
826 | * '00', '0123' fail the parse, but '0' and '0.001' pass |
---|
827 | * 2.Leading or doubled grouping separators |
---|
828 | * ',123' and '1,,234" fail |
---|
829 | * 3.Groups of incorrect length when grouping is used |
---|
830 | * '1,23' and '1234,567' fail, but '1234' passes |
---|
831 | * 4.Grouping separators used in numbers followed by exponents |
---|
832 | * '1,234E5' fails, but '1234E5' and '1,234E' pass |
---|
833 | */ |
---|
834 | //options={locale:"en",strict:true}; |
---|
835 | //runBatchParse(options,passData,false/*strict parse*/); |
---|
836 | //runBatchParse(options,failData,false/*strict parse*/); |
---|
837 | |
---|
838 | //options = {pattern:"#,##,##0.#",locale:"en-us",strict:true}; |
---|
839 | //runBatchParse(options,mixedPassData,false/*strict parse*/); |
---|
840 | //runBatchParse(options,mixedFailData,false/*strict parse*/); |
---|
841 | |
---|
842 | //print("test_number_parse_icu4j3_6() end..............\n"); |
---|
843 | } |
---|
844 | }, |
---|
845 | { |
---|
846 | name: "parse_whitespace", |
---|
847 | runTest: function(t){ |
---|
848 | /** |
---|
849 | * TODO:!!Failed case |
---|
850 | * With pattern "a b#0c ",both "a b3456c " and and "a b1234c " should be parsed to 3456,but got NaN instead. |
---|
851 | * |
---|
852 | * Refer to ICU4J's NumberFormatTest.TestWhiteSpaceParsing |
---|
853 | */ |
---|
854 | /* |
---|
855 | print("test_number_parse_WhiteSpace() start.............."); |
---|
856 | var pattern = "a b#0c "; |
---|
857 | var expectResult = 3456; |
---|
858 | result = number.parse("a b3456c ",{pattern:pattern,locale:"en-us"}); |
---|
859 | t.is(expectResult,result); |
---|
860 | result = number.parse("a b3456c ",{pattern:pattern,locale:"en-us"}); |
---|
861 | t.is(expectResult,result); |
---|
862 | print("test_number_parse_WhiteSpace() end..............\n"); |
---|
863 | */ |
---|
864 | } |
---|
865 | }, |
---|
866 | /************************************************************************************************* |
---|
867 | * Regression test cases |
---|
868 | * These test cases are referred to ICU4J's NumberFormatRegressionTest and NumberFormatRegression. |
---|
869 | * The regression cases in ICU4J are used as unit test cases for bug fixing, |
---|
870 | * They are inluced here so that dojo.number may avoid those similar bugs. |
---|
871 | *************************************************************************************************/ |
---|
872 | { |
---|
873 | name: "number_regression_1", |
---|
874 | runTest: function(t){ |
---|
875 | /** |
---|
876 | * Refer to ICU4J's NumberFormatRegressionTest.Test4161100() |
---|
877 | */ |
---|
878 | tests.number.checkFormatParseCycle(t, {pattern:"#0.#", locale: 'en-us'},-0.09,"-0.1",false); |
---|
879 | } |
---|
880 | }, |
---|
881 | { |
---|
882 | name: "number_regression_2", |
---|
883 | runTest: function(t){ |
---|
884 | /** |
---|
885 | * !!Failed case,rounding hasn't been implemented yet. |
---|
886 | * Refer to ICU4J's NumberFormatRegressionTest.Test4408066() |
---|
887 | */ |
---|
888 | /* |
---|
889 | var data = ([-3.75, -2.5, -1.5, |
---|
890 | -1.25, 0, 1.0, |
---|
891 | 1.25, 1.5, 2.5, |
---|
892 | 3.75, 10.0, 255.5]); |
---|
893 | var expected = (["-4", "-2", "-2", |
---|
894 | "-1", "0", "1", |
---|
895 | "1", "2", "2", |
---|
896 | "4", "10", "256"]); |
---|
897 | var options = {locale:"zh-cn",round:true}; |
---|
898 | for(var i =0; i < data.length; i++){ |
---|
899 | tests.number.checkFormatParseCycle(t, options,data[i],expected[i],false); |
---|
900 | } |
---|
901 | |
---|
902 | data = ([ "-3.75", "-2.5", "-1.5", |
---|
903 | "-1.25", "0", "1.0", |
---|
904 | "1.25", "1.5", "2.5", |
---|
905 | "3.75", "10.0", "255.5"]); |
---|
906 | expected =([ -3, -2, -1, |
---|
907 | -1, 0, 1, |
---|
908 | 1, 1, 2, |
---|
909 | 3, 10, 255]); |
---|
910 | |
---|
911 | for(var i =0; i < data.length; i++){ |
---|
912 | tests.number.checkParse(t, options,data[i],expected[i]); |
---|
913 | } |
---|
914 | */ |
---|
915 | } |
---|
916 | }, |
---|
917 | { |
---|
918 | name: "number_regression_3", |
---|
919 | runTest: function(t){ |
---|
920 | /** |
---|
921 | * Refer to ICU4J's NumberRegression.Test4087535() and Test4243108() |
---|
922 | */ |
---|
923 | tests.number.checkFormatParseCycle(t, {places:0},0,"0",false); |
---|
924 | //TODO:in icu4j,0.1 should be formatted to ".1" when minimumIntegerDigits=0 |
---|
925 | tests.number.checkFormatParseCycle(t, {places:0},0.1,"0",false); |
---|
926 | tests.number.checkParse(t, {pattern:"#0.#####", locale: 'en-us'},123.55456,123.55456); |
---|
927 | //!! fails because default pattern only has 3 decimal places |
---|
928 | // tests.number.checkParse(t, null,123.55456,123.55456); |
---|
929 | |
---|
930 | //See whether it fails first format 0.0 ,parse "99.99",and then reformat 0.0 |
---|
931 | tests.number.checkFormatParseCycle(t, {pattern:"#.#"},0.0,"0",false); |
---|
932 | tests.number.checkParse(t, {locale: 'en-us'},"99.99",99.99); |
---|
933 | tests.number.checkFormatParseCycle(t, {pattern:"#.#"},0.0,"0",false); |
---|
934 | } |
---|
935 | }, |
---|
936 | { |
---|
937 | name: "number_regression_4", |
---|
938 | runTest: function(t){ |
---|
939 | /** |
---|
940 | * TODO: |
---|
941 | * In ICU -0.0 and -0.0001 should be formatted to "-0" with FieldPosition(0) |
---|
942 | * dojo.i18n.number format -0.0 to "-0"; -0.0001 to "-0.000100" |
---|
943 | * |
---|
944 | * Refer to ICU4J's NumberRegression.Test4088503() and Test4106658() |
---|
945 | */ |
---|
946 | tests.number.checkFormatParseCycle(t, {places:0},123,"123",false); |
---|
947 | |
---|
948 | //TODO: differernt from ICU where -0.0 is formatted to "-0" |
---|
949 | tests.number.checkFormatParseCycle(t, {locale:"en-us"},-0.0,"0",false); |
---|
950 | |
---|
951 | //TODO: differernt from ICU where -0.0001 is formatted to "-0" |
---|
952 | tests.number.checkFormatParseCycle(t, {locale:"en-us",places:6},-0.0001,"-0.000100",false); |
---|
953 | } |
---|
954 | }, |
---|
955 | { |
---|
956 | name: "number_regression_5", |
---|
957 | runTest: function(t){ |
---|
958 | /** |
---|
959 | * !!Failed case,rounding has not implemented yet. |
---|
960 | * 0.00159999 should be formatted as 0.0016 but got 0.0015 instead. |
---|
961 | * Refer to ICU4J's NumberRegression.Test4071492() |
---|
962 | */ |
---|
963 | //tests.number.checkFormatParseCycle(t, {places:4,round:true},0.00159999,"0.0016",false); |
---|
964 | } |
---|
965 | }, |
---|
966 | { |
---|
967 | name: "number_regression_6", |
---|
968 | runTest: function(t){ |
---|
969 | /** |
---|
970 | * Refer to ICU4J's NumberRegression.Test4086575() |
---|
971 | */ |
---|
972 | var pattern = "###.00;(###.00)"; |
---|
973 | var locale = "fr"; |
---|
974 | var options = {pattern:pattern,locale:locale}; |
---|
975 | |
---|
976 | //no group separator |
---|
977 | tests.number.checkFormatParseCycle(t, options,1234,"1234,00",false); |
---|
978 | tests.number.checkFormatParseCycle(t, options,-1234,"(1234,00)",false); |
---|
979 | |
---|
980 | //space as group separator |
---|
981 | pattern = "#,###.00;(#,###.00)"; |
---|
982 | options = {pattern:pattern,locale:locale}; |
---|
983 | tests.number.checkFormatParseCycle(t, options,1234,"1\u00a0234,00",false);// Expect 1 234,00 |
---|
984 | tests.number.checkFormatParseCycle(t, options,-1234,"(1\u00a0234,00)",false); // Expect (1 234,00) |
---|
985 | } |
---|
986 | }, |
---|
987 | { |
---|
988 | name: "number_regression_7", |
---|
989 | runTest: function(t){ |
---|
990 | /** |
---|
991 | * !!Failed case - expontent has not implemented yet |
---|
992 | * shuold format 1.000000000000001E7 to 10000000.00000001, but got 10,000,000.000 instead |
---|
993 | * Refer to ICU4J's NumberRegression.Test4090489() - loses precision |
---|
994 | */ |
---|
995 | //tests.number.checkFormatParseCycle(t, null,1.000000000000001E7,"10000000.00000001",false); |
---|
996 | } |
---|
997 | }, |
---|
998 | { |
---|
999 | name: "number_regression_8", |
---|
1000 | runTest: function(t){ |
---|
1001 | /** |
---|
1002 | * !!Failed case |
---|
1003 | * 1.with pattern "#,#00.00 p''ieces;-#,#00.00 p''ieces" |
---|
1004 | * 3456.78 should be formated to "3,456.78 p'ieces", |
---|
1005 | * but got "3,456.78 p''ieces","''" should be replaced with "'" |
---|
1006 | * 2.with illegal pattern "000.0#0" |
---|
1007 | * no error for the illegal pattern, and 3456.78 is formatted to 456.780 |
---|
1008 | * 3.with illegal pattern "0#0.000" |
---|
1009 | * no error for the illegal pattern, and 3456.78 is formatted to 3456.780 |
---|
1010 | * |
---|
1011 | * Refer to ICU4J's NumberRegression.Test4092480(),Test4074454() |
---|
1012 | */ |
---|
1013 | var patterns = (["#0000","#000","#00","#0","#"]); |
---|
1014 | var expect = (["0042","042","42","42","42"]); |
---|
1015 | |
---|
1016 | for(var i =0; i < patterns.length; i ++){ |
---|
1017 | tests.number.checkFormatParseCycle(t, {pattern:patterns[i]},42,expect[i],false); |
---|
1018 | tests.number.checkFormatParseCycle(t, {pattern:patterns[i]},-42,"-"+expect[i],false); |
---|
1019 | } |
---|
1020 | |
---|
1021 | tests.number.checkFormatParseCycle(t, {pattern:"#,#00.00;-#.#", locale: 'en-us'},3456.78,"3,456.78",false); |
---|
1022 | //!!Failed case |
---|
1023 | //tests.number.checkFormatParseCycle(t, {pattern:"#,#00.00 p''ieces;-#,#00.00 p''ieces"},3456.78,"3,456.78 p'ieces",false); |
---|
1024 | //tests.number.checkFormatParseCycle(t, {pattern:"000.0#0"},3456.78,null,false); |
---|
1025 | //tests.number.checkFormatParseCycle(t, {pattern:"0#0.000"},3456.78,null,false); |
---|
1026 | } |
---|
1027 | }, |
---|
1028 | { |
---|
1029 | name: "number_regression_9", |
---|
1030 | runTest: function(t){ |
---|
1031 | /** |
---|
1032 | * TODO |
---|
1033 | * Refer to ICU4J's NumberRegression.Test4052223() |
---|
1034 | */ |
---|
1035 | //TODO:only got NaN,need an illegal pattern exception? |
---|
1036 | tests.number.checkParse(t, {pattern:"#,#00.00"},"abc3"); |
---|
1037 | |
---|
1038 | //TODO: got NaN instead of 1.222, is it ok? |
---|
1039 | //tests.number.checkParse(t, {pattern:"#,##0.###",locale:"en-us"},"1.222,111",1.222); |
---|
1040 | //tests.number.checkParse(t, {pattern:"#,##0.###",locale:"en-us"},"1.222x111",1.222); |
---|
1041 | |
---|
1042 | //got NaN for illeal input,ok |
---|
1043 | tests.number.checkParse(t, null,"hello: ,.#$@^&**10x"); |
---|
1044 | } |
---|
1045 | }, |
---|
1046 | { |
---|
1047 | name: "number_regression_10", |
---|
1048 | runTest: function(t){ |
---|
1049 | /** |
---|
1050 | * Refer to ICU4J's NumberRegression.Test4125885() |
---|
1051 | */ |
---|
1052 | tests.number.checkFormatParseCycle(t, {pattern:"000.00", locale: 'en-us'},12.34,"012.34",false); |
---|
1053 | tests.number.checkFormatParseCycle(t, {pattern:"+000.00%;-000.00%", locale: 'en-us'},0.1234,"+012.34%",false); |
---|
1054 | tests.number.checkFormatParseCycle(t, {pattern:"##,###,###.00", locale: 'en-us'},9.02,"9.02",false); |
---|
1055 | |
---|
1056 | var patterns =(["#.00", "0.00", "00.00", "#0.0#", "#0.00"]); |
---|
1057 | var expect = (["1.20", "1.20", "01.20", "1.2", "1.20" ]); |
---|
1058 | for(var i =0 ; i < patterns.length; i ++){ |
---|
1059 | tests.number.checkFormatParseCycle(t, {pattern:patterns[i], locale: 'en-us'},1.2,expect[i],false); |
---|
1060 | } |
---|
1061 | } |
---|
1062 | }, |
---|
1063 | { |
---|
1064 | name: "number_regression_11", |
---|
1065 | runTest: function(t){ |
---|
1066 | /** |
---|
1067 | * TODO:!!Failed case |
---|
1068 | * Make sure that all special characters, when quoted in a suffix or prefix, lose their special meaning. |
---|
1069 | * The detail error info : |
---|
1070 | * for input 123 |
---|
1071 | * pattern:'0'#0'0'; expect:"01230"; but got "'3'#0'0'" instead |
---|
1072 | * pattern:','#0','; expect:",123,"; but got "','123','" instead |
---|
1073 | * pattern:'.'#0'.'; expect:".123."; but got "'.'123'.'" instead |
---|
1074 | * pattern:'â°'#0'â°'; expect:"â°123â°"; but got "'â°'123000'â°'" instead |
---|
1075 | * pattern:'%'#0'%'; expect:"%123%"; but got "'%'12300'%'" instead |
---|
1076 | * pattern:'#'#0'#'; expect:"#123#"; but got "'123'#0'#'" instead |
---|
1077 | * pattern:';'#0';'; expect:";123;"; but got "[dojo-test] FATAL exception raised: |
---|
1078 | * unable to find a number expression in pattern: '" |
---|
1079 | * pattern:'E'#0'E'; expect:"E123E"; not implemeted yet |
---|
1080 | * pattern:'*'#0'*'; expect:"*123*"; but got "'*'123'*'" instead |
---|
1081 | * pattern:'+'#0'+'; expect:"+123+"; but got "'+'123'+'" instead |
---|
1082 | * pattern:'-'#0'-'; expect:"-123-"; but got "'-'123'-'" instead |
---|
1083 | * |
---|
1084 | * TODO: is it ok to remain "'" in the formatted result as above?? |
---|
1085 | * |
---|
1086 | * Refer to ICU4J's NumberRegression.Test4212072() |
---|
1087 | */ |
---|
1088 | /* |
---|
1089 | var specials = ([ '0', ',', '.', '\u2030', '%', '#',';', 'E', '*', '+', '-']); |
---|
1090 | var pattern; |
---|
1091 | var expect; |
---|
1092 | |
---|
1093 | for(var i=0; i < specials.length; i ++){ |
---|
1094 | pattern = "'" + specials[i] + "'#0'" + specials[i] + "'"; |
---|
1095 | expect = "" + specials[i] + "123" + specials[i]; |
---|
1096 | tests.number.checkFormatParseCycle(t, {pattern:pattern,locale:"en-us"},123,expect,false); |
---|
1097 | } |
---|
1098 | */ |
---|
1099 | } |
---|
1100 | }, |
---|
1101 | { |
---|
1102 | name: "number_regression_12", |
---|
1103 | runTest: function(t){ |
---|
1104 | /** |
---|
1105 | * TODO: add more rounding test cases, refer to ICU4J's NumberRegression.Test4071005(),Test4071014() etc.. |
---|
1106 | */ |
---|
1107 | |
---|
1108 | /** |
---|
1109 | * TODO:Decimal format doesnt round a double properly when the number is less than 1 |
---|
1110 | * |
---|
1111 | * Refer to ICU4J's NumberRegression.test4241880() |
---|
1112 | */ |
---|
1113 | /* |
---|
1114 | var input = ([ .019, .009, .015, .016, .014, |
---|
1115 | .004, .005, .006, .007, .008, |
---|
1116 | .5, 1.5, .05, .15, .005, |
---|
1117 | .015, .0005, .0015]); |
---|
1118 | var patterns = (["##0%", "##0%", "##0%", "##0%", "##0%", |
---|
1119 | "##0%", "##0%", "##0%", "##0%", "##0%", |
---|
1120 | "#,##0", "#,##0", "#,##0.0", "#,##0.0", "#,##0.00", |
---|
1121 | "#,##0.00", "#,##0.000", "#,##0.000"]); |
---|
1122 | var expect =([ "2%", "1%", "2%", "2%", "1%", |
---|
1123 | "0%", "0%", "1%", "1%", "1%", |
---|
1124 | "0", "2", "0.0", "0.2", "0.00", |
---|
1125 | "0.02", "0.000", "0.002"]); |
---|
1126 | for(var i = 0; i <input.length; i ++){ |
---|
1127 | tests.number.checkFormatParseCycle(t, {pattern:patterns[i],round:true},input[i],expect[i],false); |
---|
1128 | } |
---|
1129 | */ |
---|
1130 | } |
---|
1131 | } |
---|
1132 | ] |
---|
1133 | ); |
---|
1134 | }); |
---|