Debian Codegraph @

src/com/google/javascript/rhino/testing/Asserts.java

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Nick Santos
26 * Google Inc.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License Version 2 or later (the "GPL"), in which
30 * case the provisions of the GPL are applicable instead of those above. If
31 * you wish to allow use of your version of this file only under the terms of
32 * the GPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replacing
34 * them with the notice and other provisions required by the GPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the GPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
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 * Helper methods for making assertions about the validity of types.
53 * @author [email protected] (Nick Santos)
54 */
55 public class Asserts {
56 private Asserts() {} // all static
57
58 public static JSType assertResolvesToSame(JSType type) {
59 Assert.assertSame(type, assertValidResolve(type));
60 return type;
61 }
62
63 /** @return The resolved type */
64 public static JSType assertValidResolve(JSType type) {
65 return assertValidResolve(type, MapBasedScope.emptyScope());
66 }
67
68 /** @return The resolved type */
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 * For the given equivalent types, run all type operations that
132 * should have trivial solutions (getGreatestSubtype, isEquivalentTo, etc)
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 }