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