|
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 Number.prototype.toLocaleString throws the same exceptions as Intl.NumberFormat. |
|
6 * @author Norbert Lindenberg |
|
7 */ |
|
8 |
|
9 var locales = [null, [NaN], ["i"], ["de_DE"]]; |
|
10 var options = [ |
|
11 {localeMatcher: null}, |
|
12 {style: "invalid"}, |
|
13 {style: "currency"}, |
|
14 {style: "currency", currency: "ßP"}, |
|
15 {maximumSignificantDigits: -Infinity} |
|
16 ]; |
|
17 |
|
18 locales.forEach(function (locales) { |
|
19 var referenceError, error; |
|
20 try { |
|
21 var format = new Intl.NumberFormat(locales); |
|
22 } catch (e) { |
|
23 referenceError = e; |
|
24 } |
|
25 if (referenceError === undefined) { |
|
26 $ERROR("Internal error: Expected exception was not thrown by Intl.NumberFormat for locales " + locales + "."); |
|
27 } |
|
28 |
|
29 try { |
|
30 var result = (0).toLocaleString(locales); |
|
31 } catch (e) { |
|
32 error = e; |
|
33 } |
|
34 if (error === undefined) { |
|
35 $ERROR("Number.prototype.toLocaleString didn't throw exception for locales " + locales + "."); |
|
36 } else if (error.name !== referenceError.name) { |
|
37 $ERROR("Number.prototype.toLocaleString threw exception " + error.name + |
|
38 " for locales " + locales + "; expected " + referenceError.name + "."); |
|
39 } |
|
40 }); |
|
41 |
|
42 options.forEach(function (options) { |
|
43 var referenceError, error; |
|
44 try { |
|
45 var format = new Intl.NumberFormat([], options); |
|
46 } catch (e) { |
|
47 referenceError = e; |
|
48 } |
|
49 if (referenceError === undefined) { |
|
50 $ERROR("Internal error: Expected exception was not thrown by Intl.NumberFormat for options " + |
|
51 JSON.stringify(options) + "."); |
|
52 } |
|
53 |
|
54 try { |
|
55 var result = (0).toLocaleString([], options); |
|
56 } catch (e) { |
|
57 error = e; |
|
58 } |
|
59 if (error === undefined) { |
|
60 $ERROR("Number.prototype.toLocaleString didn't throw exception for options " + |
|
61 JSON.stringify(options) + "."); |
|
62 } else if (error.name !== referenceError.name) { |
|
63 $ERROR("Number.prototype.toLocaleString threw exception " + error.name + |
|
64 " for options " + JSON.stringify(options) + "; expected " + referenceError.name + "."); |
|
65 } |
|
66 }); |
|
67 |