aboutsummaryrefslogtreecommitdiff
path: root/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestCheckDisplayCollisions.java
blob: 09cc8f1a22c23bf328082a2836d190ae4456cee1 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package org.unicode.cldr.unittest;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import org.unicode.cldr.test.CheckCLDR.CheckStatus;
import org.unicode.cldr.test.CheckCLDR.Options;
import org.unicode.cldr.test.CheckDisplayCollisions;
import org.unicode.cldr.util.CLDRConfig;
import org.unicode.cldr.util.CLDRFile;
import org.unicode.cldr.util.SimpleXMLSource;
import org.unicode.cldr.util.XMLSource;

public class TestCheckDisplayCollisions extends TestFmwkPlus {
    private static final String ukRegion =
            "//ldml/localeDisplayNames/territories/territory[@type=\"GB\"]";
    private static final String englandSubdivision =
            "//ldml/localeDisplayNames/subdivisions/subdivision[@type=\"gbeng\"]";

    private static final String scorpioEmoji =
            "//ldml/annotations/annotation[@cp=\"♏\"][@type=\"tts\"]";
    private static final String scorpionEmoji =
            "//ldml/annotations/annotation[@cp=\"🦂\"][@type=\"tts\"]";

    private static final String japanRegion =
            "//ldml/localeDisplayNames/territories/territory[@type=\"JP\"]";
    private static final String japanMap =
            "//ldml/annotations/annotation[@cp=\"🗾\"][@type=\"tts\"]";

    private static final String milli =
            "//ldml/units/unitLength[@type=\"short\"]/compoundUnit[@type=\"10p-3\"]/unitPrefixPattern";
    private static final String mega =
            "//ldml/units/unitLength[@type=\"short\"]/compoundUnit[@type=\"10p6\"]/unitPrefixPattern";

    private static final String deciLong =
            "//ldml/units/unitLength[@type=\"long\"]/compoundUnit[@type=\"10p-1\"]/unitPrefixPattern";
    private static final String deciNarrow =
            "//ldml/units/unitLength[@type=\"narrow\"]/compoundUnit[@type=\"10p-1\"]/unitPrefixPattern";
    private static final String deciShort =
            "//ldml/units/unitLength[@type=\"short\"]/compoundUnit[@type=\"10p-1\"]/unitPrefixPattern";

    public static void main(String[] args) {
        new TestCheckDisplayCollisions().run(args);
    }

    public void testInheritance() {
        XMLSource rootSource = new SimpleXMLSource("root");
        CLDRFile root = new CLDRFile(rootSource);

        XMLSource enSource = new SimpleXMLSource("en");
        CLDRFile en = new CLDRFile(enSource);

        XMLSource frSource = new SimpleXMLSource("fr");
        frSource.putValueAtPath(scorpionEmoji, "scorpion");
        frSource.putValueAtPath(scorpioEmoji, "scorpion zodiac");
        frSource.putValueAtPath(englandSubdivision, "Angleterre");
        frSource.putValueAtPath(ukRegion, "Royaume-Uni");
        frSource.putValueAtDPath(japanRegion, "Japon");
        frSource.putValueAtDPath(japanMap, "carte du Japon");
        frSource.putValueAtDPath(milli, "m{0}");
        frSource.putValueAtDPath(mega, "M{0}");

        frSource.putValueAtDPath(deciLong, "d{0}");
        frSource.putValueAtDPath(deciNarrow, "d{0}");
        frSource.putValueAtDPath(deciShort, "d{0}");

        CLDRFile fr = new CLDRFile(frSource);

        XMLSource frCaSource = new SimpleXMLSource("fr_CA");
        frCaSource.putValueAtPath(scorpioEmoji, "scorpion");
        frCaSource.putValueAtPath(ukRegion, "Angleterre");
        frCaSource.putValueAtDPath(japanMap, "Japon");
        CLDRFile frCA = new CLDRFile(frCaSource);

        TestFactory factory = new TestFactory();
        factory.addFile(root);
        factory.addFile(en);
        factory.addFile(fr);
        factory.addFile(frCA);

        CheckDisplayCollisions cdc = new CheckDisplayCollisions(factory);
        cdc.setEnglishFile(CLDRConfig.getInstance().getEnglish());

        CLDRFile frResolved = factory.make("fr", true);
        checkFile(cdc, fr, frResolved);

        CLDRFile frCaResolved = factory.make("fr_CA", true);
        checkFile(cdc, frCA, frCaResolved, scorpioEmoji, ukRegion);
    }

    public void testUnitPatternCollisions() {
        final String unitPattern1 =
                "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-dot\"]/unitPattern[@count=\"one\"]";
        /** different count as # 1. MUST NOT COLLIDE WITH #1 */
        final String unitPattern2 =
                "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-dot\"]/unitPattern[@count=\"other\"]";
        /** different unit as # 1. MUST COLLIDE WITH #1 */
        final String unitPattern3 =
                "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"length-point\"]/unitPattern[@count=\"one\"]";
        /**
         * #4 and #5 must NOT collide; case="nominative" and case="accusative" when paths are
         * otherwise identical and have the same value
         */
        final String unitPattern4 =
                "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"volume-dram\"]/unitPattern[@count=\"other\"][@case=\"nominative\"]";
        final String unitPattern5 =
                "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"volume-dram\"]/unitPattern[@count=\"other\"][@case=\"accusative\"]";

        mustCollide(false, unitPattern1, unitPattern2);
        mustCollide(true, unitPattern1, unitPattern3);
        mustCollide(false, unitPattern4, unitPattern5);
    }

    private void mustCollide(
            boolean expectCollisionErrors, String unitPatternA, String unitPatternB) {
        final String testLocale = "pt";
        final String duplicatedValue = "{0}pontos";

        final XMLSource rootSource = new SimpleXMLSource("root");
        final CLDRFile root = new CLDRFile(rootSource);

        final XMLSource enSource = new SimpleXMLSource("en");
        final CLDRFile en = new CLDRFile(enSource);

        final XMLSource source = new SimpleXMLSource(testLocale);

        source.putValueAtPath(unitPatternA, duplicatedValue);
        source.putValueAtPath(unitPatternB, duplicatedValue);

        CLDRFile file = new CLDRFile(source);

        TestFactory factory = new TestFactory();
        factory.addFile(root);
        factory.addFile(en);
        factory.addFile(file);

        CheckDisplayCollisions cdc = new CheckDisplayCollisions(factory);
        cdc.setEnglishFile(CLDRConfig.getInstance().getEnglish());

        CLDRFile ptResolved = factory.make(testLocale, true);
        if (expectCollisionErrors) {
            checkFile(cdc, file, ptResolved, unitPatternA, unitPatternB);
        } else {
            checkFile(cdc, file, ptResolved);
        }
    }

    private void checkFile(
            CheckDisplayCollisions cdc,
            CLDRFile cldrFile,
            CLDRFile cldrFileResolved,
            String... expectedErrors) {
        List<CheckStatus> possibleErrors = new ArrayList<>();
        Options options = new Options();
        cdc.setCldrFileToCheck(cldrFile, options, possibleErrors);
        if (!possibleErrors.isEmpty()) {
            errln("init: " + possibleErrors);
            possibleErrors.clear();
        }
        Map<String, List<CheckStatus>> found = new HashMap<>();
        for (String path : cldrFileResolved) {
            String value = cldrFileResolved.getStringValue(path);
            // System.out.println(path + "\t" + value);
            if (path.equals(deciLong)) {
                int debug = 0;
            }
            cdc.check(path, path, value, options, possibleErrors);
            if (!possibleErrors.isEmpty()) {
                found.put(path, ImmutableList.copyOf(possibleErrors));
                possibleErrors.clear();
                logln("Found error " + cldrFile.getLocaleID() + ":\t" + path + ", " + value);
            } else {
                logln("No error " + cldrFile.getLocaleID() + ":\t" + path + ", " + value);
            }
        }
        Set<String> expected = new TreeSet<>(Arrays.asList(expectedErrors));
        for (Entry<String, List<CheckStatus>> entry : found.entrySet()) {
            String path = entry.getKey();
            if (expected.contains(path)) {
                expected.remove(path);
            } else {
                errln(
                        cldrFile.getLocaleID()
                                + " unexpected error: "
                                + path
                                + " : "
                                + entry.getValue());
            }
            checkUnknown(path, entry);
        }
        assertEquals(
                cldrFile.getLocaleID() + " expected to be errors: ",
                Collections.emptySet(),
                expected);
    }

    /**
     * Report an error if the CheckStatus parameters contain the word "Unknown", which can be a
     * symptom of errors such as removal of required "count" attribute. "Unknown" may not always be
     * an error, but for the data used in this test we don't expect it.
     *
     * @param path
     * @param entry
     */
    private void checkUnknown(String path, Entry<String, List<CheckStatus>> entry) {
        CheckStatus cs = entry.getValue().get(0);
        String s = cs.getParameters()[0].toString();
        if (s.contains("Unknown")) {
            errln("Found Unknown in : " + path + ":\n" + s);
        }
    }

    public void TestDotPixel14031() {
        Map<String, String> pathValuePairs =
                ImmutableMap.of(
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-dot\"]/displayName",
                                "Punkt",
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-pixel\"]/displayName",
                                "Punkt",
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-pixel-per-centimeter\"]/displayName",
                                "Punkt pro Zentimeter",
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-dot-per-centimeter\"]/displayName",
                                "Punkt pro Zentimeter");
        TestFactory factory = makeFakeCldrFile("de", pathValuePairs);
        checkDisplayCollisions("de", pathValuePairs, factory);
    }

    public void checkDisplayCollisions(
            String locale, Map<String, String> pathValuePairs, TestFactory factory) {
        CheckDisplayCollisions cdc = new CheckDisplayCollisions(factory);
        cdc.setEnglishFile(CLDRConfig.getInstance().getEnglish());

        List<CheckStatus> possibleErrors = new ArrayList<>();
        cdc.setCldrFileToCheck(
                factory.make(locale, true), new Options(ImmutableMap.of()), possibleErrors);
        for (Entry<String, String> entry : pathValuePairs.entrySet()) {
            cdc.check(
                    entry.getKey(),
                    entry.getKey(),
                    entry.getValue(),
                    new Options(ImmutableMap.of()),
                    possibleErrors);
            assertEquals(entry.toString(), Collections.emptyList(), possibleErrors);
        }
    }

    public TestFactory makeFakeCldrFile(String locale, Map<String, String> pathValuePairs) {
        TestFactory factory = new TestFactory();
        XMLSource rootSource = new SimpleXMLSource("root");
        factory.addFile(new CLDRFile(rootSource));

        XMLSource localeSource = new SimpleXMLSource(locale);
        for (Entry<String, String> entry : pathValuePairs.entrySet()) {
            localeSource.putValueAtPath(entry.getKey(), entry.getValue());
        }
        factory.addFile(new CLDRFile(localeSource));
        return factory;
    }

    public void TestDurationPersonVariants() {
        Map<String, String> pathValuePairs =
                ImmutableMap.of(
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"duration-day-person\"]/unitPattern[@count=\"other\"]",
                                "Punkt",
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"duration-day\"]/unitPattern[@count=\"other\"]",
                                "Punkt",
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-pixel\"]/displayName",
                                "Punkt",
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-pixel-per-centimeter\"]/displayName",
                                "Punkt pro Zentimeter",
                        "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"graphics-dot-per-centimeter\"]/displayName",
                                "Punkt pro Zentimeter");
        TestFactory factory = makeFakeCldrFile("de", pathValuePairs);
        checkDisplayCollisions("de", pathValuePairs, factory);
    }
}