summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/ListFormat/unit-type.js
blob: 8c76677865f28caf27b30a9a54fff62e5d680c5e (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// |reftest| skip -- "unit" type currently not supported

const {Element, Literal} = ListFormatParts;
const styles = ["long", "short", "narrow"];

// Test with zero elements.
{
    const list = [];
    const expected = [];
    const locales = ["ar", "de", "en", "es", "ja", "nl", "th", "zh"];

    for (let locale of locales) {
        for (let style of styles) {
            let lf = new Intl.ListFormat(locale, {type: "unit", style});
            assertParts(lf, list, expected);
        }
    }
}

// Test with one element.
{
    const list = ["A"];
    const expected = [Element(list[0])];
    const locales = ["ar", "de", "en", "es", "ja", "nl", "th", "zh"];

    for (let locale of locales) {
        for (let style of styles) {
            let lf = new Intl.ListFormat(locale, {type: "unit", style});
            assertParts(lf, list, expected);
        }
    }
}

// Test with two elements to cover the [[Template2]] case.
{
    const list = ["A", "B"];

    const testData = {
        "ar": {
            long: [Element("A"), Literal(" و"), Element("B")],
            narrow: [Element("A"), Literal("، "), Element("B")],
        },
        "de": {
            long: [Element("A"), Literal(", "), Element("B")],
        },
        "en": {
            long: [Element("A"), Literal(", "), Element("B")],
            narrow: [Element("A"), Literal(" "), Element("B")],
        },
        "es": {
            long: [Element("A"), Literal(" y "), Element("B")],
            narrow: [Element("A"), Literal(" "), Element("B")],
        },
        "ja": {
            long: [Element("A"), Literal(" "), Element("B")],
            narrow: [Element("A"), Element("B")],
        },
        "nl": {
            long: [Element("A"), Literal(" en "), Element("B")],
            short: [Element("A"), Literal(", "), Element("B")],
            narrow: [Element("A"), Literal(", "), Element("B")],
        },
        "th": {
            long: [Element("A"), Literal(" และ "), Element("B")],
            short: [Element("A"), Literal(" "), Element("B")],
            narrow: [Element("A"), Literal(" "), Element("B")],
        },
        "zh": {
            long: [Element("A"), Element("B")],
        },
    };

    for (let [locale, localeData] of Object.entries(testData)) {
        for (let style of styles) {
            let lf = new Intl.ListFormat(locale, {type: "unit", style});
            let {[style]: expected = localeData.long} = localeData;
            assertParts(lf, list, expected);
        }
    }
}

// Test with more than two elements.
//
// Use four elements to cover all template parts ([[TemplateStart]], [[TemplateMiddle]], and
// [[TemplateEnd]]).
{
    const list = ["A", "B", "C", "D"];

    const testData = {
        // non-ASCII case
        "ar": {
            long: [Element("A"), Literal("، و"), Element("B"), Literal("، و"), Element("C"), Literal("، و"), Element("D")],
            narrow: [Element("A"), Literal("، "), Element("B"), Literal("، "), Element("C"), Literal("، "), Element("D")],
        },

        // all values are equal
        "de": {
            long: [Element("A"), Literal(", "), Element("B"), Literal(", "), Element("C"), Literal(" und "), Element("D")],
        },

        // long and short values are equal
        "en": {
            long: [Element("A"), Literal(", "), Element("B"), Literal(", "), Element("C"), Literal(", "), Element("D")],
            narrow: [Element("A"), Literal(" "), Element("B"), Literal(" "), Element("C"), Literal(" "), Element("D")],
        },

        // all values are different
        "es": {
            long: [Element("A"), Literal(", "), Element("B"), Literal(", "), Element("C"), Literal(" y "), Element("D")],
            short: [Element("A"), Literal(", "), Element("B"), Literal(", "), Element("C"), Literal(", "), Element("D")],
            narrow: [Element("A"), Literal(" "), Element("B"), Literal(" "), Element("C"), Literal(" "), Element("D")],
        },

        // no spacing for narrow case
        "ja": {
            long: [Element("A"), Literal(" "), Element("B"), Literal(" "), Element("C"), Literal(" "), Element("D")],
            narrow: [Element("A"), Element("B"), Element("C"), Element("D")],
        },

        // short and narrow values are equal
        "nl": {
            long: [Element("A"), Literal(", "), Element("B"), Literal(", "), Element("C"), Literal(" en "), Element("D")],
            short: [Element("A"), Literal(", "), Element("B"), Literal(", "), Element("C"), Literal(", "), Element("D")],
            narrow: [Element("A"), Literal(", "), Element("B"), Literal(", "), Element("C"), Literal(", "), Element("D")],
        },

        // another non-ASCII case
        "th": {
            long: [Element("A"), Literal(" "), Element("B"), Literal(" "), Element("C"), Literal(" และ "), Element("D")],
            narrow: [Element("A"), Literal(" "), Element("B"), Literal(" "), Element("C"), Literal(" "), Element("D")],
        },

        // no whitespace at all
        "zh": {
            long: [Element("A"), Element("B"), Element("C"), Element("D")],
        },
    };

    for (let [locale, localeData] of Object.entries(testData)) {
        for (let style of styles) {
            let lf = new Intl.ListFormat(locale, {type: "unit", style});
            let {[style]: expected = localeData.long} = localeData;
            assertParts(lf, list, expected);
        }
    }
}

if (typeof reportCompare === "function")
    reportCompare(0, 0);