Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: pkg/analyzer/test/generated/test_support.dart

Issue 685573002: Inline JUnitTestCase 'assert' methods. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.test_support; 8 library engine.test_support;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
11 import "dart:math" as math; 11 import "dart:math" as math;
12 12
13 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator; 13 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator;
14 import 'package:analyzer/src/generated/element.dart'; 14 import 'package:analyzer/src/generated/element.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 15 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/error.dart'; 16 import 'package:analyzer/src/generated/error.dart';
17 import 'package:analyzer/src/generated/java_core.dart'; 17 import 'package:analyzer/src/generated/java_core.dart';
18 import 'package:analyzer/src/generated/java_engine.dart'; 18 import 'package:analyzer/src/generated/java_engine.dart';
19 import 'package:analyzer/src/generated/java_junit.dart';
20 import 'package:analyzer/src/generated/scanner.dart'; 19 import 'package:analyzer/src/generated/scanner.dart';
21 import 'package:analyzer/src/generated/source.dart'; 20 import 'package:analyzer/src/generated/source.dart';
21 import 'package:unittest/unittest.dart';
22 22
23 23
24 /** 24 /**
25 * The class `EngineTestCase` defines utility methods for making assertions. 25 * The class `EngineTestCase` defines utility methods for making assertions.
26 */ 26 */
27 class EngineTestCase extends JUnitTestCase { 27 class EngineTestCase {
28 static int _PRINT_RANGE = 6; 28 static int _PRINT_RANGE = 6;
29 29
30 void setUp() {}
31
32 void tearDown() {}
33
30 /** 34 /**
31 * Assert that the given collection has the same number of elements as the num ber of specified 35 * Assert that the given collection has the same number of elements as the num ber of specified
32 * names, and that for each specified name, a corresponding element can be fou nd in the given 36 * names, and that for each specified name, a corresponding element can be fou nd in the given
33 * collection with that name. 37 * collection with that name.
34 * 38 *
35 * @param elements the elements 39 * @param elements the elements
36 * @param names the names 40 * @param names the names
37 */ 41 */
38 void assertNamedElements(List<Element> elements, List<String> names) { 42 void assertNamedElements(List<Element> elements, List<String> names) {
39 for (String elemName in names) { 43 for (String elemName in names) {
40 bool found = false; 44 bool found = false;
41 for (Element elem in elements) { 45 for (Element elem in elements) {
42 if (elem.name == elemName) { 46 if (elem.name == elemName) {
43 found = true; 47 found = true;
44 break; 48 break;
45 } 49 }
46 } 50 }
47 if (!found) { 51 if (!found) {
48 StringBuffer buffer = new StringBuffer(); 52 StringBuffer buffer = new StringBuffer();
49 buffer.write("Expected element named: "); 53 buffer.write("Expected element named: ");
50 buffer.write(elemName); 54 buffer.write(elemName);
51 buffer.write("\n but found: "); 55 buffer.write("\n but found: ");
52 for (Element elem in elements) { 56 for (Element elem in elements) {
53 buffer.write(elem.name); 57 buffer.write(elem.name);
54 buffer.write(", "); 58 buffer.write(", ");
55 } 59 }
56 JUnitTestCase.fail(buffer.toString()); 60 fail(buffer.toString());
57 } 61 }
58 } 62 }
59 assertLength(names.length, elements); 63 assertLength(names.length, elements);
60 } 64 }
61 65
62 AnalysisContextImpl createAnalysisContext() { 66 AnalysisContextImpl createAnalysisContext() {
63 AnalysisContextImpl context = new AnalysisContextImpl(); 67 AnalysisContextImpl context = new AnalysisContextImpl();
64 context.sourceFactory = new SourceFactory([]); 68 context.sourceFactory = new SourceFactory([]);
65 return context; 69 return context;
66 } 70 }
67 71
68 /** 72 /**
69 * Return the getter in the given type with the given name. Inherited getters are ignored. 73 * Return the getter in the given type with the given name. Inherited getters are ignored.
70 * 74 *
71 * @param type the type in which the getter is declared 75 * @param type the type in which the getter is declared
72 * @param getterName the name of the getter to be returned 76 * @param getterName the name of the getter to be returned
73 * @return the property accessor element representing the getter with the give n name 77 * @return the property accessor element representing the getter with the give n name
74 */ 78 */
75 PropertyAccessorElement getGetter(InterfaceType type, String getterName) { 79 PropertyAccessorElement getGetter(InterfaceType type, String getterName) {
76 for (PropertyAccessorElement accessor in type.element.accessors) { 80 for (PropertyAccessorElement accessor in type.element.accessors) {
77 if (accessor.isGetter && accessor.name == getterName) { 81 if (accessor.isGetter && accessor.name == getterName) {
78 return accessor; 82 return accessor;
79 } 83 }
80 } 84 }
81 JUnitTestCase.fail( 85 fail("Could not find getter named $getterName in ${type.displayName}");
82 "Could not find getter named $getterName in ${type.displayName}");
83 return null; 86 return null;
84 } 87 }
85 88
86 /** 89 /**
87 * Return the method in the given type with the given name. Inherited methods are ignored. 90 * Return the method in the given type with the given name. Inherited methods are ignored.
88 * 91 *
89 * @param type the type in which the method is declared 92 * @param type the type in which the method is declared
90 * @param methodName the name of the method to be returned 93 * @param methodName the name of the method to be returned
91 * @return the method element representing the method with the given name 94 * @return the method element representing the method with the given name
92 */ 95 */
93 MethodElement getMethod(InterfaceType type, String methodName) { 96 MethodElement getMethod(InterfaceType type, String methodName) {
94 for (MethodElement method in type.element.methods) { 97 for (MethodElement method in type.element.methods) {
95 if (method.name == methodName) { 98 if (method.name == methodName) {
96 return method; 99 return method;
97 } 100 }
98 } 101 }
99 JUnitTestCase.fail( 102 fail("Could not find method named $methodName in ${type.displayName}");
100 "Could not find method named $methodName in ${type.displayName}");
101 return null; 103 return null;
102 } 104 }
103 105
104 /** 106 /**
105 * Assert that the tokens in the actual stream of tokens have the same types a nd lexemes as the 107 * Assert that the tokens in the actual stream of tokens have the same types a nd lexemes as the
106 * tokens in the expected stream of tokens. Note that this does not assert any thing about the 108 * tokens in the expected stream of tokens. Note that this does not assert any thing about the
107 * offsets of the tokens (although the lengths will be equal). 109 * offsets of the tokens (although the lengths will be equal).
108 * 110 *
109 * @param expectedStream the head of the stream of tokens that were expected 111 * @param expectedStream the head of the stream of tokens that were expected
110 * @param actualStream the head of the stream of tokens that were actually fou nd 112 * @param actualStream the head of the stream of tokens that were actually fou nd
(...skipping 12 matching lines...) Expand all
123 /** 125 /**
124 * Assert that the given collection is non-`null` and has the expected number of elements. 126 * Assert that the given collection is non-`null` and has the expected number of elements.
125 * 127 *
126 * @param expectedSize the expected number of elements 128 * @param expectedSize the expected number of elements
127 * @param c the collection being tested 129 * @param c the collection being tested
128 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number 130 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number
129 * of elements 131 * of elements
130 */ 132 */
131 static void assertCollectionSize(int expectedSize, Iterable c) { 133 static void assertCollectionSize(int expectedSize, Iterable c) {
132 if (c == null) { 134 if (c == null) {
133 JUnitTestCase.fail( 135 fail("Expected collection of size $expectedSize; found null");
134 "Expected collection of size $expectedSize; found null");
135 } else if (c.length != expectedSize) { 136 } else if (c.length != expectedSize) {
136 JUnitTestCase.fail( 137 fail("Expected collection of size $expectedSize; contained ${c.length} ele ments");
137 "Expected collection of size $expectedSize; contained ${c.length} elem ents");
138 } 138 }
139 } 139 }
140 140
141 /** 141 /**
142 * Assert that the given array is non-`null` and contains the expected element s. The 142 * Assert that the given array is non-`null` and contains the expected element s. The
143 * elements can appear in any order. 143 * elements can appear in any order.
144 * 144 *
145 * @param array the array being tested 145 * @param array the array being tested
146 * @param expectedElements the expected elements 146 * @param expectedElements the expected elements
147 * @throws AssertionFailedError if the array is `null` or does not contain the expected 147 * @throws AssertionFailedError if the array is `null` or does not contain the expected
148 * elements 148 * elements
149 */ 149 */
150 static void assertContains(List<Object> array, 150 static void assertContains(List<Object> array,
151 List<Object> expectedElements) { 151 List<Object> expectedElements) {
152 int expectedSize = expectedElements.length; 152 int expectedSize = expectedElements.length;
153 if (array == null) { 153 if (array == null) {
154 JUnitTestCase.fail( 154 fail("Expected array of length $expectedSize; found null");
155 "Expected array of length $expectedSize; found null");
156 } 155 }
157 if (array.length != expectedSize) { 156 if (array.length != expectedSize) {
158 JUnitTestCase.fail( 157 fail("Expected array of length $expectedSize; contained ${array.length} el ements");
159 "Expected array of length $expectedSize; contained ${array.length} ele ments");
160 } 158 }
161 List<bool> found = new List<bool>.filled(expectedSize, false); 159 List<bool> found = new List<bool>.filled(expectedSize, false);
162 for (int i = 0; i < expectedSize; i++) { 160 for (int i = 0; i < expectedSize; i++) {
163 _privateAssertContains(array, found, expectedElements[i]); 161 _privateAssertContains(array, found, expectedElements[i]);
164 } 162 }
165 } 163 }
166 164
167 /** 165 /**
168 * Assert that a given String is equal to an expected value. 166 * Assert that a given String is equal to an expected value.
169 * 167 *
170 * @param expected the expected String value 168 * @param expected the expected String value
171 * @param actual the actual String value 169 * @param actual the actual String value
172 */ 170 */
173 static void assertEqualString(String expected, String actual) { 171 static void assertEqualString(String expected, String actual) {
174 if (actual == null || expected == null) { 172 if (actual == null || expected == null) {
175 if (identical(actual, expected)) { 173 if (identical(actual, expected)) {
176 return; 174 return;
177 } 175 }
178 if (actual == null) { 176 if (actual == null) {
179 JUnitTestCase.assertTrueMsg( 177 expect(false, isTrue, reason: "Content not as expected: is 'null' expect ed: $expected");
Brian Wilkerson 2014/10/27 23:13:29 Why not just use 'fail'?
scheglov 2014/10/28 00:01:06 The original Java source: Assert.assertTrue("Cont
180 "Content not as expected: is 'null' expected: $expected",
181 false);
182 } else { 178 } else {
183 JUnitTestCase.assertTrueMsg( 179 expect(false, isTrue, reason: "Content not as expected: expected 'null' is: $actual");
184 "Content not as expected: expected 'null' is: $actual",
185 false);
186 } 180 }
187 } 181 }
188 int diffPos = _getDiffPos(expected, actual); 182 int diffPos = _getDiffPos(expected, actual);
189 if (diffPos != -1) { 183 if (diffPos != -1) {
190 int diffAhead = math.max(0, diffPos - _PRINT_RANGE); 184 int diffAhead = math.max(0, diffPos - _PRINT_RANGE);
191 int diffAfter = math.min(actual.length, diffPos + _PRINT_RANGE); 185 int diffAfter = math.min(actual.length, diffPos + _PRINT_RANGE);
192 String diffStr = 186 String diffStr =
193 "${actual.substring(diffAhead, diffPos)}^${actual.substring(diffPos, d iffAfter)}"; 187 "${actual.substring(diffAhead, diffPos)}^${actual.substring(diffPos, d iffAfter)}";
194 // use detailed message 188 // use detailed message
195 String message = 189 String message =
196 "Content not as expected: is\n$actual\nDiffers at pos $diffPos: $diffS tr\nexpected:\n$expected"; 190 "Content not as expected: is\n$actual\nDiffers at pos $diffPos: $diffS tr\nexpected:\n$expected";
197 JUnitTestCase.assertEqualsMsg(message, expected, actual); 191 expect(actual, expected, reason: message);
198 } 192 }
199 } 193 }
200 194
201 /** 195 /**
202 * Assert that the array of actual values contain exactly the same values as t hose in the array of 196 * Assert that the array of actual values contain exactly the same values as t hose in the array of
203 * expected value, with the exception that the order of the elements is not re quired to be the 197 * expected value, with the exception that the order of the elements is not re quired to be the
204 * same. 198 * same.
205 * 199 *
206 * @param expectedValues the values that are expected to be found 200 * @param expectedValues the values that are expected to be found
207 * @param actualValues the actual values that are being compared against the e xpected values 201 * @param actualValues the actual values that are being compared against the e xpected values
208 */ 202 */
209 static void assertEqualsIgnoreOrder(List<Object> expectedValues, 203 static void assertEqualsIgnoreOrder(List<Object> expectedValues,
210 List<Object> actualValues) { 204 List<Object> actualValues) {
211 JUnitTestCase.assertNotNull(actualValues); 205 expect(actualValues, isNotNull);
212 int expectedLength = expectedValues.length; 206 int expectedLength = expectedValues.length;
213 JUnitTestCase.assertEquals(expectedLength, actualValues.length); 207 expect(actualValues.length, expectedLength);
214 List<bool> found = new List<bool>.filled(expectedLength, false); 208 List<bool> found = new List<bool>.filled(expectedLength, false);
215 for (int i = 0; i < expectedLength; i++) { 209 for (int i = 0; i < expectedLength; i++) {
216 found[i] = false; 210 found[i] = false;
217 } 211 }
218 for (Object actualValue in actualValues) { 212 for (Object actualValue in actualValues) {
219 bool wasExpected = false; 213 bool wasExpected = false;
220 for (int i = 0; i < expectedLength; i++) { 214 for (int i = 0; i < expectedLength; i++) {
221 if (!found[i] && expectedValues[i] == actualValue) { 215 if (!found[i] && expectedValues[i] == actualValue) {
222 found[i] = true; 216 found[i] = true;
223 wasExpected = true; 217 wasExpected = true;
224 break; 218 break;
225 } 219 }
226 } 220 }
227 if (!wasExpected) { 221 if (!wasExpected) {
228 JUnitTestCase.fail("The actual value $actualValue was not expected"); 222 fail("The actual value $actualValue was not expected");
229 } 223 }
230 } 224 }
231 } 225 }
232 226
233 /** 227 /**
234 * Assert that the given array is non-`null` and has exactly expected elements . 228 * Assert that the given array is non-`null` and has exactly expected elements .
235 * 229 *
236 * @param array the array being tested 230 * @param array the array being tested
237 * @param expectedElements the expected elements 231 * @param expectedElements the expected elements
238 * @throws AssertionFailedError if the array is `null` or does not have the ex pected 232 * @throws AssertionFailedError if the array is `null` or does not have the ex pected
239 * elements 233 * elements
240 */ 234 */
241 static void assertExactElementsInArray(List<Object> array, 235 static void assertExactElementsInArray(List<Object> array,
242 List<Object> expectedElements) { 236 List<Object> expectedElements) {
243 int expectedSize = expectedElements.length; 237 int expectedSize = expectedElements.length;
244 if (array == null) { 238 if (array == null) {
245 JUnitTestCase.fail("Expected array of size $expectedSize; found null"); 239 fail("Expected array of size $expectedSize; found null");
246 } 240 }
247 if (array.length != expectedSize) { 241 if (array.length != expectedSize) {
248 JUnitTestCase.fail( 242 fail("Expected array of size $expectedSize; contained ${array.length} elem ents");
249 "Expected array of size $expectedSize; contained ${array.length} eleme nts");
250 } 243 }
251 for (int i = 0; i < expectedSize; i++) { 244 for (int i = 0; i < expectedSize; i++) {
252 Object element = array[i]; 245 Object element = array[i];
253 Object expectedElement = expectedElements[i]; 246 Object expectedElement = expectedElements[i];
254 if (element != expectedElement) { 247 if (element != expectedElement) {
255 JUnitTestCase.fail( 248 fail("Expected $expectedElement at [$i]; found $element");
256 "Expected $expectedElement at [$i]; found $element");
257 } 249 }
258 } 250 }
259 } 251 }
260 252
261 /** 253 /**
262 * Assert that the given list is non-`null` and has exactly expected elements. 254 * Assert that the given list is non-`null` and has exactly expected elements.
263 * 255 *
264 * @param list the list being tested 256 * @param list the list being tested
265 * @param expectedElements the expected elements 257 * @param expectedElements the expected elements
266 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements 258 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements
267 */ 259 */
268 static void assertExactElementsInList(List list, 260 static void assertExactElementsInList(List list,
269 List<Object> expectedElements) { 261 List<Object> expectedElements) {
270 int expectedSize = expectedElements.length; 262 int expectedSize = expectedElements.length;
271 if (list == null) { 263 if (list == null) {
272 JUnitTestCase.fail("Expected list of size $expectedSize; found null"); 264 fail("Expected list of size $expectedSize; found null");
273 } 265 }
274 if (list.length != expectedSize) { 266 if (list.length != expectedSize) {
275 JUnitTestCase.fail( 267 fail("Expected list of size $expectedSize; contained ${list.length} elemen ts");
276 "Expected list of size $expectedSize; contained ${list.length} element s");
277 } 268 }
278 for (int i = 0; i < expectedSize; i++) { 269 for (int i = 0; i < expectedSize; i++) {
279 Object element = list[i]; 270 Object element = list[i];
280 Object expectedElement = expectedElements[i]; 271 Object expectedElement = expectedElements[i];
281 if (element != expectedElement) { 272 if (element != expectedElement) {
282 JUnitTestCase.fail( 273 fail("Expected $expectedElement at [$i]; found $element");
283 "Expected $expectedElement at [$i]; found $element");
284 } 274 }
285 } 275 }
286 } 276 }
287 277
288 /** 278 /**
289 * Assert that the given list is non-`null` and has exactly expected elements. 279 * Assert that the given list is non-`null` and has exactly expected elements.
290 * 280 *
291 * @param set the list being tested 281 * @param set the list being tested
292 * @param expectedElements the expected elements 282 * @param expectedElements the expected elements
293 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements 283 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements
294 */ 284 */
295 static void assertExactElementsInSet(Set set, List<Object> expectedElements) { 285 static void assertExactElementsInSet(Set set, List<Object> expectedElements) {
296 int expectedSize = expectedElements.length; 286 int expectedSize = expectedElements.length;
297 if (set == null) { 287 if (set == null) {
298 JUnitTestCase.fail("Expected list of size $expectedSize; found null"); 288 fail("Expected list of size $expectedSize; found null");
299 } 289 }
300 if (set.length != expectedSize) { 290 if (set.length != expectedSize) {
301 JUnitTestCase.fail( 291 fail("Expected list of size $expectedSize; contained ${set.length} element s");
302 "Expected list of size $expectedSize; contained ${set.length} elements ");
303 } 292 }
304 for (int i = 0; i < expectedSize; i++) { 293 for (int i = 0; i < expectedSize; i++) {
305 Object expectedElement = expectedElements[i]; 294 Object expectedElement = expectedElements[i];
306 if (!set.contains(expectedElement)) { 295 if (!set.contains(expectedElement)) {
307 JUnitTestCase.fail("Expected $expectedElement in set$set"); 296 fail("Expected $expectedElement in set$set");
308 } 297 }
309 } 298 }
310 } 299 }
311 300
312 /** 301 /**
313 * Assert that the given object is an instance of the expected class. 302 * Assert that the given object is an instance of the expected class.
314 * 303 *
315 * @param expectedClass the class that the object is expected to be an instanc e of 304 * @param expectedClass the class that the object is expected to be an instanc e of
316 * @param object the object being tested 305 * @param object the object being tested
317 * @return the object that was being tested 306 * @return the object that was being tested
318 * @throws Exception if the object is not an instance of the expected class 307 * @throws Exception if the object is not an instance of the expected class
319 */ 308 */
320 static Object assertInstanceOf(Predicate<Object> predicate, 309 static Object assertInstanceOf(Predicate<Object> predicate,
321 Type expectedClass, Object object) { 310 Type expectedClass, Object object) {
322 if (!predicate(object)) { 311 if (!predicate(object)) {
323 JUnitTestCase.fail( 312 fail("Expected instance of $expectedClass, found ${object == null ? "null" : object.runtimeType}");
324 "Expected instance of $expectedClass, found ${object == null ? "null" : object.runtimeType}");
325 } 313 }
326 return object; 314 return object;
327 } 315 }
328 316
329 /** 317 /**
330 * Assert that the given array is non-`null` and has the expected number of el ements. 318 * Assert that the given array is non-`null` and has the expected number of el ements.
331 * 319 *
332 * @param expectedLength the expected number of elements 320 * @param expectedLength the expected number of elements
333 * @param array the array being tested 321 * @param array the array being tested
334 * @throws AssertionFailedError if the array is `null` or does not have the ex pected number 322 * @throws AssertionFailedError if the array is `null` or does not have the ex pected number
335 * of elements 323 * of elements
336 */ 324 */
337 static void assertLength(int expectedLength, List<Object> array) { 325 static void assertLength(int expectedLength, List<Object> array) {
338 if (array == null) { 326 if (array == null) {
339 JUnitTestCase.fail( 327 fail("Expected array of length $expectedLength; found null");
340 "Expected array of length $expectedLength; found null");
341 } else if (array.length != expectedLength) { 328 } else if (array.length != expectedLength) {
342 JUnitTestCase.fail( 329 fail("Expected array of length $expectedLength; contained ${array.length} elements");
343 "Expected array of length $expectedLength; contained ${array.length} e lements");
344 } 330 }
345 } 331 }
346 332
347 /** 333 /**
348 * Assert that the actual token has the same type and lexeme as the expected t oken. Note that this 334 * Assert that the actual token has the same type and lexeme as the expected t oken. Note that this
349 * does not assert anything about the offsets of the tokens (although the leng ths will be equal). 335 * does not assert anything about the offsets of the tokens (although the leng ths will be equal).
350 * 336 *
351 * @param expectedToken the token that was expected 337 * @param expectedToken the token that was expected
352 * @param actualToken the token that was found 338 * @param actualToken the token that was found
353 * @throws AssertionFailedError if the two tokens are not the same 339 * @throws AssertionFailedError if the two tokens are not the same
354 */ 340 */
355 static void assertMatches(Token expectedToken, Token actualToken) { 341 static void assertMatches(Token expectedToken, Token actualToken) {
356 JUnitTestCase.assertEquals(expectedToken.type, actualToken.type); 342 expect(actualToken.type, expectedToken.type);
357 if (expectedToken is KeywordToken) { 343 if (expectedToken is KeywordToken) {
358 assertInstanceOf((obj) => obj is KeywordToken, KeywordToken, actualToken); 344 assertInstanceOf((obj) => obj is KeywordToken, KeywordToken, actualToken);
359 JUnitTestCase.assertEquals( 345 expect((actualToken as KeywordToken).keyword, expectedToken.keyword);
360 expectedToken.keyword,
361 (actualToken as KeywordToken).keyword);
362 } else if (expectedToken is StringToken) { 346 } else if (expectedToken is StringToken) {
363 assertInstanceOf((obj) => obj is StringToken, StringToken, actualToken); 347 assertInstanceOf((obj) => obj is StringToken, StringToken, actualToken);
364 JUnitTestCase.assertEquals( 348 expect((actualToken as StringToken).lexeme, expectedToken.lexeme);
365 expectedToken.lexeme,
366 (actualToken as StringToken).lexeme);
367 } 349 }
368 } 350 }
369 351
370 /** 352 /**
371 * Assert that the given list is non-`null` and has the expected number of ele ments. 353 * Assert that the given list is non-`null` and has the expected number of ele ments.
372 * 354 *
373 * @param expectedSize the expected number of elements 355 * @param expectedSize the expected number of elements
374 * @param list the list being tested 356 * @param list the list being tested
375 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number 357 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number
376 * of elements 358 * of elements
377 */ 359 */
378 static void assertSizeOfList(int expectedSize, List list) { 360 static void assertSizeOfList(int expectedSize, List list) {
379 if (list == null) { 361 if (list == null) {
380 JUnitTestCase.fail("Expected list of size $expectedSize; found null"); 362 fail("Expected list of size $expectedSize; found null");
381 } else if (list.length != expectedSize) { 363 } else if (list.length != expectedSize) {
382 JUnitTestCase.fail( 364 fail("Expected list of size $expectedSize; contained ${list.length} elemen ts");
383 "Expected list of size $expectedSize; contained ${list.length} element s");
384 } 365 }
385 } 366 }
386 367
387 /** 368 /**
388 * Assert that the given map is non-`null` and has the expected number of elem ents. 369 * Assert that the given map is non-`null` and has the expected number of elem ents.
389 * 370 *
390 * @param expectedSize the expected number of elements 371 * @param expectedSize the expected number of elements
391 * @param map the map being tested 372 * @param map the map being tested
392 * @throws AssertionFailedError if the map is `null` or does not have the expe cted number of 373 * @throws AssertionFailedError if the map is `null` or does not have the expe cted number of
393 * elements 374 * elements
394 */ 375 */
395 static void assertSizeOfMap(int expectedSize, Map map) { 376 static void assertSizeOfMap(int expectedSize, Map map) {
396 if (map == null) { 377 if (map == null) {
397 JUnitTestCase.fail("Expected map of size $expectedSize; found null"); 378 fail("Expected map of size $expectedSize; found null");
398 } else if (map.length != expectedSize) { 379 } else if (map.length != expectedSize) {
399 JUnitTestCase.fail( 380 fail("Expected map of size $expectedSize; contained ${map.length} elements ");
400 "Expected map of size $expectedSize; contained ${map.length} elements" );
401 } 381 }
402 } 382 }
403 383
404 /** 384 /**
405 * Assert that the given set is non-`null` and has the expected number of elem ents. 385 * Assert that the given set is non-`null` and has the expected number of elem ents.
406 * 386 *
407 * @param expectedSize the expected number of elements 387 * @param expectedSize the expected number of elements
408 * @param set the set being tested 388 * @param set the set being tested
409 * @throws AssertionFailedError if the set is `null` or does not have the expe cted number of 389 * @throws AssertionFailedError if the set is `null` or does not have the expe cted number of
410 * elements 390 * elements
411 */ 391 */
412 static void assertSizeOfSet(int expectedSize, Set set) { 392 static void assertSizeOfSet(int expectedSize, Set set) {
413 if (set == null) { 393 if (set == null) {
414 JUnitTestCase.fail("Expected set of size $expectedSize; found null"); 394 fail("Expected set of size $expectedSize; found null");
415 } else if (set.length != expectedSize) { 395 } else if (set.length != expectedSize) {
416 JUnitTestCase.fail( 396 fail("Expected set of size $expectedSize; contained ${set.length} elements ");
417 "Expected set of size $expectedSize; contained ${set.length} elements" );
418 } 397 }
419 } 398 }
420 399
421 /** 400 /**
422 * @return the [AstNode] with requested type at offset of the "prefix". 401 * @return the [AstNode] with requested type at offset of the "prefix".
423 */ 402 */
424 static AstNode findNode(AstNode root, String code, String prefix, 403 static AstNode findNode(AstNode root, String code, String prefix,
425 Predicate<AstNode> predicate) { 404 Predicate<AstNode> predicate) {
426 int offset = code.indexOf(prefix); 405 int offset = code.indexOf(prefix);
427 if (offset == -1) { 406 if (offset == -1) {
(...skipping 29 matching lines...) Expand all
457 Object element) { 436 Object element) {
458 if (element == null) { 437 if (element == null) {
459 for (int i = 0; i < array.length; i++) { 438 for (int i = 0; i < array.length; i++) {
460 if (!found[i]) { 439 if (!found[i]) {
461 if (array[i] == null) { 440 if (array[i] == null) {
462 found[i] = true; 441 found[i] = true;
463 return; 442 return;
464 } 443 }
465 } 444 }
466 } 445 }
467 JUnitTestCase.fail("Does not contain null"); 446 fail("Does not contain null");
468 } else { 447 } else {
469 for (int i = 0; i < array.length; i++) { 448 for (int i = 0; i < array.length; i++) {
470 if (!found[i]) { 449 if (!found[i]) {
471 if (element == array[i]) { 450 if (element == array[i]) {
472 found[i] = true; 451 found[i] = true;
473 return; 452 return;
474 } 453 }
475 } 454 }
476 } 455 }
477 JUnitTestCase.fail("Does not contain $element"); 456 fail("Does not contain $element");
478 } 457 }
479 } 458 }
480 } 459 }
481 460
482 /** 461 /**
483 * Instances of the class `GatheringErrorListener` implement an error listener t hat collects 462 * Instances of the class `GatheringErrorListener` implement an error listener t hat collects
484 * all of the errors passed to it for later examination. 463 * all of the errors passed to it for later examination.
485 */ 464 */
486 class GatheringErrorListener implements AnalysisErrorListener { 465 class GatheringErrorListener implements AnalysisErrorListener {
487 /** 466 /**
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 * @param expectedErrorCodes the error codes of the errors that should have be en gathered 567 * @param expectedErrorCodes the error codes of the errors that should have be en gathered
589 * @throws AssertionFailedError if a different number of errors have been gath ered than were 568 * @throws AssertionFailedError if a different number of errors have been gath ered than were
590 * expected 569 * expected
591 */ 570 */
592 void assertErrorsWithCodes(List<ErrorCode> expectedErrorCodes) { 571 void assertErrorsWithCodes(List<ErrorCode> expectedErrorCodes) {
593 StringBuffer buffer = new StringBuffer(); 572 StringBuffer buffer = new StringBuffer();
594 // 573 //
595 // Verify that the expected error codes have a non-empty message. 574 // Verify that the expected error codes have a non-empty message.
596 // 575 //
597 for (ErrorCode errorCode in expectedErrorCodes) { 576 for (ErrorCode errorCode in expectedErrorCodes) {
598 JUnitTestCase.assertFalseMsg( 577 expect(errorCode.message.isEmpty, isFalse, reason: "Empty error code messa ge");
599 "Empty error code message",
600 errorCode.message.isEmpty);
601 } 578 }
602 // 579 //
603 // Compute the expected number of each type of error. 580 // Compute the expected number of each type of error.
604 // 581 //
605 HashMap<ErrorCode, int> expectedCounts = new HashMap<ErrorCode, int>(); 582 HashMap<ErrorCode, int> expectedCounts = new HashMap<ErrorCode, int>();
606 for (ErrorCode code in expectedErrorCodes) { 583 for (ErrorCode code in expectedErrorCodes) {
607 int count = expectedCounts[code]; 584 int count = expectedCounts[code];
608 if (count == null) { 585 if (count == null) {
609 count = 1; 586 count = 1;
610 } else { 587 } else {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 for (int i = 0; i < actualErrors.length; i++) { 646 for (int i = 0; i < actualErrors.length; i++) {
670 AnalysisError error = actualErrors[i]; 647 AnalysisError error = actualErrors[i];
671 if (i > 0) { 648 if (i > 0) {
672 buffer.write(", "); 649 buffer.write(", ");
673 } 650 }
674 buffer.write(error.offset); 651 buffer.write(error.offset);
675 } 652 }
676 buffer.write(")"); 653 buffer.write(")");
677 }); 654 });
678 if (buffer.length > 0) { 655 if (buffer.length > 0) {
679 JUnitTestCase.fail(buffer.toString()); 656 fail(buffer.toString());
680 } 657 }
681 } 658 }
682 659
683 /** 660 /**
684 * Assert that the number of errors that have been gathered matches the number of severities that 661 * Assert that the number of errors that have been gathered matches the number of severities that
685 * are given and that there are the same number of errors and warnings as spec ified by the 662 * are given and that there are the same number of errors and warnings as spec ified by the
686 * argument. The order in which the errors were gathered is ignored. 663 * argument. The order in which the errors were gathered is ignored.
687 * 664 *
688 * @param expectedSeverities the severities of the errors that should have bee n gathered 665 * @param expectedSeverities the severities of the errors that should have bee n gathered
689 * @throws AssertionFailedError if a different number of errors have been gath ered than were 666 * @throws AssertionFailedError if a different number of errors have been gath ered than were
(...skipping 13 matching lines...) Expand all
703 int actualWarningCount = 0; 680 int actualWarningCount = 0;
704 for (AnalysisError error in _errors) { 681 for (AnalysisError error in _errors) {
705 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) { 682 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) {
706 actualErrorCount++; 683 actualErrorCount++;
707 } else { 684 } else {
708 actualWarningCount++; 685 actualWarningCount++;
709 } 686 }
710 } 687 }
711 if (expectedErrorCount != actualErrorCount || 688 if (expectedErrorCount != actualErrorCount ||
712 expectedWarningCount != actualWarningCount) { 689 expectedWarningCount != actualWarningCount) {
713 JUnitTestCase.fail( 690 fail("Expected $expectedErrorCount errors and $expectedWarningCount warnin gs, found $actualErrorCount errors and $actualWarningCount warnings");
714 "Expected $expectedErrorCount errors and $expectedWarningCount warning s, found $actualErrorCount errors and $actualWarningCount warnings");
715 } 691 }
716 } 692 }
717 693
718 /** 694 /**
719 * Assert that no errors have been gathered. 695 * Assert that no errors have been gathered.
720 * 696 *
721 * @throws AssertionFailedError if any errors have been gathered 697 * @throws AssertionFailedError if any errors have been gathered
722 */ 698 */
723 void assertNoErrors() { 699 void assertNoErrors() {
724 assertErrors(_NO_ERRORS); 700 assertErrors(_NO_ERRORS);
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 " %s %s (%d, %d/%d): %s", 840 " %s %s (%d, %d/%d): %s",
865 [ 841 [
866 source == null ? "" : source.shortName, 842 source == null ? "" : source.shortName,
867 error.errorCode, 843 error.errorCode,
868 location.lineNumber, 844 location.lineNumber,
869 location.columnNumber, 845 location.columnNumber,
870 error.length, 846 error.length,
871 error.message]); 847 error.message]);
872 } 848 }
873 } 849 }
874 JUnitTestCase.fail(buffer.toString()); 850 fail(buffer.toString());
875 } 851 }
876 852
877 /** 853 /**
878 * Search through the given list of errors for an error that is equal to the t arget error. If one 854 * Search through the given list of errors for an error that is equal to the t arget error. If one
879 * is found, remove it from the list and return `true`, otherwise return `fals e` 855 * is found, remove it from the list and return `true`, otherwise return `fals e`
880 * without modifying the list. 856 * without modifying the list.
881 * 857 *
882 * @param errors the errors through which we are searching 858 * @param errors the errors through which we are searching
883 * @param targetError the error being searched for 859 * @param targetError the error being searched for
884 * @return `true` if the error is found and removed from the list 860 * @return `true` if the error is found and removed from the list
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
991 void getContentsToReceiver(Source_ContentReceiver receiver) { 967 void getContentsToReceiver(Source_ContentReceiver receiver) {
992 throw new UnsupportedOperationException(); 968 throw new UnsupportedOperationException();
993 } 969 }
994 Source resolve(String uri) { 970 Source resolve(String uri) {
995 throw new UnsupportedOperationException(); 971 throw new UnsupportedOperationException();
996 } 972 }
997 Uri resolveRelativeUri(Uri uri) { 973 Uri resolveRelativeUri(Uri uri) {
998 return new Uri(scheme: 'file', path: _name).resolveUri(uri); 974 return new Uri(scheme: 'file', path: _name).resolveUri(uri);
999 } 975 }
1000 } 976 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698