| OLD | NEW |
| 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/scanner.dart'; | 19 import 'package:analyzer/src/generated/scanner.dart'; |
| 20 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 21 import 'package:unittest/unittest.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 { | 27 class EngineTestCase { |
| 28 static int _PRINT_RANGE = 6; | |
| 29 | |
| 30 void setUp() {} | 28 void setUp() {} |
| 31 | 29 |
| 32 void tearDown() {} | 30 void tearDown() {} |
| 33 | 31 |
| 34 /** | 32 /** |
| 35 * Assert that the given collection has the same number of elements as the num
ber of specified | 33 * Assert that the given collection has the same number of elements as the num
ber of specified |
| 36 * names, and that for each specified name, a corresponding element can be fou
nd in the given | 34 * names, and that for each specified name, a corresponding element can be fou
nd in the given |
| 37 * collection with that name. | 35 * collection with that name. |
| 38 * | 36 * |
| 39 * @param elements the elements | 37 * @param elements the elements |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 for (MethodElement method in type.element.methods) { | 95 for (MethodElement method in type.element.methods) { |
| 98 if (method.name == methodName) { | 96 if (method.name == methodName) { |
| 99 return method; | 97 return method; |
| 100 } | 98 } |
| 101 } | 99 } |
| 102 fail("Could not find method named $methodName in ${type.displayName}"); | 100 fail("Could not find method named $methodName in ${type.displayName}"); |
| 103 return null; | 101 return null; |
| 104 } | 102 } |
| 105 | 103 |
| 106 /** | 104 /** |
| 107 * Assert that the tokens in the actual stream of tokens have the same types a
nd lexemes as the | |
| 108 * tokens in the expected stream of tokens. Note that this does not assert any
thing about the | |
| 109 * offsets of the tokens (although the lengths will be equal). | |
| 110 * | |
| 111 * @param expectedStream the head of the stream of tokens that were expected | |
| 112 * @param actualStream the head of the stream of tokens that were actually fou
nd | |
| 113 * @throws AssertionFailedError if the two streams of tokens are not the same | |
| 114 */ | |
| 115 static void assertAllMatch(Token expectedStream, Token actualStream) { | |
| 116 Token left = expectedStream; | |
| 117 Token right = actualStream; | |
| 118 while (left.type != TokenType.EOF && right.type != TokenType.EOF) { | |
| 119 assertMatches(left, right); | |
| 120 left = left.next; | |
| 121 right = right.next; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Assert that the given array is non-`null` and contains the expected element
s. The | 105 * Assert that the given array is non-`null` and contains the expected element
s. The |
| 127 * elements can appear in any order. | 106 * elements can appear in any order. |
| 128 * | 107 * |
| 129 * @param array the array being tested | 108 * @param array the array being tested |
| 130 * @param expectedElements the expected elements | 109 * @param expectedElements the expected elements |
| 131 * @throws AssertionFailedError if the array is `null` or does not contain the
expected | 110 * @throws AssertionFailedError if the array is `null` or does not contain the
expected |
| 132 * elements | 111 * elements |
| 133 */ | 112 */ |
| 134 static void assertContains(List<Object> array, | 113 static void assertContains(List<Object> array, |
| 135 List<Object> expectedElements) { | 114 List<Object> expectedElements) { |
| 136 int expectedSize = expectedElements.length; | 115 int expectedSize = expectedElements.length; |
| 137 if (array == null) { | 116 if (array == null) { |
| 138 fail("Expected array of length $expectedSize; found null"); | 117 fail("Expected array of length $expectedSize; found null"); |
| 139 } | 118 } |
| 140 if (array.length != expectedSize) { | 119 if (array.length != expectedSize) { |
| 141 fail("Expected array of length $expectedSize; contained ${array.length} el
ements"); | 120 fail("Expected array of length $expectedSize; contained ${array.length} el
ements"); |
| 142 } | 121 } |
| 143 List<bool> found = new List<bool>.filled(expectedSize, false); | 122 List<bool> found = new List<bool>.filled(expectedSize, false); |
| 144 for (int i = 0; i < expectedSize; i++) { | 123 for (int i = 0; i < expectedSize; i++) { |
| 145 _privateAssertContains(array, found, expectedElements[i]); | 124 _privateAssertContains(array, found, expectedElements[i]); |
| 146 } | 125 } |
| 147 } | 126 } |
| 148 | 127 |
| 149 /** | 128 /** |
| 150 * Assert that a given String is equal to an expected value. | |
| 151 * | |
| 152 * @param expected the expected String value | |
| 153 * @param actual the actual String value | |
| 154 */ | |
| 155 static void assertEqualString(String expected, String actual) { | |
| 156 if (actual == null || expected == null) { | |
| 157 if (identical(actual, expected)) { | |
| 158 return; | |
| 159 } | |
| 160 if (actual == null) { | |
| 161 fail("Content not as expected: is 'null' expected: $expected"); | |
| 162 } else { | |
| 163 fail("Content not as expected: expected 'null' is: $actual"); | |
| 164 } | |
| 165 } | |
| 166 int diffPos = _getDiffPos(expected, actual); | |
| 167 if (diffPos != -1) { | |
| 168 int diffAhead = math.max(0, diffPos - _PRINT_RANGE); | |
| 169 int diffAfter = math.min(actual.length, diffPos + _PRINT_RANGE); | |
| 170 String diffStr = | |
| 171 "${actual.substring(diffAhead, diffPos)}^${actual.substring(diffPos, d
iffAfter)}"; | |
| 172 // use detailed message | |
| 173 String message = | |
| 174 "Content not as expected: is\n$actual\nDiffers at pos $diffPos: $diffS
tr\nexpected:\n$expected"; | |
| 175 expect(actual, expected, reason: message); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 /** | |
| 180 * Assert that the array of actual values contain exactly the same values as t
hose in the array of | 129 * Assert that the array of actual values contain exactly the same values as t
hose in the array of |
| 181 * expected value, with the exception that the order of the elements is not re
quired to be the | 130 * expected value, with the exception that the order of the elements is not re
quired to be the |
| 182 * same. | 131 * same. |
| 183 * | 132 * |
| 184 * @param expectedValues the values that are expected to be found | 133 * @param expectedValues the values that are expected to be found |
| 185 * @param actualValues the actual values that are being compared against the e
xpected values | 134 * @param actualValues the actual values that are being compared against the e
xpected values |
| 186 */ | 135 */ |
| 187 static void assertEqualsIgnoreOrder(List<Object> expectedValues, | 136 static void assertEqualsIgnoreOrder(List<Object> expectedValues, |
| 188 List<Object> actualValues) { | 137 List<Object> actualValues) { |
| 189 expect(actualValues, isNotNull); | 138 expect(actualValues, isNotNull); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 202 break; | 151 break; |
| 203 } | 152 } |
| 204 } | 153 } |
| 205 if (!wasExpected) { | 154 if (!wasExpected) { |
| 206 fail("The actual value $actualValue was not expected"); | 155 fail("The actual value $actualValue was not expected"); |
| 207 } | 156 } |
| 208 } | 157 } |
| 209 } | 158 } |
| 210 | 159 |
| 211 /** | 160 /** |
| 212 * Assert that the given array is non-`null` and has exactly expected elements
. | |
| 213 * | |
| 214 * @param array the array being tested | |
| 215 * @param expectedElements the expected elements | |
| 216 * @throws AssertionFailedError if the array is `null` or does not have the ex
pected | |
| 217 * elements | |
| 218 */ | |
| 219 static void assertExactElementsInArray(List<Object> array, | |
| 220 List<Object> expectedElements) { | |
| 221 int expectedSize = expectedElements.length; | |
| 222 if (array == null) { | |
| 223 fail("Expected array of size $expectedSize; found null"); | |
| 224 } | |
| 225 if (array.length != expectedSize) { | |
| 226 fail("Expected array of size $expectedSize; contained ${array.length} elem
ents"); | |
| 227 } | |
| 228 for (int i = 0; i < expectedSize; i++) { | |
| 229 Object element = array[i]; | |
| 230 Object expectedElement = expectedElements[i]; | |
| 231 if (element != expectedElement) { | |
| 232 fail("Expected $expectedElement at [$i]; found $element"); | |
| 233 } | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 /** | |
| 238 * Assert that the given list is non-`null` and has exactly expected elements. | |
| 239 * | |
| 240 * @param list the list being tested | |
| 241 * @param expectedElements the expected elements | |
| 242 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected elements | |
| 243 */ | |
| 244 static void assertExactElementsInList(List list, | |
| 245 List<Object> expectedElements) { | |
| 246 int expectedSize = expectedElements.length; | |
| 247 if (list == null) { | |
| 248 fail("Expected list of size $expectedSize; found null"); | |
| 249 } | |
| 250 if (list.length != expectedSize) { | |
| 251 fail("Expected list of size $expectedSize; contained ${list.length} elemen
ts"); | |
| 252 } | |
| 253 for (int i = 0; i < expectedSize; i++) { | |
| 254 Object element = list[i]; | |
| 255 Object expectedElement = expectedElements[i]; | |
| 256 if (element != expectedElement) { | |
| 257 fail("Expected $expectedElement at [$i]; found $element"); | |
| 258 } | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 /** | |
| 263 * Assert that the given list is non-`null` and has exactly expected elements. | |
| 264 * | |
| 265 * @param set the list being tested | |
| 266 * @param expectedElements the expected elements | |
| 267 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected elements | |
| 268 */ | |
| 269 static void assertExactElementsInSet(Set set, List<Object> expectedElements) { | |
| 270 int expectedSize = expectedElements.length; | |
| 271 if (set == null) { | |
| 272 fail("Expected list of size $expectedSize; found null"); | |
| 273 } | |
| 274 if (set.length != expectedSize) { | |
| 275 fail("Expected list of size $expectedSize; contained ${set.length} element
s"); | |
| 276 } | |
| 277 for (int i = 0; i < expectedSize; i++) { | |
| 278 Object expectedElement = expectedElements[i]; | |
| 279 if (!set.contains(expectedElement)) { | |
| 280 fail("Expected $expectedElement in set$set"); | |
| 281 } | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 /** | |
| 286 * Assert that the given object is an instance of the expected class. | 161 * Assert that the given object is an instance of the expected class. |
| 287 * | 162 * |
| 288 * @param expectedClass the class that the object is expected to be an instanc
e of | 163 * @param expectedClass the class that the object is expected to be an instanc
e of |
| 289 * @param object the object being tested | 164 * @param object the object being tested |
| 290 * @return the object that was being tested | 165 * @return the object that was being tested |
| 291 * @throws Exception if the object is not an instance of the expected class | 166 * @throws Exception if the object is not an instance of the expected class |
| 292 */ | 167 */ |
| 293 static Object assertInstanceOf(Predicate<Object> predicate, | 168 static Object assertInstanceOf(Predicate<Object> predicate, |
| 294 Type expectedClass, Object object) { | 169 Type expectedClass, Object object) { |
| 295 if (!predicate(object)) { | 170 if (!predicate(object)) { |
| 296 fail("Expected instance of $expectedClass, found ${object == null ? "null"
: object.runtimeType}"); | 171 fail("Expected instance of $expectedClass, found ${object == null ? "null"
: object.runtimeType}"); |
| 297 } | 172 } |
| 298 return object; | 173 return object; |
| 299 } | 174 } |
| 300 | 175 |
| 301 /** | 176 /** |
| 302 * Assert that the actual token has the same type and lexeme as the expected t
oken. Note that this | |
| 303 * does not assert anything about the offsets of the tokens (although the leng
ths will be equal). | |
| 304 * | |
| 305 * @param expectedToken the token that was expected | |
| 306 * @param actualToken the token that was found | |
| 307 * @throws AssertionFailedError if the two tokens are not the same | |
| 308 */ | |
| 309 static void assertMatches(Token expectedToken, Token actualToken) { | |
| 310 expect(actualToken.type, expectedToken.type); | |
| 311 if (expectedToken is KeywordToken) { | |
| 312 assertInstanceOf((obj) => obj is KeywordToken, KeywordToken, actualToken); | |
| 313 expect((actualToken as KeywordToken).keyword, expectedToken.keyword); | |
| 314 } else if (expectedToken is StringToken) { | |
| 315 assertInstanceOf((obj) => obj is StringToken, StringToken, actualToken); | |
| 316 expect((actualToken as StringToken).lexeme, expectedToken.lexeme); | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 | |
| 321 /** | |
| 322 * @return the [AstNode] with requested type at offset of the "prefix". | 177 * @return the [AstNode] with requested type at offset of the "prefix". |
| 323 */ | 178 */ |
| 324 static AstNode findNode(AstNode root, String code, String prefix, | 179 static AstNode findNode(AstNode root, String code, String prefix, |
| 325 Predicate<AstNode> predicate) { | 180 Predicate<AstNode> predicate) { |
| 326 int offset = code.indexOf(prefix); | 181 int offset = code.indexOf(prefix); |
| 327 if (offset == -1) { | 182 if (offset == -1) { |
| 328 throw new IllegalArgumentException("Not found '$prefix'."); | 183 throw new IllegalArgumentException("Not found '$prefix'."); |
| 329 } | 184 } |
| 330 AstNode node = new NodeLocator.con1(offset).searchWithin(root); | 185 AstNode node = new NodeLocator.con1(offset).searchWithin(root); |
| 331 return node.getAncestor(predicate); | 186 return node.getAncestor(predicate); |
| 332 } | 187 } |
| 333 | 188 |
| 334 /** | |
| 335 * Calculate the offset where the given strings differ. | |
| 336 * | |
| 337 * @param str1 the first String to compare | |
| 338 * @param str2 the second String to compare | |
| 339 * @return the offset at which the strings differ (or <code>-1</code> if they
do not) | |
| 340 */ | |
| 341 static int _getDiffPos(String str1, String str2) { | |
| 342 int len1 = math.min(str1.length, str2.length); | |
| 343 int diffPos = -1; | |
| 344 for (int i = 0; i < len1; i++) { | |
| 345 if (str1.codeUnitAt(i) != str2.codeUnitAt(i)) { | |
| 346 diffPos = i; | |
| 347 break; | |
| 348 } | |
| 349 } | |
| 350 if (diffPos == -1 && str1.length != str2.length) { | |
| 351 diffPos = len1; | |
| 352 } | |
| 353 return diffPos; | |
| 354 } | |
| 355 | |
| 356 static void _privateAssertContains(List<Object> array, List<bool> found, | 189 static void _privateAssertContains(List<Object> array, List<bool> found, |
| 357 Object element) { | 190 Object element) { |
| 358 if (element == null) { | 191 if (element == null) { |
| 359 for (int i = 0; i < array.length; i++) { | 192 for (int i = 0; i < array.length; i++) { |
| 360 if (!found[i]) { | 193 if (!found[i]) { |
| 361 if (array[i] == null) { | 194 if (array[i] == null) { |
| 362 found[i] = true; | 195 found[i] = true; |
| 363 return; | 196 return; |
| 364 } | 197 } |
| 365 } | 198 } |
| (...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 void getContentsToReceiver(Source_ContentReceiver receiver) { | 721 void getContentsToReceiver(Source_ContentReceiver receiver) { |
| 889 throw new UnsupportedOperationException(); | 722 throw new UnsupportedOperationException(); |
| 890 } | 723 } |
| 891 Source resolve(String uri) { | 724 Source resolve(String uri) { |
| 892 throw new UnsupportedOperationException(); | 725 throw new UnsupportedOperationException(); |
| 893 } | 726 } |
| 894 Uri resolveRelativeUri(Uri uri) { | 727 Uri resolveRelativeUri(Uri uri) { |
| 895 return new Uri(scheme: 'file', path: _name).resolveUri(uri); | 728 return new Uri(scheme: 'file', path: _name).resolveUri(uri); |
| 896 } | 729 } |
| 897 } | 730 } |
| OLD | NEW |