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