aboutsummaryrefslogtreecommitdiffstats
path: root/test/suite/intl402/ch11/11.1/11.1.1_17.js
blob: 08c0e8e5c510880b12b4ba7a839698fba8239298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2012 Mozilla Corporation. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/**
 * @description Tests that the option currency is processed correctly.
 * @author Norbert Lindenberg
 */

var validValues = ["CNY", "USD", "EUR", "IDR", "jpy", {toString: function () {return "INR";}}];
var invalidValues = ["$", "SFr.", "US$", "ßP", {toString: function () {return;}}];

var defaultLocale = new Intl.NumberFormat().resolvedOptions().locale;

validValues.forEach(function (value) {
    var format, actual, expected;

    // with currency style, we should get the upper case form back
    format = new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value});
    actual = format.resolvedOptions().currency;
    expected = value.toString().toUpperCase();
    if (actual !== expected) {
        $ERROR("Incorrect resolved currency with currency style - expected " +
            expected + "; got " + actual + ".");
    }
    
    // without currency style, we shouldn't get any currency back
    format = new Intl.NumberFormat([defaultLocale], {currency: value});
    actual = format.resolvedOptions().currency;
    expected = undefined;
    if (actual !== expected) {
        $ERROR("Incorrect resolved currency with non-currency style - expected " +
            expected + "; got " + actual + ".");
    }
    
    // currencies specified through the locale must be ignored
    format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value});
    actual = format.resolvedOptions().currency;
    expected = value.toString().toUpperCase();
    if (actual !== expected) {
        $ERROR("Incorrect resolved currency with -u-cu- and currency style - expected " +
            expected + "; got " + actual + ".");
    }
    
    format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value});
    actual = format.resolvedOptions().currency;
    expected = undefined;
    if (actual !== expected) {
        $ERROR("Incorrect resolved currency with -u-cu- and non-currency style - expected " +
            expected + "; got " + actual + ".");
    }
});

invalidValues.forEach(function (value) {
    function expectError(f) {
        var error;
        try {
            f();
        } catch (e) {
            error = e;
        }
        if (error === undefined) {
            $ERROR("Invalid currency value " + value + " was not rejected.");
        } else if (error.name !== "RangeError") {
            $ERROR("Invalid currency value " + value + " was rejected with wrong error " + error.name + ".");
        }
    }

    expectError(function () {
            return new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value});
    });
    expectError(function () {
            return new Intl.NumberFormat([defaultLocale], {currency: value});
    });
    expectError(function () {
            return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value});
    });
    expectError(function () {
            return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value});
    });
});