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

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

Issue 259773005: New analyzer snapshot. Sorted unit members. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months 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
« no previous file with comments | « pkg/analyzer/test/generated/scanner_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 'package:analyzer/src/generated/java_core.dart'; 10 import 'package:analyzer/src/generated/java_core.dart';
11 import 'package:analyzer/src/generated/java_junit.dart'; 11 import 'package:analyzer/src/generated/java_junit.dart';
12 import 'package:analyzer/src/generated/java_engine.dart'; 12 import 'package:analyzer/src/generated/java_engine.dart';
13 import 'package:analyzer/src/generated/source.dart'; 13 import 'package:analyzer/src/generated/source.dart';
14 import 'package:analyzer/src/generated/error.dart'; 14 import 'package:analyzer/src/generated/error.dart';
15 import 'package:analyzer/src/generated/scanner.dart'; 15 import 'package:analyzer/src/generated/scanner.dart';
16 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator; 16 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator;
17 import 'package:analyzer/src/generated/element.dart' show InterfaceType, MethodE lement, PropertyAccessorElement; 17 import 'package:analyzer/src/generated/element.dart' show InterfaceType, MethodE lement, PropertyAccessorElement;
18 import 'package:analyzer/src/generated/engine.dart'; 18 import 'package:analyzer/src/generated/engine.dart';
19 import 'package:unittest/unittest.dart' as _ut; 19 import 'package:unittest/unittest.dart' as _ut;
20 20
21 /** 21 /**
22 * The class `EngineTestCase` defines utility methods for making assertions.
23 */
24 class EngineTestCase extends JUnitTestCase {
25 static int _PRINT_RANGE = 6;
26
27 /**
28 * Assert that the tokens in the actual stream of tokens have the same types a nd lexemes as the
29 * tokens in the expected stream of tokens. Note that this does not assert any thing about the
30 * offsets of the tokens (although the lengths will be equal).
31 *
32 * @param expectedStream the head of the stream of tokens that were expected
33 * @param actualStream the head of the stream of tokens that were actually fou nd
34 * @throws AssertionFailedError if the two streams of tokens are not the same
35 */
36 static void assertAllMatch(Token expectedStream, Token actualStream) {
37 Token left = expectedStream;
38 Token right = actualStream;
39 while (left.type != TokenType.EOF && right.type != TokenType.EOF) {
40 assertMatches(left, right);
41 left = left.next;
42 right = right.next;
43 }
44 }
45
46 /**
47 * Assert that the given collection is non-`null` and has the expected number of elements.
48 *
49 * @param expectedSize the expected number of elements
50 * @param c the collection being tested
51 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number
52 * of elements
53 */
54 static void assertCollectionSize(int expectedSize, Iterable c) {
55 if (c == null) {
56 JUnitTestCase.fail("Expected collection of size ${expectedSize}; found nul l");
57 } else if (c.length != expectedSize) {
58 JUnitTestCase.fail("Expected collection of size ${expectedSize}; contained ${c.length} elements");
59 }
60 }
61
62 /**
63 * Assert that the given array is non-`null` and contains the expected element s. The
64 * elements can appear in any order.
65 *
66 * @param array the array being tested
67 * @param expectedElements the expected elements
68 * @throws AssertionFailedError if the array is `null` or does not contain the expected
69 * elements
70 */
71 static void assertContains(List<Object> array, List<Object> expectedElements) {
72 int expectedSize = expectedElements.length;
73 if (array == null) {
74 JUnitTestCase.fail("Expected array of length ${expectedSize}; found null") ;
75 }
76 if (array.length != expectedSize) {
77 JUnitTestCase.fail("Expected array of length ${expectedSize}; contained ${ array.length} elements");
78 }
79 List<bool> found = new List<bool>.filled(expectedSize, false);
80 for (int i = 0; i < expectedSize; i++) {
81 _privateAssertContains(array, found, expectedElements[i]);
82 }
83 }
84
85 /**
86 * Assert that the array of actual values contain exactly the same values as t hose in the array of
87 * expected value, with the exception that the order of the elements is not re quired to be the
88 * same.
89 *
90 * @param expectedValues the values that are expected to be found
91 * @param actualValues the actual values that are being compared against the e xpected values
92 */
93 static void assertEqualsIgnoreOrder(List<Object> expectedValues, List<Object> actualValues) {
94 JUnitTestCase.assertNotNull(actualValues);
95 int expectedLength = expectedValues.length;
96 JUnitTestCase.assertEquals(expectedLength, actualValues.length);
97 List<bool> found = new List<bool>.filled(expectedLength, false);
98 for (int i = 0; i < expectedLength; i++) {
99 found[i] = false;
100 }
101 for (Object actualValue in actualValues) {
102 bool wasExpected = false;
103 for (int i = 0; i < expectedLength; i++) {
104 if (!found[i] && expectedValues[i] == actualValue) {
105 found[i] = true;
106 wasExpected = true;
107 break;
108 }
109 }
110 if (!wasExpected) {
111 JUnitTestCase.fail("The actual value ${actualValue} was not expected");
112 }
113 }
114 }
115
116 /**
117 * Assert that a given String is equal to an expected value.
118 *
119 * @param expected the expected String value
120 * @param actual the actual String value
121 */
122 static void assertEqualString(String expected, String actual) {
123 if (actual == null || expected == null) {
124 if (identical(actual, expected)) {
125 return;
126 }
127 if (actual == null) {
128 JUnitTestCase.assertTrueMsg("Content not as expected: is 'null' expected : ${expected}", false);
129 } else {
130 JUnitTestCase.assertTrueMsg("Content not as expected: expected 'null' is : ${actual}", false);
131 }
132 }
133 int diffPos = _getDiffPos(expected, actual);
134 if (diffPos != -1) {
135 int diffAhead = Math.max(0, diffPos - _PRINT_RANGE);
136 int diffAfter = Math.min(actual.length, diffPos + _PRINT_RANGE);
137 String diffStr = "${actual.substring(diffAhead, diffPos)}^${actual.substri ng(diffPos, diffAfter)}";
138 // use detailed message
139 String message = "Content not as expected: is\n${actual}\nDiffers at pos $ {diffPos}: ${diffStr}\nexpected:\n${expected}";
140 JUnitTestCase.assertEqualsMsg(message, expected, actual);
141 }
142 }
143
144 /**
145 * Assert that the given array is non-`null` and has exactly expected elements .
146 *
147 * @param array the array being tested
148 * @param expectedElements the expected elements
149 * @throws AssertionFailedError if the array is `null` or does not have the ex pected
150 * elements
151 */
152 static void assertExactElementsInArray(List<Object> array, List<Object> expect edElements) {
153 int expectedSize = expectedElements.length;
154 if (array == null) {
155 JUnitTestCase.fail("Expected array of size ${expectedSize}; found null");
156 }
157 if (array.length != expectedSize) {
158 JUnitTestCase.fail("Expected array of size ${expectedSize}; contained ${ar ray.length} elements");
159 }
160 for (int i = 0; i < expectedSize; i++) {
161 Object element = array[i];
162 Object expectedElement = expectedElements[i];
163 if (element != expectedElement) {
164 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme nt}");
165 }
166 }
167 }
168
169 /**
170 * Assert that the given list is non-`null` and has exactly expected elements.
171 *
172 * @param list the list being tested
173 * @param expectedElements the expected elements
174 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements
175 */
176 static void assertExactElementsInList(List list, List<Object> expectedElements ) {
177 int expectedSize = expectedElements.length;
178 if (list == null) {
179 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null");
180 }
181 if (list.length != expectedSize) {
182 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis t.length} elements");
183 }
184 for (int i = 0; i < expectedSize; i++) {
185 Object element = list[i];
186 Object expectedElement = expectedElements[i];
187 if (element != expectedElement) {
188 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme nt}");
189 }
190 }
191 }
192
193 /**
194 * Assert that the given list is non-`null` and has exactly expected elements.
195 *
196 * @param set the list being tested
197 * @param expectedElements the expected elements
198 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements
199 */
200 static void assertExactElementsInSet(Set set, List<Object> expectedElements) {
201 int expectedSize = expectedElements.length;
202 if (set == null) {
203 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null");
204 }
205 if (set.length != expectedSize) {
206 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${set .length} elements");
207 }
208 for (int i = 0; i < expectedSize; i++) {
209 Object expectedElement = expectedElements[i];
210 if (!set.contains(expectedElement)) {
211 JUnitTestCase.fail("Expected ${expectedElement} in set${set}");
212 }
213 }
214 }
215
216 /**
217 * Assert that the given object is an instance of the expected class.
218 *
219 * @param expectedClass the class that the object is expected to be an instanc e of
220 * @param object the object being tested
221 * @return the object that was being tested
222 * @throws Exception if the object is not an instance of the expected class
223 */
224 static Object assertInstanceOf(Predicate<Object> predicate, Type expectedClass , Object object) {
225 if (!predicate(object)) {
226 JUnitTestCase.fail("Expected instance of ${expectedClass.toString()}, foun d ${(object == null ? "null" : object.runtimeType.toString())}");
227 }
228 return object;
229 }
230
231 /**
232 * Assert that the given array is non-`null` and has the expected number of el ements.
233 *
234 * @param expectedLength the expected number of elements
235 * @param array the array being tested
236 * @throws AssertionFailedError if the array is `null` or does not have the ex pected number
237 * of elements
238 */
239 static void assertLength(int expectedLength, List<Object> array) {
240 if (array == null) {
241 JUnitTestCase.fail("Expected array of length ${expectedLength}; found null ");
242 } else if (array.length != expectedLength) {
243 JUnitTestCase.fail("Expected array of length ${expectedLength}; contained ${array.length} elements");
244 }
245 }
246
247 /**
248 * Assert that the actual token has the same type and lexeme as the expected t oken. Note that this
249 * does not assert anything about the offsets of the tokens (although the leng ths will be equal).
250 *
251 * @param expectedToken the token that was expected
252 * @param actualToken the token that was found
253 * @throws AssertionFailedError if the two tokens are not the same
254 */
255 static void assertMatches(Token expectedToken, Token actualToken) {
256 JUnitTestCase.assertEquals(expectedToken.type, actualToken.type);
257 if (expectedToken is KeywordToken) {
258 assertInstanceOf((obj) => obj is KeywordToken, KeywordToken, actualToken);
259 JUnitTestCase.assertEquals(expectedToken.keyword, (actualToken as KeywordT oken).keyword);
260 } else if (expectedToken is StringToken) {
261 assertInstanceOf((obj) => obj is StringToken, StringToken, actualToken);
262 JUnitTestCase.assertEquals(expectedToken.lexeme, (actualToken as StringTok en).lexeme);
263 }
264 }
265
266 /**
267 * Assert that the given list is non-`null` and has the expected number of ele ments.
268 *
269 * @param expectedSize the expected number of elements
270 * @param list the list being tested
271 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number
272 * of elements
273 */
274 static void assertSizeOfList(int expectedSize, List list) {
275 if (list == null) {
276 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null");
277 } else if (list.length != expectedSize) {
278 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis t.length} elements");
279 }
280 }
281
282 /**
283 * Assert that the given map is non-`null` and has the expected number of elem ents.
284 *
285 * @param expectedSize the expected number of elements
286 * @param map the map being tested
287 * @throws AssertionFailedError if the map is `null` or does not have the expe cted number of
288 * elements
289 */
290 static void assertSizeOfMap(int expectedSize, Map map) {
291 if (map == null) {
292 JUnitTestCase.fail("Expected map of size ${expectedSize}; found null");
293 } else if (map.length != expectedSize) {
294 JUnitTestCase.fail("Expected map of size ${expectedSize}; contained ${map. length} elements");
295 }
296 }
297
298 /**
299 * Assert that the given set is non-`null` and has the expected number of elem ents.
300 *
301 * @param expectedSize the expected number of elements
302 * @param set the set being tested
303 * @throws AssertionFailedError if the set is `null` or does not have the expe cted number of
304 * elements
305 */
306 static void assertSizeOfSet(int expectedSize, Set set) {
307 if (set == null) {
308 JUnitTestCase.fail("Expected set of size ${expectedSize}; found null");
309 } else if (set.length != expectedSize) {
310 JUnitTestCase.fail("Expected set of size ${expectedSize}; contained ${set. length} elements");
311 }
312 }
313
314 /**
315 * Convert the given array of lines into a single source string.
316 *
317 * @param lines the lines to be merged into a single source string
318 * @return the source string composed of the given lines
319 */
320 static String createSource(List<String> lines) {
321 PrintStringWriter writer = new PrintStringWriter();
322 for (String line in lines) {
323 writer.println(line);
324 }
325 return writer.toString();
326 }
327
328 /**
329 * @return the [AstNode] with requested type at offset of the "prefix".
330 */
331 static AstNode findNode(AstNode root, String code, String prefix, Predicate<As tNode> predicate) {
332 int offset = code.indexOf(prefix);
333 if (offset == -1) {
334 throw new IllegalArgumentException("Not found '${prefix}'.");
335 }
336 AstNode node = new NodeLocator.con1(offset).searchWithin(root);
337 return node.getAncestor(predicate);
338 }
339
340 /**
341 * Calculate the offset where the given strings differ.
342 *
343 * @param str1 the first String to compare
344 * @param str2 the second String to compare
345 * @return the offset at which the strings differ (or <code>-1</code> if they do not)
346 */
347 static int _getDiffPos(String str1, String str2) {
348 int len1 = Math.min(str1.length, str2.length);
349 int diffPos = -1;
350 for (int i = 0; i < len1; i++) {
351 if (str1.codeUnitAt(i) != str2.codeUnitAt(i)) {
352 diffPos = i;
353 break;
354 }
355 }
356 if (diffPos == -1 && str1.length != str2.length) {
357 diffPos = len1;
358 }
359 return diffPos;
360 }
361
362 static void _privateAssertContains(List<Object> array, List<bool> found, Objec t element) {
363 if (element == null) {
364 for (int i = 0; i < array.length; i++) {
365 if (!found[i]) {
366 if (array[i] == null) {
367 found[i] = true;
368 return;
369 }
370 }
371 }
372 JUnitTestCase.fail("Does not contain null");
373 } else {
374 for (int i = 0; i < array.length; i++) {
375 if (!found[i]) {
376 if (element == array[i]) {
377 found[i] = true;
378 return;
379 }
380 }
381 }
382 JUnitTestCase.fail("Does not contain ${element}");
383 }
384 }
385
386 AnalysisContextImpl createAnalysisContext() {
387 AnalysisContextImpl context = new AnalysisContextImpl();
388 context.sourceFactory = new SourceFactory([]);
389 return context;
390 }
391
392 /**
393 * Return the getter in the given type with the given name. Inherited getters are ignored.
394 *
395 * @param type the type in which the getter is declared
396 * @param getterName the name of the getter to be returned
397 * @return the property accessor element representing the getter with the give n name
398 */
399 PropertyAccessorElement getGetter(InterfaceType type, String getterName) {
400 for (PropertyAccessorElement accessor in type.element.accessors) {
401 if (accessor.isGetter && accessor.name == getterName) {
402 return accessor;
403 }
404 }
405 JUnitTestCase.fail("Could not find getter named ${getterName} in ${type.disp layName}");
406 return null;
407 }
408
409 /**
410 * Return the method in the given type with the given name. Inherited methods are ignored.
411 *
412 * @param type the type in which the method is declared
413 * @param methodName the name of the method to be returned
414 * @return the method element representing the method with the given name
415 */
416 MethodElement getMethod(InterfaceType type, String methodName) {
417 for (MethodElement method in type.element.methods) {
418 if (method.name == methodName) {
419 return method;
420 }
421 }
422 JUnitTestCase.fail("Could not find method named ${methodName} in ${type.disp layName}");
423 return null;
424 }
425
426 static dartSuite() {
427 _ut.group('EngineTestCase', () {
428 });
429 }
430 }
431
432 /**
22 * Instances of the class `GatheringErrorListener` implement an error listener t hat collects 433 * Instances of the class `GatheringErrorListener` implement an error listener t hat collects
23 * all of the errors passed to it for later examination. 434 * all of the errors passed to it for later examination.
24 */ 435 */
25 class GatheringErrorListener implements AnalysisErrorListener { 436 class GatheringErrorListener implements AnalysisErrorListener {
26 /** 437 /**
27 * The source being parsed. 438 * The source being parsed.
28 */ 439 */
29 final String _rawSource; 440 final String _rawSource;
30 441
31 /** 442 /**
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 for (AnalysisError error in errors) { 809 for (AnalysisError error in errors) {
399 if (_equalErrors(error, targetError)) { 810 if (_equalErrors(error, targetError)) {
400 errors.remove(error); 811 errors.remove(error);
401 return true; 812 return true;
402 } 813 }
403 } 814 }
404 return false; 815 return false;
405 } 816 }
406 } 817 }
407 818
408 /**
409 * The class `EngineTestCase` defines utility methods for making assertions.
410 */
411 class EngineTestCase extends JUnitTestCase {
412 static int _PRINT_RANGE = 6;
413
414 /**
415 * Assert that the tokens in the actual stream of tokens have the same types a nd lexemes as the
416 * tokens in the expected stream of tokens. Note that this does not assert any thing about the
417 * offsets of the tokens (although the lengths will be equal).
418 *
419 * @param expectedStream the head of the stream of tokens that were expected
420 * @param actualStream the head of the stream of tokens that were actually fou nd
421 * @throws AssertionFailedError if the two streams of tokens are not the same
422 */
423 static void assertAllMatch(Token expectedStream, Token actualStream) {
424 Token left = expectedStream;
425 Token right = actualStream;
426 while (left.type != TokenType.EOF && right.type != TokenType.EOF) {
427 assertMatches(left, right);
428 left = left.next;
429 right = right.next;
430 }
431 }
432
433 /**
434 * Assert that the given collection is non-`null` and has the expected number of elements.
435 *
436 * @param expectedSize the expected number of elements
437 * @param c the collection being tested
438 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number
439 * of elements
440 */
441 static void assertCollectionSize(int expectedSize, Iterable c) {
442 if (c == null) {
443 JUnitTestCase.fail("Expected collection of size ${expectedSize}; found nul l");
444 } else if (c.length != expectedSize) {
445 JUnitTestCase.fail("Expected collection of size ${expectedSize}; contained ${c.length} elements");
446 }
447 }
448
449 /**
450 * Assert that the given array is non-`null` and contains the expected element s. The
451 * elements can appear in any order.
452 *
453 * @param array the array being tested
454 * @param expectedElements the expected elements
455 * @throws AssertionFailedError if the array is `null` or does not contain the expected
456 * elements
457 */
458 static void assertContains(List<Object> array, List<Object> expectedElements) {
459 int expectedSize = expectedElements.length;
460 if (array == null) {
461 JUnitTestCase.fail("Expected array of length ${expectedSize}; found null") ;
462 }
463 if (array.length != expectedSize) {
464 JUnitTestCase.fail("Expected array of length ${expectedSize}; contained ${ array.length} elements");
465 }
466 List<bool> found = new List<bool>.filled(expectedSize, false);
467 for (int i = 0; i < expectedSize; i++) {
468 _privateAssertContains(array, found, expectedElements[i]);
469 }
470 }
471
472 /**
473 * Assert that the array of actual values contain exactly the same values as t hose in the array of
474 * expected value, with the exception that the order of the elements is not re quired to be the
475 * same.
476 *
477 * @param expectedValues the values that are expected to be found
478 * @param actualValues the actual values that are being compared against the e xpected values
479 */
480 static void assertEqualsIgnoreOrder(List<Object> expectedValues, List<Object> actualValues) {
481 JUnitTestCase.assertNotNull(actualValues);
482 int expectedLength = expectedValues.length;
483 JUnitTestCase.assertEquals(expectedLength, actualValues.length);
484 List<bool> found = new List<bool>.filled(expectedLength, false);
485 for (int i = 0; i < expectedLength; i++) {
486 found[i] = false;
487 }
488 for (Object actualValue in actualValues) {
489 bool wasExpected = false;
490 for (int i = 0; i < expectedLength; i++) {
491 if (!found[i] && expectedValues[i] == actualValue) {
492 found[i] = true;
493 wasExpected = true;
494 break;
495 }
496 }
497 if (!wasExpected) {
498 JUnitTestCase.fail("The actual value ${actualValue} was not expected");
499 }
500 }
501 }
502
503 /**
504 * Assert that a given String is equal to an expected value.
505 *
506 * @param expected the expected String value
507 * @param actual the actual String value
508 */
509 static void assertEqualString(String expected, String actual) {
510 if (actual == null || expected == null) {
511 if (identical(actual, expected)) {
512 return;
513 }
514 if (actual == null) {
515 JUnitTestCase.assertTrueMsg("Content not as expected: is 'null' expected : ${expected}", false);
516 } else {
517 JUnitTestCase.assertTrueMsg("Content not as expected: expected 'null' is : ${actual}", false);
518 }
519 }
520 int diffPos = _getDiffPos(expected, actual);
521 if (diffPos != -1) {
522 int diffAhead = Math.max(0, diffPos - _PRINT_RANGE);
523 int diffAfter = Math.min(actual.length, diffPos + _PRINT_RANGE);
524 String diffStr = "${actual.substring(diffAhead, diffPos)}^${actual.substri ng(diffPos, diffAfter)}";
525 // use detailed message
526 String message = "Content not as expected: is\n${actual}\nDiffers at pos $ {diffPos}: ${diffStr}\nexpected:\n${expected}";
527 JUnitTestCase.assertEqualsMsg(message, expected, actual);
528 }
529 }
530
531 /**
532 * Assert that the given array is non-`null` and has exactly expected elements .
533 *
534 * @param array the array being tested
535 * @param expectedElements the expected elements
536 * @throws AssertionFailedError if the array is `null` or does not have the ex pected
537 * elements
538 */
539 static void assertExactElementsInArray(List<Object> array, List<Object> expect edElements) {
540 int expectedSize = expectedElements.length;
541 if (array == null) {
542 JUnitTestCase.fail("Expected array of size ${expectedSize}; found null");
543 }
544 if (array.length != expectedSize) {
545 JUnitTestCase.fail("Expected array of size ${expectedSize}; contained ${ar ray.length} elements");
546 }
547 for (int i = 0; i < expectedSize; i++) {
548 Object element = array[i];
549 Object expectedElement = expectedElements[i];
550 if (element != expectedElement) {
551 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme nt}");
552 }
553 }
554 }
555
556 /**
557 * Assert that the given list is non-`null` and has exactly expected elements.
558 *
559 * @param list the list being tested
560 * @param expectedElements the expected elements
561 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements
562 */
563 static void assertExactElementsInList(List list, List<Object> expectedElements ) {
564 int expectedSize = expectedElements.length;
565 if (list == null) {
566 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null");
567 }
568 if (list.length != expectedSize) {
569 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis t.length} elements");
570 }
571 for (int i = 0; i < expectedSize; i++) {
572 Object element = list[i];
573 Object expectedElement = expectedElements[i];
574 if (element != expectedElement) {
575 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme nt}");
576 }
577 }
578 }
579
580 /**
581 * Assert that the given list is non-`null` and has exactly expected elements.
582 *
583 * @param set the list being tested
584 * @param expectedElements the expected elements
585 * @throws AssertionFailedError if the list is `null` or does not have the exp ected elements
586 */
587 static void assertExactElementsInSet(Set set, List<Object> expectedElements) {
588 int expectedSize = expectedElements.length;
589 if (set == null) {
590 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null");
591 }
592 if (set.length != expectedSize) {
593 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${set .length} elements");
594 }
595 for (int i = 0; i < expectedSize; i++) {
596 Object expectedElement = expectedElements[i];
597 if (!set.contains(expectedElement)) {
598 JUnitTestCase.fail("Expected ${expectedElement} in set${set}");
599 }
600 }
601 }
602
603 /**
604 * Assert that the given object is an instance of the expected class.
605 *
606 * @param expectedClass the class that the object is expected to be an instanc e of
607 * @param object the object being tested
608 * @return the object that was being tested
609 * @throws Exception if the object is not an instance of the expected class
610 */
611 static Object assertInstanceOf(Predicate<Object> predicate, Type expectedClass , Object object) {
612 if (!predicate(object)) {
613 JUnitTestCase.fail("Expected instance of ${expectedClass.toString()}, foun d ${(object == null ? "null" : object.runtimeType.toString())}");
614 }
615 return object;
616 }
617
618 /**
619 * Assert that the given array is non-`null` and has the expected number of el ements.
620 *
621 * @param expectedLength the expected number of elements
622 * @param array the array being tested
623 * @throws AssertionFailedError if the array is `null` or does not have the ex pected number
624 * of elements
625 */
626 static void assertLength(int expectedLength, List<Object> array) {
627 if (array == null) {
628 JUnitTestCase.fail("Expected array of length ${expectedLength}; found null ");
629 } else if (array.length != expectedLength) {
630 JUnitTestCase.fail("Expected array of length ${expectedLength}; contained ${array.length} elements");
631 }
632 }
633
634 /**
635 * Assert that the actual token has the same type and lexeme as the expected t oken. Note that this
636 * does not assert anything about the offsets of the tokens (although the leng ths will be equal).
637 *
638 * @param expectedToken the token that was expected
639 * @param actualToken the token that was found
640 * @throws AssertionFailedError if the two tokens are not the same
641 */
642 static void assertMatches(Token expectedToken, Token actualToken) {
643 JUnitTestCase.assertEquals(expectedToken.type, actualToken.type);
644 if (expectedToken is KeywordToken) {
645 assertInstanceOf((obj) => obj is KeywordToken, KeywordToken, actualToken);
646 JUnitTestCase.assertEquals(expectedToken.keyword, (actualToken as KeywordT oken).keyword);
647 } else if (expectedToken is StringToken) {
648 assertInstanceOf((obj) => obj is StringToken, StringToken, actualToken);
649 JUnitTestCase.assertEquals(expectedToken.lexeme, (actualToken as StringTok en).lexeme);
650 }
651 }
652
653 /**
654 * Assert that the given list is non-`null` and has the expected number of ele ments.
655 *
656 * @param expectedSize the expected number of elements
657 * @param list the list being tested
658 * @throws AssertionFailedError if the list is `null` or does not have the exp ected number
659 * of elements
660 */
661 static void assertSizeOfList(int expectedSize, List list) {
662 if (list == null) {
663 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null");
664 } else if (list.length != expectedSize) {
665 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis t.length} elements");
666 }
667 }
668
669 /**
670 * Assert that the given map is non-`null` and has the expected number of elem ents.
671 *
672 * @param expectedSize the expected number of elements
673 * @param map the map being tested
674 * @throws AssertionFailedError if the map is `null` or does not have the expe cted number of
675 * elements
676 */
677 static void assertSizeOfMap(int expectedSize, Map map) {
678 if (map == null) {
679 JUnitTestCase.fail("Expected map of size ${expectedSize}; found null");
680 } else if (map.length != expectedSize) {
681 JUnitTestCase.fail("Expected map of size ${expectedSize}; contained ${map. length} elements");
682 }
683 }
684
685 /**
686 * Assert that the given set is non-`null` and has the expected number of elem ents.
687 *
688 * @param expectedSize the expected number of elements
689 * @param set the set being tested
690 * @throws AssertionFailedError if the set is `null` or does not have the expe cted number of
691 * elements
692 */
693 static void assertSizeOfSet(int expectedSize, Set set) {
694 if (set == null) {
695 JUnitTestCase.fail("Expected set of size ${expectedSize}; found null");
696 } else if (set.length != expectedSize) {
697 JUnitTestCase.fail("Expected set of size ${expectedSize}; contained ${set. length} elements");
698 }
699 }
700
701 /**
702 * Convert the given array of lines into a single source string.
703 *
704 * @param lines the lines to be merged into a single source string
705 * @return the source string composed of the given lines
706 */
707 static String createSource(List<String> lines) {
708 PrintStringWriter writer = new PrintStringWriter();
709 for (String line in lines) {
710 writer.println(line);
711 }
712 return writer.toString();
713 }
714
715 /**
716 * @return the [AstNode] with requested type at offset of the "prefix".
717 */
718 static AstNode findNode(AstNode root, String code, String prefix, Predicate<As tNode> predicate) {
719 int offset = code.indexOf(prefix);
720 if (offset == -1) {
721 throw new IllegalArgumentException("Not found '${prefix}'.");
722 }
723 AstNode node = new NodeLocator.con1(offset).searchWithin(root);
724 return node.getAncestor(predicate);
725 }
726
727 /**
728 * Calculate the offset where the given strings differ.
729 *
730 * @param str1 the first String to compare
731 * @param str2 the second String to compare
732 * @return the offset at which the strings differ (or <code>-1</code> if they do not)
733 */
734 static int _getDiffPos(String str1, String str2) {
735 int len1 = Math.min(str1.length, str2.length);
736 int diffPos = -1;
737 for (int i = 0; i < len1; i++) {
738 if (str1.codeUnitAt(i) != str2.codeUnitAt(i)) {
739 diffPos = i;
740 break;
741 }
742 }
743 if (diffPos == -1 && str1.length != str2.length) {
744 diffPos = len1;
745 }
746 return diffPos;
747 }
748
749 static void _privateAssertContains(List<Object> array, List<bool> found, Objec t element) {
750 if (element == null) {
751 for (int i = 0; i < array.length; i++) {
752 if (!found[i]) {
753 if (array[i] == null) {
754 found[i] = true;
755 return;
756 }
757 }
758 }
759 JUnitTestCase.fail("Does not contain null");
760 } else {
761 for (int i = 0; i < array.length; i++) {
762 if (!found[i]) {
763 if (element == array[i]) {
764 found[i] = true;
765 return;
766 }
767 }
768 }
769 JUnitTestCase.fail("Does not contain ${element}");
770 }
771 }
772
773 AnalysisContextImpl createAnalysisContext() {
774 AnalysisContextImpl context = new AnalysisContextImpl();
775 context.sourceFactory = new SourceFactory([]);
776 return context;
777 }
778
779 /**
780 * Return the getter in the given type with the given name. Inherited getters are ignored.
781 *
782 * @param type the type in which the getter is declared
783 * @param getterName the name of the getter to be returned
784 * @return the property accessor element representing the getter with the give n name
785 */
786 PropertyAccessorElement getGetter(InterfaceType type, String getterName) {
787 for (PropertyAccessorElement accessor in type.element.accessors) {
788 if (accessor.isGetter && accessor.name == getterName) {
789 return accessor;
790 }
791 }
792 JUnitTestCase.fail("Could not find getter named ${getterName} in ${type.disp layName}");
793 return null;
794 }
795
796 /**
797 * Return the method in the given type with the given name. Inherited methods are ignored.
798 *
799 * @param type the type in which the method is declared
800 * @param methodName the name of the method to be returned
801 * @return the method element representing the method with the given name
802 */
803 MethodElement getMethod(InterfaceType type, String methodName) {
804 for (MethodElement method in type.element.methods) {
805 if (method.name == methodName) {
806 return method;
807 }
808 }
809 JUnitTestCase.fail("Could not find method named ${methodName} in ${type.disp layName}");
810 return null;
811 }
812
813 static dartSuite() {
814 _ut.group('EngineTestCase', () {
815 });
816 }
817 }
818
819 main() { 819 main() {
820 } 820 }
821 821
822 class TestSource implements Source { 822 class TestSource implements Source {
823 String _name; 823 String _name;
824 TestSource([this._name = '/test.dart']); 824 TestSource([this._name = '/test.dart']);
825 int get hashCode => 0; 825 int get hashCode => 0;
826 bool operator ==(Object object) { 826 bool operator ==(Object object) {
827 return object is TestSource; 827 return object is TestSource;
828 } 828 }
(...skipping 25 matching lines...) Expand all
854 Source resolveRelative(Uri uri) { 854 Source resolveRelative(Uri uri) {
855 throw new UnsupportedOperationException(); 855 throw new UnsupportedOperationException();
856 } 856 }
857 UriKind get uriKind { 857 UriKind get uriKind {
858 throw new UnsupportedOperationException(); 858 throw new UnsupportedOperationException();
859 } 859 }
860 TimestampedData<String> get contents { 860 TimestampedData<String> get contents {
861 throw new UnsupportedOperationException(); 861 throw new UnsupportedOperationException();
862 } 862 }
863 } 863 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/generated/scanner_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698