michael@0: // Copyright 2012 Mozilla Corporation. All rights reserved. michael@0: // This code is governed by the BSD license found in the LICENSE file. michael@0: michael@0: /** michael@0: * @description Tests that the option currency is processed correctly. michael@0: * @author Norbert Lindenberg michael@0: */ michael@0: michael@0: var validValues = ["CNY", "USD", "EUR", "IDR", "jpy", {toString: function () {return "INR";}}]; michael@0: var invalidValues = ["$", "SFr.", "US$", "ßP", {toString: function () {return;}}]; michael@0: michael@0: var defaultLocale = new Intl.NumberFormat().resolvedOptions().locale; michael@0: michael@0: validValues.forEach(function (value) { michael@0: var format, actual, expected; michael@0: michael@0: // with currency style, we should get the upper case form back michael@0: format = new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value}); michael@0: actual = format.resolvedOptions().currency; michael@0: expected = value.toString().toUpperCase(); michael@0: if (actual !== expected) { michael@0: $ERROR("Incorrect resolved currency with currency style - expected " + michael@0: expected + "; got " + actual + "."); michael@0: } michael@0: michael@0: // without currency style, we shouldn't get any currency back michael@0: format = new Intl.NumberFormat([defaultLocale], {currency: value}); michael@0: actual = format.resolvedOptions().currency; michael@0: expected = undefined; michael@0: if (actual !== expected) { michael@0: $ERROR("Incorrect resolved currency with non-currency style - expected " + michael@0: expected + "; got " + actual + "."); michael@0: } michael@0: michael@0: // currencies specified through the locale must be ignored michael@0: format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value}); michael@0: actual = format.resolvedOptions().currency; michael@0: expected = value.toString().toUpperCase(); michael@0: if (actual !== expected) { michael@0: $ERROR("Incorrect resolved currency with -u-cu- and currency style - expected " + michael@0: expected + "; got " + actual + "."); michael@0: } michael@0: michael@0: format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value}); michael@0: actual = format.resolvedOptions().currency; michael@0: expected = undefined; michael@0: if (actual !== expected) { michael@0: $ERROR("Incorrect resolved currency with -u-cu- and non-currency style - expected " + michael@0: expected + "; got " + actual + "."); michael@0: } michael@0: }); michael@0: michael@0: invalidValues.forEach(function (value) { michael@0: function expectError(f) { michael@0: var error; michael@0: try { michael@0: f(); michael@0: } catch (e) { michael@0: error = e; michael@0: } michael@0: if (error === undefined) { michael@0: $ERROR("Invalid currency value " + value + " was not rejected."); michael@0: } else if (error.name !== "RangeError") { michael@0: $ERROR("Invalid currency value " + value + " was rejected with wrong error " + error.name + "."); michael@0: } michael@0: } michael@0: michael@0: expectError(function () { michael@0: return new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value}); michael@0: }); michael@0: expectError(function () { michael@0: return new Intl.NumberFormat([defaultLocale], {currency: value}); michael@0: }); michael@0: expectError(function () { michael@0: return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value}); michael@0: }); michael@0: expectError(function () { michael@0: return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value}); michael@0: }); michael@0: }); michael@0: