xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestFmwkPlus.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.unittest;
2 
3 import com.google.common.base.Joiner;
4 import com.ibm.icu.dev.test.TestFmwk;
5 import com.ibm.icu.text.Transform;
6 import com.ibm.icu.text.Transliterator;
7 import com.ibm.icu.text.UnicodeSet;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.HashSet;
11 import java.util.Set;
12 import org.unicode.cldr.util.CldrUtility;
13 
14 public class TestFmwkPlus extends TestFmwk {
15 
16     @SuppressWarnings("unchecked")
assertTrue( String message, T arg0, R relation, V... args)17     public <T, V, R extends TestRelation<T, V>> boolean assertTrue(
18             String message, T arg0, R relation, V... args) {
19         return assertRelation(message, true, arg0, relation, args);
20     }
21 
22     @SuppressWarnings("unchecked")
assertFalse( String message, T arg0, R relation, V... args)23     public <T, V, R extends TestRelation<T, V>> boolean assertFalse(
24             String message, T arg0, R relation, V... args) {
25         return assertRelation(message, false, arg0, relation, args);
26     }
27 
28     @SuppressWarnings("unchecked")
assertTrue(T arg0, R relation, V... args)29     public <T, V, R extends TestRelation<T, V>> boolean assertTrue(T arg0, R relation, V... args) {
30         return assertRelation(null, true, arg0, relation, args);
31     }
32 
33     @SuppressWarnings("unchecked")
assertFalse(T arg0, R relation, V... args)34     public <T, V, R extends TestRelation<T, V>> boolean assertFalse(T arg0, R relation, V... args) {
35         return assertRelation(null, false, arg0, relation, args);
36     }
37 
38     @SuppressWarnings("unchecked")
assertRelation( String message, boolean expected, T arg0, R relation, V... args)39     public <T, V, R extends TestRelation<T, V>> boolean assertRelation(
40             String message, boolean expected, T arg0, R relation, V... args) {
41         boolean actual = args.length == 0 ? relation.isTrue(arg0) : relation.isTrue(arg0, args);
42         boolean test = expected == actual;
43         if (!test) {
44             errln(showArgs("", message, actual, arg0, relation, args) + "; expected " + expected);
45         } else if (isVerbose()) {
46             logln(showArgs("OK ", message, actual, arg0, relation, args));
47         }
48         return test;
49     }
50 
assertTransformsTo( String message, E expected, T transform, S source)51     public <E, S, T extends Transform<S, E>> boolean assertTransformsTo(
52             String message, E expected, T transform, S source) {
53         E actual = transform.transform(source);
54         boolean test = CldrUtility.equals(expected, actual);
55         if (!test) {
56             errln(
57                     showArgs("", message, expected, actual, transform, source)
58                             + "; expected "
59                             + "‹"
60                             + expected
61                             + "›");
62         } else if (isVerbose()) {
63             logln(showArgs("OK ", message, expected, actual, transform, source));
64         }
65         return test;
66     }
67 
showArgs( String prefix, String message, E expected, E actual, T transform, S source)68     private <E, S, T extends Transform<S, E>> String showArgs(
69             String prefix, String message, E expected, E actual, T transform, S source) {
70         String simpleName =
71                 transform instanceof Transliterator
72                         ? ((Transliterator) transform).getID()
73                         : transform.getClass().getSimpleName();
74         return prefix
75                 + sourceLocationPlus()
76                 + " "
77                 + (message == null ? "" : message + " : ")
78                 + "got ‹"
79                 + actual
80                 + "› from "
81                 + simpleName
82                 + "(‹"
83                 + source
84                 + "›)";
85     }
86 
87     @SuppressWarnings("unchecked")
showArgs( String prefix, String message, boolean expected, T arg0, R relation, V... args)88     private <T, V, R extends TestRelation<T, V>> String showArgs(
89             String prefix, String message, boolean expected, T arg0, R relation, V... args) {
90         StringBuilder others = new StringBuilder();
91         for (V arg : args) {
92             if (others.length() != 0) {
93                 others.append(", ");
94             }
95             others.append(relation.showOther(arg));
96         }
97         return prefix
98                 + sourceLocationPlus()
99                 + " "
100                 + (message == null ? "" : message + " : ")
101                 + relation.showFirst(arg0)
102                 + (expected ? " " : " NOT ")
103                 + relation
104                 + " "
105                 + others;
106     }
107 
108     public abstract static class TestRelation<T, U> {
109         @SuppressWarnings("unchecked")
isTrue(T a, U... b)110         public abstract boolean isTrue(T a, U... b);
111 
112         @Override
toString()113         public String toString() {
114             String name = this.getClass().getName();
115             int pos = name.lastIndexOf('$');
116             if (pos >= 0) {
117                 return name.substring(pos + 1);
118             }
119             pos = name.lastIndexOf('.');
120             return pos < 0 ? name : name.substring(pos + 1);
121         }
122 
showFirst(T a)123         public String showFirst(T a) {
124             return show(String.valueOf(a));
125         }
126 
showOther(U b)127         public String showOther(U b) {
128             return show(String.valueOf(b));
129         }
130 
show(String a)131         public String show(String a) {
132             return "‹" + a + "›";
133         }
134     }
135 
136     public static class Invert<T, U> extends TestRelation<T, U> {
137         private final TestRelation<T, U> other;
138 
Invert(TestRelation<T, U> other)139         public Invert(TestRelation<T, U> other) {
140             this.other = other;
141         }
142 
143         @SuppressWarnings("unchecked")
144         @Override
isTrue(T a, U... b)145         public boolean isTrue(T a, U... b) {
146             return !other.isTrue(a, b);
147         }
148 
149         @Override
toString()150         public String toString() {
151             return "not " + other;
152         }
153     }
154 
155     public static class And<T, U> extends TestRelation<T, U> {
156         private final TestRelation<T, U>[] others;
157 
158         @SuppressWarnings("unchecked")
And(TestRelation<T, U>.... others)159         public And(TestRelation<T, U>... others) {
160             this.others = others;
161         }
162 
163         @SuppressWarnings("unchecked")
164         @Override
isTrue(T a, U... b)165         public boolean isTrue(T a, U... b) {
166             for (TestRelation<T, U> other : others) {
167                 if (!other.isTrue(a, b)) {
168                     return false;
169                 }
170             }
171             return true;
172         }
173 
174         @Override
toString()175         public String toString() {
176             return Joiner.on(" and ").join(others);
177         }
178     }
179 
180     public static class Or<T, U> extends TestRelation<T, U> {
181         private final TestRelation<T, U>[] others;
182 
183         @SuppressWarnings("unchecked")
Or(TestRelation<T, U>.... others)184         public Or(TestRelation<T, U>... others) {
185             this.others = others;
186         }
187 
188         @SuppressWarnings("unchecked")
189         @Override
isTrue(T a, U... b)190         public boolean isTrue(T a, U... b) {
191             for (TestRelation<T, U> other : others) {
192                 if (other.isTrue(a, b)) {
193                     return true;
194                 }
195             }
196             return false;
197         }
198 
199         @Override
toString()200         public String toString() {
201             return Joiner.on(" or ").join(others);
202         }
203     }
204 
205     @SuppressWarnings("rawtypes")
206     public static TestRelation CONTAINS =
207             new TestRelation<Collection, Object>() {
208                 @Override
209                 public boolean isTrue(Collection a, Object... bs) {
210                     for (Object b : bs) {
211                         if (!a.contains(b)) {
212                             return false;
213                         }
214                     }
215                     return true;
216                 }
217 
218                 @Override
219                 public String toString() {
220                     return "contains";
221                 }
222             };
223 
224     @SuppressWarnings("rawtypes")
225     public static TestRelation CONTAINS_ALL =
226             new TestRelation<Collection, Object>() {
227                 @Override
228                 public boolean isTrue(Collection a, Object... bs) {
229                     for (Object b : bs) {
230                         if (!(b instanceof Collection)) {
231                             return false;
232                         }
233                         if (!a.containsAll((Collection) b)) {
234                             return false;
235                         }
236                     }
237                     return true;
238                 }
239 
240                 @Override
241                 public String toString() {
242                     return "contains-all";
243                 }
244             };
245 
246     @SuppressWarnings("rawtypes")
247     public static TestRelation CONTAINS_SOME =
248             new TestRelation<Collection, Object>() {
249                 @Override
250                 public boolean isTrue(Collection a, Object... bs) {
251                     for (Object b : bs) {
252                         if (!(b instanceof Collection)) {
253                             return false;
254                         }
255                         if (Collections.disjoint(a, (Collection) b)) {
256                             return false;
257                         }
258                     }
259                     return true;
260                 }
261 
262                 @Override
263                 public String toString() {
264                     return "contains-some";
265                 }
266             };
267 
268     @SuppressWarnings("rawtypes")
269     public static TestRelation EMPTY =
270             new TestRelation<Collection, Object>() {
271                 @Override
272                 public boolean isTrue(Collection a, Object... bs) {
273                     if (bs.length != 0) {
274                         throw new IllegalArgumentException("Should only have 1 argument");
275                     }
276                     return a.size() == 0;
277                 }
278 
279                 @Override
280                 public String toString() {
281                     return "is empty";
282                 }
283             };
284 
285     @SuppressWarnings("rawtypes")
286     public static TestRelation LEQ =
287             new TestRelation<Comparable, Comparable>() {
288                 @SuppressWarnings("unchecked")
289                 @Override
290                 public boolean isTrue(Comparable a, Comparable... bs) {
291                     if (bs.length != 1) {
292                         throw new IllegalArgumentException("Should have 2 arguments");
293                     }
294                     return a.compareTo(bs[0]) <= 0;
295                 }
296 
297                 @Override
298                 public String toString() {
299                     return " ≤ ";
300                 }
301             };
302 
303     @SuppressWarnings("rawtypes")
304     public static TestRelation GEQ =
305             new TestRelation<Comparable, Comparable>() {
306                 @SuppressWarnings("unchecked")
307                 @Override
308                 public boolean isTrue(Comparable a, Comparable... bs) {
309                     if (bs.length != 1) {
310                         throw new IllegalArgumentException("Should have 2 arguments");
311                     }
312                     return a.compareTo(bs[0]) >= 0;
313                 }
314 
315                 @Override
316                 public String toString() {
317                     return "≥ ";
318                 }
319             };
320 
321     @SuppressWarnings("rawtypes")
322     public static TestRelation IDENTICAL =
323             new TestRelation<Object, Object>() {
324                 @Override
325                 public boolean isTrue(Object a, Object... bs) {
326                     for (Object b : bs) {
327                         if (a != b) {
328                             return false;
329                         }
330                     }
331                     return true;
332                 }
333 
334                 @Override
335                 public String toString() {
336                     return "is identical to";
337                 }
338             };
339 
340     @SuppressWarnings("rawtypes")
341     public static TestRelation CONTAINS_US =
342             new TestRelation<UnicodeSet, Object>() {
343                 @Override
344                 public boolean isTrue(UnicodeSet a, Object... bs) {
345                     for (Object b : bs) {
346                         if (b instanceof UnicodeSet) {
347                             if (!a.containsAll((UnicodeSet) b)) {
348                                 return false;
349                             }
350                         } else if (b instanceof Integer) {
351                             if (!a.contains((Integer) b)) {
352                                 return false;
353                             }
354                         }
355                     }
356                     return true;
357                 }
358 
359                 @Override
360                 public String showFirst(UnicodeSet a) {
361                     return show(a.toPattern(false));
362                 }
363 
364                 public String showOther(UnicodeSet b) {
365                     return show(b.toPattern(false));
366                 }
367 
368                 @Override
369                 public String toString() {
370                     return "contains";
371                 }
372             };
373 
sourceLocationPlus()374     private String sourceLocationPlus() {
375         // Walk up the stack to the first call site outside this file
376         StackTraceElement[] st = new Throwable().getStackTrace();
377         for (int i = 0; i < st.length; ++i) {
378             if (!"TestFmwkPlus.java".equals(st[i].getFileName())) {
379                 return "File " + st[i].getFileName() + ", Line " + st[i].getLineNumber();
380             }
381         }
382         throw new InternalError();
383     }
384 
main(String[] args)385     public static void main(String[] args) {
386         new TestFmwkPlus().run(args);
387     }
388 
TestTest()389     public void TestTest() {
390         Set<String> containerA = new HashSet<String>();
391         String stringA = "a";
392         String stringA2 = "ab".substring(0, 1);
393         containerA.add(stringA);
394 
395         String stringB = "b";
396 
397         logln("These work");
398 
399         assertNotEquals("should be different", stringA, stringB);
400         assertEquals("should be same", stringA, stringA2);
401 
402         logln("These work, but the messages are not clear because you can't see the arguments");
403 
404         assertTrue("should be contained", containerA.contains(stringA));
405         assertFalse("should not be contained", containerA.contains(stringB));
406 
407         logln("These work, because you can see the arguments");
408 
409         assertFalse(stringA, IDENTICAL, stringA2);
410         assertTrue(containerA, EMPTY);
411 
412         assertTrue(containerA, CONTAINS, stringA);
413         assertFalse(containerA, CONTAINS, stringB);
414 
415         assertTrue(containerA, new Or(CONTAINS, IDENTICAL), stringA);
416         assertFalse(containerA, new And(CONTAINS, IDENTICAL), stringA);
417 
418         assertTrue(new UnicodeSet("[:L:]"), CONTAINS_US, 'a', new UnicodeSet("[ab]"));
419         assertTrue(3, LEQ, 4);
420     }
421 }
422