| OLD | NEW |
| (Empty) |
| 1 // This code was auto-generated, is not intended to be edited, and is subject to | |
| 2 // significant change. Please see the README file for more information. | |
| 3 | |
| 4 library engine.test_support; | |
| 5 | |
| 6 import 'dart:collection'; | |
| 7 import 'dart:uri'; | |
| 8 import 'package:analyzer-experimental/src/generated/java_core.dart'; | |
| 9 import 'package:analyzer-experimental/src/generated/java_engine.dart'; | |
| 10 import 'package:analyzer-experimental/src/generated/java_junit.dart'; | |
| 11 import 'package:analyzer-experimental/src/generated/source.dart'; | |
| 12 import 'package:analyzer-experimental/src/generated/error.dart'; | |
| 13 import 'package:analyzer-experimental/src/generated/scanner.dart'; | |
| 14 import 'package:analyzer-experimental/src/generated/element.dart' show Interface
Type, MethodElement, PropertyAccessorElement; | |
| 15 import 'package:unittest/unittest.dart' as _ut; | |
| 16 | |
| 17 /** | |
| 18 * Instances of the class {@code GatheringErrorListener} implement an error list
ener that collects | |
| 19 * all of the errors passed to it for later examination. | |
| 20 */ | |
| 21 class GatheringErrorListener implements AnalysisErrorListener { | |
| 22 /** | |
| 23 * The source being parsed. | |
| 24 */ | |
| 25 String _rawSource; | |
| 26 /** | |
| 27 * The source being parsed after inserting a marker at the beginning and end o
f the range of the | |
| 28 * most recent error. | |
| 29 */ | |
| 30 String _markedSource; | |
| 31 /** | |
| 32 * A list containing the errors that were collected. | |
| 33 */ | |
| 34 List<AnalysisError> _errors = new List<AnalysisError>(); | |
| 35 /** | |
| 36 * A table mapping sources to the line information for the source. | |
| 37 */ | |
| 38 Map<Source, LineInfo> _lineInfoMap = new Map<Source, LineInfo>(); | |
| 39 /** | |
| 40 * An empty array of errors used when no errors are expected. | |
| 41 */ | |
| 42 static List<AnalysisError> _NO_ERRORS = new List<AnalysisError>(0); | |
| 43 /** | |
| 44 * Initialize a newly created error listener to collect errors. | |
| 45 */ | |
| 46 GatheringErrorListener() : super() { | |
| 47 _jtd_constructor_304_impl(); | |
| 48 } | |
| 49 _jtd_constructor_304_impl() { | |
| 50 } | |
| 51 /** | |
| 52 * Initialize a newly created error listener to collect errors. | |
| 53 */ | |
| 54 GatheringErrorListener.con1(String rawSource2) { | |
| 55 _jtd_constructor_305_impl(rawSource2); | |
| 56 } | |
| 57 _jtd_constructor_305_impl(String rawSource2) { | |
| 58 this._rawSource = rawSource2; | |
| 59 this._markedSource = rawSource2; | |
| 60 } | |
| 61 /** | |
| 62 * Assert that the number of errors that have been gathered matches the number
of errors that are | |
| 63 * given and that they have the expected error codes and locations. The order
in which the errors | |
| 64 * were gathered is ignored. | |
| 65 * @param errorCodes the errors that should have been gathered | |
| 66 * @throws AssertionFailedError if a different number of errors have been gath
ered than were | |
| 67 * expected or if they do not have the same codes and locations | |
| 68 */ | |
| 69 void assertErrors(List<AnalysisError> expectedErrors) { | |
| 70 if (_errors.length != expectedErrors.length) { | |
| 71 fail(expectedErrors); | |
| 72 } | |
| 73 List<AnalysisError> remainingErrors = new List<AnalysisError>(); | |
| 74 for (AnalysisError error in expectedErrors) { | |
| 75 remainingErrors.add(error); | |
| 76 } | |
| 77 for (AnalysisError error in _errors) { | |
| 78 if (!foundAndRemoved(remainingErrors, error)) { | |
| 79 fail(expectedErrors); | |
| 80 } | |
| 81 } | |
| 82 } | |
| 83 /** | |
| 84 * Assert that the number of errors that have been gathered matches the number
of errors that are | |
| 85 * given and that they have the expected error codes. The order in which the e
rrors were gathered | |
| 86 * is ignored. | |
| 87 * @param expectedErrorCodes the error codes of the errors that should have be
en gathered | |
| 88 * @throws AssertionFailedError if a different number of errors have been gath
ered than were | |
| 89 * expected | |
| 90 */ | |
| 91 void assertErrors2(List<ErrorCode> expectedErrorCodes) { | |
| 92 JavaStringBuilder builder = new JavaStringBuilder(); | |
| 93 Map<ErrorCode, int> expectedCounts = new Map<ErrorCode, int>(); | |
| 94 for (ErrorCode code in expectedErrorCodes) { | |
| 95 int count = expectedCounts[code]; | |
| 96 if (count == null) { | |
| 97 count = 1; | |
| 98 } else { | |
| 99 count = count + 1; | |
| 100 } | |
| 101 expectedCounts[code] = count; | |
| 102 } | |
| 103 Map<ErrorCode, List<AnalysisError>> errorsByCode = new Map<ErrorCode, List<A
nalysisError>>(); | |
| 104 for (AnalysisError error in _errors) { | |
| 105 ErrorCode code = error.errorCode; | |
| 106 List<AnalysisError> list = errorsByCode[code]; | |
| 107 if (list == null) { | |
| 108 list = new List<AnalysisError>(); | |
| 109 errorsByCode[code] = list; | |
| 110 } | |
| 111 list.add(error); | |
| 112 } | |
| 113 for (MapEntry<ErrorCode, int> entry in getMapEntrySet(expectedCounts)) { | |
| 114 ErrorCode code = entry.getKey(); | |
| 115 int expectedCount = entry.getValue(); | |
| 116 int actualCount; | |
| 117 List<AnalysisError> list = errorsByCode.remove(code); | |
| 118 if (list == null) { | |
| 119 actualCount = 0; | |
| 120 } else { | |
| 121 actualCount = list.length; | |
| 122 } | |
| 123 if (actualCount != expectedCount) { | |
| 124 if (builder.length == 0) { | |
| 125 builder.append("Expected "); | |
| 126 } else { | |
| 127 builder.append("; "); | |
| 128 } | |
| 129 builder.append(expectedCount); | |
| 130 builder.append(" errors of type "); | |
| 131 builder.append(code); | |
| 132 builder.append(", found "); | |
| 133 builder.append(actualCount); | |
| 134 } | |
| 135 } | |
| 136 for (MapEntry<ErrorCode, List<AnalysisError>> entry in getMapEntrySet(errors
ByCode)) { | |
| 137 ErrorCode code = entry.getKey(); | |
| 138 List<AnalysisError> actualErrors = entry.getValue(); | |
| 139 int actualCount = actualErrors.length; | |
| 140 if (builder.length == 0) { | |
| 141 builder.append("Expected "); | |
| 142 } else { | |
| 143 builder.append("; "); | |
| 144 } | |
| 145 builder.append("0 errors of type "); | |
| 146 builder.append(code); | |
| 147 builder.append(", found "); | |
| 148 builder.append(actualCount); | |
| 149 builder.append(" ("); | |
| 150 for (int i = 0; i < actualErrors.length; i++) { | |
| 151 AnalysisError error = actualErrors[i]; | |
| 152 if (i > 0) { | |
| 153 builder.append(", "); | |
| 154 } | |
| 155 builder.append(error.offset); | |
| 156 } | |
| 157 builder.append(")"); | |
| 158 } | |
| 159 if (builder.length > 0) { | |
| 160 JUnitTestCase.fail(builder.toString()); | |
| 161 } | |
| 162 } | |
| 163 /** | |
| 164 * Assert that the number of errors that have been gathered matches the number
of severities that | |
| 165 * are given and that there are the same number of errors and warnings as spec
ified by the | |
| 166 * argument. The order in which the errors were gathered is ignored. | |
| 167 * @param expectedSeverities the severities of the errors that should have bee
n gathered | |
| 168 * @throws AssertionFailedError if a different number of errors have been gath
ered than were | |
| 169 * expected | |
| 170 */ | |
| 171 void assertErrors3(List<ErrorSeverity> expectedSeverities) { | |
| 172 int expectedErrorCount = 0; | |
| 173 int expectedWarningCount = 0; | |
| 174 for (ErrorSeverity severity in expectedSeverities) { | |
| 175 if (identical(severity, ErrorSeverity.ERROR)) { | |
| 176 expectedErrorCount++; | |
| 177 } else { | |
| 178 expectedWarningCount++; | |
| 179 } | |
| 180 } | |
| 181 int actualErrorCount = 0; | |
| 182 int actualWarningCount = 0; | |
| 183 for (AnalysisError error in _errors) { | |
| 184 if (identical(error.errorCode.errorSeverity, ErrorSeverity.ERROR)) { | |
| 185 actualErrorCount++; | |
| 186 } else { | |
| 187 actualWarningCount++; | |
| 188 } | |
| 189 } | |
| 190 if (expectedErrorCount != actualErrorCount || expectedWarningCount != actual
WarningCount) { | |
| 191 JUnitTestCase.fail("Expected ${expectedErrorCount} errors and ${expectedWa
rningCount} warnings, found ${actualErrorCount} errors and ${actualWarningCount}
warnings"); | |
| 192 } | |
| 193 } | |
| 194 /** | |
| 195 * Assert that no errors have been gathered. | |
| 196 * @throws AssertionFailedError if any errors have been gathered | |
| 197 */ | |
| 198 void assertNoErrors() { | |
| 199 assertErrors(_NO_ERRORS); | |
| 200 } | |
| 201 /** | |
| 202 * Return the errors that were collected. | |
| 203 * @return the errors that were collected | |
| 204 */ | |
| 205 List<AnalysisError> get errors => _errors; | |
| 206 /** | |
| 207 * Return {@code true} if an error with the given error code has been gathered
. | |
| 208 * @param errorCode the error code being searched for | |
| 209 * @return {@code true} if an error with the given error code has been gathere
d | |
| 210 */ | |
| 211 bool hasError(ErrorCode errorCode4) { | |
| 212 for (AnalysisError error in _errors) { | |
| 213 if (identical(error.errorCode, errorCode4)) { | |
| 214 return true; | |
| 215 } | |
| 216 } | |
| 217 return false; | |
| 218 } | |
| 219 void onError(AnalysisError error) { | |
| 220 if (_rawSource != null) { | |
| 221 int left = error.offset; | |
| 222 int right = left + error.length - 1; | |
| 223 _markedSource = "${_rawSource.substring(0, left)}^${_rawSource.substring(l
eft, right)}^${_rawSource.substring(right)}"; | |
| 224 } | |
| 225 _errors.add(error); | |
| 226 } | |
| 227 /** | |
| 228 * Set the line information associated with the given source to the given info
rmation. | |
| 229 * @param source the source with which the line information is associated | |
| 230 * @param lineStarts the line start information to be associated with the sour
ce | |
| 231 */ | |
| 232 void setLineInfo(Source source, List<int> lineStarts) { | |
| 233 _lineInfoMap[source] = new LineInfo(lineStarts); | |
| 234 } | |
| 235 /** | |
| 236 * Set the line information associated with the given source to the given info
rmation. | |
| 237 * @param source the source with which the line information is associated | |
| 238 * @param lineInfo the line information to be associated with the source | |
| 239 */ | |
| 240 void setLineInfo2(Source source, LineInfo lineInfo) { | |
| 241 _lineInfoMap[source] = lineInfo; | |
| 242 } | |
| 243 /** | |
| 244 * Return {@code true} if the two errors are equivalent. | |
| 245 * @param firstError the first error being compared | |
| 246 * @param secondError the second error being compared | |
| 247 * @return {@code true} if the two errors are equivalent | |
| 248 */ | |
| 249 bool equals(AnalysisError firstError, AnalysisError secondError) => identical(
firstError.errorCode, secondError.errorCode) && firstError.offset == secondError
.offset && firstError.length == secondError.length && equals3(firstError.source,
secondError.source); | |
| 250 /** | |
| 251 * Return {@code true} if the two sources are equivalent. | |
| 252 * @param firstSource the first source being compared | |
| 253 * @param secondSource the second source being compared | |
| 254 * @return {@code true} if the two sources are equivalent | |
| 255 */ | |
| 256 bool equals3(Source firstSource, Source secondSource) { | |
| 257 if (firstSource == null) { | |
| 258 return secondSource == null; | |
| 259 } else if (secondSource == null) { | |
| 260 return false; | |
| 261 } | |
| 262 return firstSource == secondSource; | |
| 263 } | |
| 264 /** | |
| 265 * Assert that the number of errors that have been gathered matches the number
of errors that are | |
| 266 * given and that they have the expected error codes. The order in which the e
rrors were gathered | |
| 267 * is ignored. | |
| 268 * @param errorCodes the errors that should have been gathered | |
| 269 * @throws AssertionFailedError with | |
| 270 */ | |
| 271 void fail(List<AnalysisError> expectedErrors) { | |
| 272 PrintStringWriter writer = new PrintStringWriter(); | |
| 273 writer.print("Expected "); | |
| 274 writer.print(expectedErrors.length); | |
| 275 writer.print(" errors:"); | |
| 276 for (AnalysisError error in expectedErrors) { | |
| 277 Source source11 = error.source; | |
| 278 LineInfo lineInfo = _lineInfoMap[source11]; | |
| 279 writer.println(); | |
| 280 if (lineInfo == null) { | |
| 281 int offset10 = error.offset; | |
| 282 writer.printf(" %s %s (%d..%d)", [source11 == null ? "" : source11.shor
tName, error.errorCode, offset10, offset10 + error.length]); | |
| 283 } else { | |
| 284 LineInfo_Location location = lineInfo.getLocation(error.offset); | |
| 285 writer.printf(" %s %s (%d, %d/%d)", [source11 == null ? "" : source11.s
hortName, error.errorCode, location.lineNumber, location.columnNumber, error.len
gth]); | |
| 286 } | |
| 287 } | |
| 288 writer.println(); | |
| 289 writer.print("found "); | |
| 290 writer.print(_errors.length); | |
| 291 writer.print(" errors:"); | |
| 292 for (AnalysisError error in _errors) { | |
| 293 Source source12 = error.source; | |
| 294 LineInfo lineInfo = _lineInfoMap[source12]; | |
| 295 writer.println(); | |
| 296 if (lineInfo == null) { | |
| 297 int offset11 = error.offset; | |
| 298 writer.printf(" %s %s (%d..%d): %s", [source12 == null ? "" : source12.
shortName, error.errorCode, offset11, offset11 + error.length, error.message]); | |
| 299 } else { | |
| 300 LineInfo_Location location = lineInfo.getLocation(error.offset); | |
| 301 writer.printf(" %s %s (%d, %d/%d): %s", [source12 == null ? "" : source
12.shortName, error.errorCode, location.lineNumber, location.columnNumber, error
.length, error.message]); | |
| 302 } | |
| 303 } | |
| 304 JUnitTestCase.fail(writer.toString()); | |
| 305 } | |
| 306 /** | |
| 307 * Search through the given list of errors for an error that is equal to the t
arget error. If one | |
| 308 * is found, remove it from the list and return {@code true}, otherwise return
{@code false}without modifying the list. | |
| 309 * @param errors the errors through which we are searching | |
| 310 * @param targetError the error being searched for | |
| 311 * @return {@code true} if the error is found and removed from the list | |
| 312 */ | |
| 313 bool foundAndRemoved(List<AnalysisError> errors, AnalysisError targetError) { | |
| 314 for (AnalysisError error in errors) { | |
| 315 if (equals(error, targetError)) { | |
| 316 errors.remove(error); | |
| 317 return true; | |
| 318 } | |
| 319 } | |
| 320 return true; | |
| 321 } | |
| 322 } | |
| 323 /** | |
| 324 * The class {@code EngineTestCase} defines utility methods for making assertion
s. | |
| 325 */ | |
| 326 class EngineTestCase extends JUnitTestCase { | |
| 327 static int _PRINT_RANGE = 6; | |
| 328 /** | |
| 329 * Assert that the tokens in the actual stream of tokens have the same types a
nd lexemes as the | |
| 330 * tokens in the expected stream of tokens. Note that this does not assert any
thing about the | |
| 331 * offsets of the tokens (although the lengths will be equal). | |
| 332 * @param expectedStream the head of the stream of tokens that were expected | |
| 333 * @param actualStream the head of the stream of tokens that were actually fou
nd | |
| 334 * @throws AssertionFailedError if the two streams of tokens are not the same | |
| 335 */ | |
| 336 static void assertAllMatch(Token expectedStream, Token actualStream) { | |
| 337 Token left = expectedStream; | |
| 338 Token right = actualStream; | |
| 339 while (left.type != TokenType.EOF && right.type != TokenType.EOF) { | |
| 340 assertMatches(left, right); | |
| 341 left = left.next; | |
| 342 right = right.next; | |
| 343 } | |
| 344 } | |
| 345 /** | |
| 346 * Assert that the array of actual values contain exactly the same values as t
hose in the array of | |
| 347 * expected value, with the exception that the order of the elements is not re
quired to be the | |
| 348 * same. | |
| 349 * @param expectedValues the values that are expected to be found | |
| 350 * @param actualValues the actual values that are being compared against the e
xpected values | |
| 351 */ | |
| 352 static void assertEqualsIgnoreOrder(List<Object> expectedValues, List<Object>
actualValues) { | |
| 353 JUnitTestCase.assertNotNull(actualValues); | |
| 354 int expectedLength = expectedValues.length; | |
| 355 JUnitTestCase.assertEquals(expectedLength, actualValues.length); | |
| 356 List<bool> found = new List<bool>.filled(expectedLength, false); | |
| 357 for (int i = 0; i < expectedLength; i++) { | |
| 358 found[i] = false; | |
| 359 } | |
| 360 for (Object actualValue in actualValues) { | |
| 361 bool wasExpected = false; | |
| 362 for (int i = 0; i < expectedLength; i++) { | |
| 363 if (!found[i] && expectedValues[i] == actualValue) { | |
| 364 found[i] = true; | |
| 365 wasExpected = true; | |
| 366 break; | |
| 367 } | |
| 368 } | |
| 369 if (!wasExpected) { | |
| 370 JUnitTestCase.fail("The actual value ${actualValue} was not expected"); | |
| 371 } | |
| 372 } | |
| 373 } | |
| 374 /** | |
| 375 * Assert that a given String is equal to an expected value. | |
| 376 * @param expected the expected String value | |
| 377 * @param actual the actual String value | |
| 378 */ | |
| 379 static void assertEqualString(String expected, String actual) { | |
| 380 if (actual == null || expected == null) { | |
| 381 if (identical(actual, expected)) { | |
| 382 return; | |
| 383 } | |
| 384 if (actual == null) { | |
| 385 JUnitTestCase.assertTrueMsg("Content not as expected: is 'null' expected
: ${expected}", false); | |
| 386 } else { | |
| 387 JUnitTestCase.assertTrueMsg("Content not as expected: expected 'null' is
: ${actual}", false); | |
| 388 } | |
| 389 } | |
| 390 int diffPos = getDiffPos(expected, actual); | |
| 391 if (diffPos != -1) { | |
| 392 int diffAhead = Math.max(0, diffPos - _PRINT_RANGE); | |
| 393 int diffAfter = Math.min(actual.length, diffPos + _PRINT_RANGE); | |
| 394 String diffStr = "${actual.substring(diffAhead, diffPos)}^${actual.substri
ng(diffPos, diffAfter)}"; | |
| 395 String message = "Content not as expected: is\n${actual}\nDiffers at pos $
{diffPos}: ${diffStr}\nexpected:\n${expected}"; | |
| 396 JUnitTestCase.assertEqualsMsg(message, expected, actual); | |
| 397 } | |
| 398 } | |
| 399 /** | |
| 400 * Assert that the given list is non-{@code null} and has exactly expected ele
ments. | |
| 401 * @param list the list being tested | |
| 402 * @param expectedElements the expected elements | |
| 403 * @throws AssertionFailedError if the list is {@code null} or does not have t
he expected elements | |
| 404 */ | |
| 405 static void assertExactElements(List<Object> list, List<Object> expectedElemen
ts) { | |
| 406 int expectedSize = expectedElements.length; | |
| 407 if (list == null) { | |
| 408 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); | |
| 409 } | |
| 410 if (list.length != expectedSize) { | |
| 411 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis
t.length} elements"); | |
| 412 } | |
| 413 for (int i = 0; i < expectedElements.length; i++) { | |
| 414 Object element = list[i]; | |
| 415 Object expectedElement = expectedElements[i]; | |
| 416 if (!element == expectedElement) { | |
| 417 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme
nt}"); | |
| 418 } | |
| 419 } | |
| 420 } | |
| 421 /** | |
| 422 * Assert that the given array is non-{@code null} and has exactly expected el
ements. | |
| 423 * @param array the array being tested | |
| 424 * @param expectedElements the expected elements | |
| 425 * @throws AssertionFailedError if the array is {@code null} or does not have
the expected | |
| 426 * elements | |
| 427 */ | |
| 428 static void assertExactElements2(List<Object> array, List<Object> expectedElem
ents) { | |
| 429 int expectedSize = expectedElements.length; | |
| 430 if (array == null) { | |
| 431 JUnitTestCase.fail("Expected array of size ${expectedSize}; found null"); | |
| 432 } | |
| 433 if (array.length != expectedSize) { | |
| 434 JUnitTestCase.fail("Expected array of size ${expectedSize}; contained ${ar
ray.length} elements"); | |
| 435 } | |
| 436 for (int i = 0; i < expectedElements.length; i++) { | |
| 437 Object element = array[0]; | |
| 438 Object expectedElement = expectedElements[i]; | |
| 439 if (!element == expectedElement) { | |
| 440 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme
nt}"); | |
| 441 } | |
| 442 } | |
| 443 } | |
| 444 /** | |
| 445 * Assert that the given list is non-{@code null} and has exactly expected ele
ments. | |
| 446 * @param set the list being tested | |
| 447 * @param expectedElements the expected elements | |
| 448 * @throws AssertionFailedError if the list is {@code null} or does not have t
he expected elements | |
| 449 */ | |
| 450 static void assertExactElements3(Set<Object> set, List<Object> expectedElement
s) { | |
| 451 int expectedSize = expectedElements.length; | |
| 452 if (set == null) { | |
| 453 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); | |
| 454 } | |
| 455 if (set.length != expectedSize) { | |
| 456 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${set
.length} elements"); | |
| 457 } | |
| 458 for (int i = 0; i < expectedElements.length; i++) { | |
| 459 Object expectedElement = expectedElements[i]; | |
| 460 if (!set.contains(expectedElement)) { | |
| 461 JUnitTestCase.fail("Expected ${expectedElement} in set${set}"); | |
| 462 } | |
| 463 } | |
| 464 } | |
| 465 /** | |
| 466 * Assert that the given object is an instance of the expected class. | |
| 467 * @param expectedClass the class that the object is expected to be an instanc
e of | |
| 468 * @param object the object being tested | |
| 469 * @return the object that was being tested | |
| 470 * @throws Exception if the object is not an instance of the expected class | |
| 471 */ | |
| 472 static Object assertInstanceOf(Type expectedClass, Object object) { | |
| 473 if (!isInstanceOf(object, expectedClass)) { | |
| 474 JUnitTestCase.fail("Expected instance of ${expectedClass.toString()}, foun
d ${(object == null ? "null" : object.runtimeType.toString())}"); | |
| 475 } | |
| 476 return object as Object; | |
| 477 } | |
| 478 /** | |
| 479 * Assert that the given array is non-{@code null} and has the expected number
of elements. | |
| 480 * @param expectedLength the expected number of elements | |
| 481 * @param array the array being tested | |
| 482 * @throws AssertionFailedError if the array is {@code null} or does not have
the expected number | |
| 483 * of elements | |
| 484 */ | |
| 485 static void assertLength(int expectedLength, List<Object> array) { | |
| 486 if (array == null) { | |
| 487 JUnitTestCase.fail("Expected array of length ${expectedLength}; found null
"); | |
| 488 } else if (array.length != expectedLength) { | |
| 489 JUnitTestCase.fail("Expected array of length ${expectedLength}; contained
${array.length} elements"); | |
| 490 } | |
| 491 } | |
| 492 /** | |
| 493 * Assert that the actual token has the same type and lexeme as the expected t
oken. Note that this | |
| 494 * does not assert anything about the offsets of the tokens (although the leng
ths will be equal). | |
| 495 * @param expectedToken the token that was expected | |
| 496 * @param actualToken the token that was found | |
| 497 * @throws AssertionFailedError if the two tokens are not the same | |
| 498 */ | |
| 499 static void assertMatches(Token expectedToken, Token actualToken) { | |
| 500 JUnitTestCase.assertEquals(expectedToken.type, actualToken.type); | |
| 501 if (expectedToken is KeywordToken) { | |
| 502 assertInstanceOf(KeywordToken, actualToken); | |
| 503 JUnitTestCase.assertEquals(((expectedToken as KeywordToken)).keyword, ((ac
tualToken as KeywordToken)).keyword); | |
| 504 } else if (expectedToken is StringToken) { | |
| 505 assertInstanceOf(StringToken, actualToken); | |
| 506 JUnitTestCase.assertEquals(((expectedToken as StringToken)).lexeme, ((actu
alToken as StringToken)).lexeme); | |
| 507 } | |
| 508 } | |
| 509 /** | |
| 510 * Assert that the given list is non-{@code null} and has the expected number
of elements. | |
| 511 * @param expectedSize the expected number of elements | |
| 512 * @param list the list being tested | |
| 513 * @throws AssertionFailedError if the list is {@code null} or does not have t
he expected number | |
| 514 * of elements | |
| 515 */ | |
| 516 static void assertSize(int expectedSize, List<Object> list) { | |
| 517 if (list == null) { | |
| 518 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); | |
| 519 } else if (list.length != expectedSize) { | |
| 520 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis
t.length} elements"); | |
| 521 } | |
| 522 } | |
| 523 /** | |
| 524 * Assert that the given map is non-{@code null} and has the expected number o
f elements. | |
| 525 * @param expectedSize the expected number of elements | |
| 526 * @param map the map being tested | |
| 527 * @throws AssertionFailedError if the map is {@code null} or does not have th
e expected number of | |
| 528 * elements | |
| 529 */ | |
| 530 static void assertSize2(int expectedSize, Map<Object, Object> map) { | |
| 531 if (map == null) { | |
| 532 JUnitTestCase.fail("Expected map of size ${expectedSize}; found null"); | |
| 533 } else if (map.length != expectedSize) { | |
| 534 JUnitTestCase.fail("Expected map of size ${expectedSize}; contained ${map.
length} elements"); | |
| 535 } | |
| 536 } | |
| 537 /** | |
| 538 * Convert the given array of lines into a single source string. | |
| 539 * @param lines the lines to be merged into a single source string | |
| 540 * @return the source string composed of the given lines | |
| 541 */ | |
| 542 static String createSource(List<String> lines) { | |
| 543 PrintStringWriter writer = new PrintStringWriter(); | |
| 544 for (String line in lines) { | |
| 545 writer.printlnObject(line); | |
| 546 } | |
| 547 return writer.toString(); | |
| 548 } | |
| 549 /** | |
| 550 * Calculate the offset where the given strings differ. | |
| 551 * @param str1 the first String to compare | |
| 552 * @param str2 the second String to compare | |
| 553 * @return the offset at which the strings differ (or <code>-1</code> if they
do not) | |
| 554 */ | |
| 555 static int getDiffPos(String str1, String str2) { | |
| 556 int len1 = Math.min(str1.length, str2.length); | |
| 557 int diffPos = -1; | |
| 558 for (int i = 0; i < len1; i++) { | |
| 559 if (str1.codeUnitAt(i) != str2.codeUnitAt(i)) { | |
| 560 diffPos = i; | |
| 561 break; | |
| 562 } | |
| 563 } | |
| 564 if (diffPos == -1 && str1.length != str2.length) { | |
| 565 diffPos = len1; | |
| 566 } | |
| 567 return diffPos; | |
| 568 } | |
| 569 /** | |
| 570 * Return the getter in the given type with the given name. Inherited getters
are ignored. | |
| 571 * @param type the type in which the getter is declared | |
| 572 * @param getterName the name of the getter to be returned | |
| 573 * @return the property accessor element representing the getter with the give
n name | |
| 574 */ | |
| 575 PropertyAccessorElement getGetter(InterfaceType type, String getterName) { | |
| 576 for (PropertyAccessorElement accessor in type.element.accessors) { | |
| 577 if (accessor.isGetter() && accessor.name == getterName) { | |
| 578 return accessor; | |
| 579 } | |
| 580 } | |
| 581 JUnitTestCase.fail("Could not find getter named ${getterName} in ${type.name
}"); | |
| 582 return null; | |
| 583 } | |
| 584 /** | |
| 585 * Return the method in the given type with the given name. Inherited methods
are ignored. | |
| 586 * @param type the type in which the method is declared | |
| 587 * @param methodName the name of the method to be returned | |
| 588 * @return the method element representing the method with the given name | |
| 589 */ | |
| 590 MethodElement getMethod(InterfaceType type, String methodName) { | |
| 591 for (MethodElement method in type.element.methods) { | |
| 592 if (method.name == methodName) { | |
| 593 return method; | |
| 594 } | |
| 595 } | |
| 596 JUnitTestCase.fail("Could not find method named ${methodName} in ${type.name
}"); | |
| 597 return null; | |
| 598 } | |
| 599 static dartSuite() { | |
| 600 _ut.group('EngineTestCase', () { | |
| 601 }); | |
| 602 } | |
| 603 } | |
| 604 main() { | |
| 605 } | |
| 606 | |
| 607 class TestSource implements Source { | |
| 608 bool operator ==(Object object) { | |
| 609 return this == object; | |
| 610 } | |
| 611 void getContents(Source_ContentReceiver receiver) { | |
| 612 throw new UnsupportedOperationException(); | |
| 613 } | |
| 614 String get fullName { | |
| 615 throw new UnsupportedOperationException(); | |
| 616 } | |
| 617 String get shortName { | |
| 618 throw new UnsupportedOperationException(); | |
| 619 } | |
| 620 String get encoding { | |
| 621 throw new UnsupportedOperationException(); | |
| 622 } | |
| 623 int get modificationStamp { | |
| 624 throw new UnsupportedOperationException(); | |
| 625 } | |
| 626 bool exists() => true; | |
| 627 bool isInSystemLibrary() { | |
| 628 throw new UnsupportedOperationException(); | |
| 629 } | |
| 630 Source resolve(String uri) { | |
| 631 throw new UnsupportedOperationException(); | |
| 632 } | |
| 633 Source resolveRelative(Uri uri) { | |
| 634 throw new UnsupportedOperationException(); | |
| 635 } | |
| 636 } | |
| 637 | |
| 638 /** | |
| 639 * Wrapper around [Function] which should be called with [target] and [arguments
]. | |
| 640 */ | |
| 641 class MethodTrampoline { | |
| 642 int parameterCount; | |
| 643 Function trampoline; | |
| 644 MethodTrampoline(this.parameterCount, this.trampoline); | |
| 645 Object invoke(target, List arguments) { | |
| 646 if (arguments.length != parameterCount) { | |
| 647 throw new IllegalArgumentException("${arguments.length} != $parameterCount
"); | |
| 648 } | |
| 649 switch (parameterCount) { | |
| 650 case 0: | |
| 651 return trampoline(target); | |
| 652 case 1: | |
| 653 return trampoline(target, arguments[0]); | |
| 654 case 2: | |
| 655 return trampoline(target, arguments[0], arguments[1]); | |
| 656 case 3: | |
| 657 return trampoline(target, arguments[0], arguments[1], arguments[2]); | |
| 658 case 4: | |
| 659 return trampoline(target, arguments[0], arguments[1], arguments[2], argu
ments[3]); | |
| 660 default: | |
| 661 throw new IllegalArgumentException("Not implemented for > 4 arguments"); | |
| 662 } | |
| 663 } | |
| 664 } | |
| OLD | NEW |