|
1 // Copyright 2012 Mozilla Corporation. All rights reserved. |
|
2 // This code is governed by the BSD license found in the LICENSE file. |
|
3 |
|
4 /** |
|
5 * @description Tests that the option currency is processed correctly. |
|
6 * @author Norbert Lindenberg |
|
7 */ |
|
8 |
|
9 var validValues = ["CNY", "USD", "EUR", "IDR", "jpy", {toString: function () {return "INR";}}]; |
|
10 var invalidValues = ["$", "SFr.", "US$", "ßP", {toString: function () {return;}}]; |
|
11 |
|
12 var defaultLocale = new Intl.NumberFormat().resolvedOptions().locale; |
|
13 |
|
14 validValues.forEach(function (value) { |
|
15 var format, actual, expected; |
|
16 |
|
17 // with currency style, we should get the upper case form back |
|
18 format = new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value}); |
|
19 actual = format.resolvedOptions().currency; |
|
20 expected = value.toString().toUpperCase(); |
|
21 if (actual !== expected) { |
|
22 $ERROR("Incorrect resolved currency with currency style - expected " + |
|
23 expected + "; got " + actual + "."); |
|
24 } |
|
25 |
|
26 // without currency style, we shouldn't get any currency back |
|
27 format = new Intl.NumberFormat([defaultLocale], {currency: value}); |
|
28 actual = format.resolvedOptions().currency; |
|
29 expected = undefined; |
|
30 if (actual !== expected) { |
|
31 $ERROR("Incorrect resolved currency with non-currency style - expected " + |
|
32 expected + "; got " + actual + "."); |
|
33 } |
|
34 |
|
35 // currencies specified through the locale must be ignored |
|
36 format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value}); |
|
37 actual = format.resolvedOptions().currency; |
|
38 expected = value.toString().toUpperCase(); |
|
39 if (actual !== expected) { |
|
40 $ERROR("Incorrect resolved currency with -u-cu- and currency style - expected " + |
|
41 expected + "; got " + actual + "."); |
|
42 } |
|
43 |
|
44 format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value}); |
|
45 actual = format.resolvedOptions().currency; |
|
46 expected = undefined; |
|
47 if (actual !== expected) { |
|
48 $ERROR("Incorrect resolved currency with -u-cu- and non-currency style - expected " + |
|
49 expected + "; got " + actual + "."); |
|
50 } |
|
51 }); |
|
52 |
|
53 invalidValues.forEach(function (value) { |
|
54 function expectError(f) { |
|
55 var error; |
|
56 try { |
|
57 f(); |
|
58 } catch (e) { |
|
59 error = e; |
|
60 } |
|
61 if (error === undefined) { |
|
62 $ERROR("Invalid currency value " + value + " was not rejected."); |
|
63 } else if (error.name !== "RangeError") { |
|
64 $ERROR("Invalid currency value " + value + " was rejected with wrong error " + error.name + "."); |
|
65 } |
|
66 } |
|
67 |
|
68 expectError(function () { |
|
69 return new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value}); |
|
70 }); |
|
71 expectError(function () { |
|
72 return new Intl.NumberFormat([defaultLocale], {currency: value}); |
|
73 }); |
|
74 expectError(function () { |
|
75 return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value}); |
|
76 }); |
|
77 expectError(function () { |
|
78 return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value}); |
|
79 }); |
|
80 }); |
|
81 |