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