| 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 |
package com.google.javascript.rhino.testing; |
| 41 |
|
| 42 |
import com.google.common.collect.Iterables; |
| 43 |
import com.google.javascript.rhino.ErrorReporter; |
| 44 |
import com.google.javascript.rhino.jstype.JSType; |
| 45 |
import com.google.javascript.rhino.jstype.StaticScope; |
| 46 |
|
| 47 |
import junit.framework.Assert; |
| 48 |
|
| 49 |
import java.util.Iterator; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class Asserts { |
| 56 |
private Asserts() {} |
| 57 |
|
| 58 |
public static JSType assertResolvesToSame(JSType type) { |
| 59 |
Assert.assertSame(type, assertValidResolve(type)); |
| 60 |
return type; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
public static JSType assertValidResolve(JSType type) { |
| 65 |
return assertValidResolve(type, MapBasedScope.emptyScope()); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
public static JSType assertValidResolve( |
| 70 |
JSType type, StaticScope<JSType> scope) { |
| 71 |
ErrorReporter t = TestErrorReporter.forNoExpectedReports(); |
| 72 |
JSType resolvedType = type.resolve(t, scope); |
| 73 |
assertTypeEquals("JSType#resolve should not affect object equality", |
| 74 |
type, resolvedType); |
| 75 |
return resolvedType; |
| 76 |
} |
| 77 |
|
| 78 |
public static void assertTypeNotEquals(JSType a, JSType b) { |
| 79 |
assertTypeNotEquals("", a, b); |
| 80 |
} |
| 81 |
|
| 82 |
public static void assertTypeNotEquals(String message, JSType a, JSType b) { |
| 83 |
Assert.assertFalse( |
| 84 |
message + |
| 85 |
(message.isEmpty() ? "" : "\n") + |
| 86 |
"Type: " + b + "\n", |
| 87 |
a.isEquivalentTo(b)); |
| 88 |
Assert.assertFalse( |
| 89 |
message + |
| 90 |
" Equals is not symmetric.\n" + |
| 91 |
"Type: " + b + "\n", |
| 92 |
b.isEquivalentTo(a)); |
| 93 |
} |
| 94 |
|
| 95 |
public static void assertTypeEquals(JSType a, JSType b) { |
| 96 |
assertTypeEquals("", a, b); |
| 97 |
} |
| 98 |
|
| 99 |
public static void assertTypeEquals(String message, JSType a, JSType b) { |
| 100 |
Assert.assertTrue( |
| 101 |
"Both types must be null, or both must be non-null " + a + "," + b, |
| 102 |
(a == null) == (b == null)); |
| 103 |
if (a == null) { |
| 104 |
return; |
| 105 |
} |
| 106 |
Assert.assertTrue( |
| 107 |
message + |
| 108 |
(message.isEmpty() ? "" : "\n") + |
| 109 |
"Expected: " + a + "\n" + |
| 110 |
"Actual : " + b, |
| 111 |
a.isEquivalentTo(b)); |
| 112 |
Assert.assertTrue( |
| 113 |
message + |
| 114 |
" Equals is not symmetric.\n" + |
| 115 |
"Expected: " + b + "\n" + |
| 116 |
"Actual : " + a, |
| 117 |
b.isEquivalentTo(a)); |
| 118 |
} |
| 119 |
|
| 120 |
public static <T extends JSType, S extends JSType> void |
| 121 |
assertTypeCollectionEquals(Iterable<T> a, Iterable<S> b) { |
| 122 |
Assert.assertEquals(Iterables.size(a), Iterables.size(b)); |
| 123 |
Iterator<T> aIterator = a.iterator(); |
| 124 |
Iterator<S> bIterator = b.iterator(); |
| 125 |
while (aIterator.hasNext()) { |
| 126 |
assertTypeEquals(aIterator.next(), bIterator.next()); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static void assertEquivalenceOperations(JSType a, JSType b) { |
| 135 |
Assert.assertTrue(a.isEquivalentTo(b)); |
| 136 |
Assert.assertTrue(a.isEquivalentTo(a)); |
| 137 |
Assert.assertTrue(b.isEquivalentTo(b)); |
| 138 |
Assert.assertTrue(b.isEquivalentTo(a)); |
| 139 |
|
| 140 |
Assert.assertTrue(a.isSubtype(b)); |
| 141 |
Assert.assertTrue(a.isSubtype(a)); |
| 142 |
Assert.assertTrue(b.isSubtype(b)); |
| 143 |
Assert.assertTrue(b.isSubtype(a)); |
| 144 |
|
| 145 |
assertTypeEquals(a, a.getGreatestSubtype(b)); |
| 146 |
assertTypeEquals(a, a.getGreatestSubtype(a)); |
| 147 |
assertTypeEquals(a, b.getGreatestSubtype(b)); |
| 148 |
assertTypeEquals(a, b.getGreatestSubtype(a)); |
| 149 |
|
| 150 |
assertTypeEquals(a, a.getLeastSupertype(b)); |
| 151 |
assertTypeEquals(a, a.getLeastSupertype(a)); |
| 152 |
assertTypeEquals(a, b.getLeastSupertype(b)); |
| 153 |
assertTypeEquals(a, b.getLeastSupertype(a)); |
| 154 |
|
| 155 |
Assert.assertTrue(a.canCastTo(b)); |
| 156 |
Assert.assertTrue(a.canCastTo(a)); |
| 157 |
Assert.assertTrue(b.canCastTo(b)); |
| 158 |
Assert.assertTrue(b.canCastTo(a)); |
| 159 |
} |
| 160 |
} |