| 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 'package:analyzer/src/generated/java_core.dart'; | 11 |
| 12 import 'package:analyzer/src/generated/java_junit.dart'; | |
| 13 import 'package:analyzer/src/generated/java_engine.dart'; | |
| 14 import 'package:analyzer/src/generated/source.dart'; | |
| 15 import 'package:analyzer/src/generated/error.dart'; | |
| 16 import 'package:analyzer/src/generated/scanner.dart'; | |
| 17 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator; | 12 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator; |
| 18 import 'package:analyzer/src/generated/element.dart'; | 13 import 'package:analyzer/src/generated/element.dart'; |
| 19 import 'package:analyzer/src/generated/engine.dart'; | 14 import 'package:analyzer/src/generated/engine.dart'; |
| 15 import 'package:analyzer/src/generated/error.dart'; |
| 16 import 'package:analyzer/src/generated/java_core.dart'; |
| 17 import 'package:analyzer/src/generated/java_engine.dart'; |
| 18 import 'package:analyzer/src/generated/java_junit.dart'; |
| 19 import 'package:analyzer/src/generated/scanner.dart'; |
| 20 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:unittest/unittest.dart' as _ut; | 21 import 'package:unittest/unittest.dart' as _ut; |
| 21 | 22 |
| 23 |
| 22 /** | 24 /** |
| 23 * The class `EngineTestCase` defines utility methods for making assertions. | 25 * The class `EngineTestCase` defines utility methods for making assertions. |
| 24 */ | 26 */ |
| 25 class EngineTestCase extends JUnitTestCase { | 27 class EngineTestCase extends JUnitTestCase { |
| 26 static int _PRINT_RANGE = 6; | 28 static int _PRINT_RANGE = 6; |
| 27 | 29 |
| 28 /** | 30 /** |
| 31 * Assert that the given collection has the same number of elements as the num
ber of specified |
| 32 * names, and that for each specified name, a corresponding element can be fou
nd in the given |
| 33 * collection with that name. |
| 34 * |
| 35 * @param elements the elements |
| 36 * @param names the names |
| 37 */ |
| 38 void assertNamedElements(List<Element> elements, List<String> names) { |
| 39 for (String elemName in names) { |
| 40 bool found = false; |
| 41 for (Element elem in elements) { |
| 42 if (elem.name == elemName) { |
| 43 found = true; |
| 44 break; |
| 45 } |
| 46 } |
| 47 if (!found) { |
| 48 JavaStringBuilder msg = new JavaStringBuilder(); |
| 49 msg.append("Expected element named: "); |
| 50 msg.append(elemName); |
| 51 msg.append("\n but found: "); |
| 52 for (Element elem in elements) { |
| 53 msg.append(elem.name); |
| 54 msg.append(", "); |
| 55 } |
| 56 JUnitTestCase.fail(msg.toString()); |
| 57 } |
| 58 } |
| 59 assertLength(names.length, elements); |
| 60 } |
| 61 |
| 62 AnalysisContextImpl createAnalysisContext() { |
| 63 AnalysisContextImpl context = new AnalysisContextImpl(); |
| 64 context.sourceFactory = new SourceFactory([]); |
| 65 return context; |
| 66 } |
| 67 |
| 68 /** |
| 69 * Return the getter in the given type with the given name. Inherited getters
are ignored. |
| 70 * |
| 71 * @param type the type in which the getter is declared |
| 72 * @param getterName the name of the getter to be returned |
| 73 * @return the property accessor element representing the getter with the give
n name |
| 74 */ |
| 75 PropertyAccessorElement getGetter(InterfaceType type, String getterName) { |
| 76 for (PropertyAccessorElement accessor in type.element.accessors) { |
| 77 if (accessor.isGetter && accessor.name == getterName) { |
| 78 return accessor; |
| 79 } |
| 80 } |
| 81 JUnitTestCase.fail( |
| 82 "Could not find getter named ${getterName} in ${type.displayName}"); |
| 83 return null; |
| 84 } |
| 85 |
| 86 /** |
| 87 * Return the method in the given type with the given name. Inherited methods
are ignored. |
| 88 * |
| 89 * @param type the type in which the method is declared |
| 90 * @param methodName the name of the method to be returned |
| 91 * @return the method element representing the method with the given name |
| 92 */ |
| 93 MethodElement getMethod(InterfaceType type, String methodName) { |
| 94 for (MethodElement method in type.element.methods) { |
| 95 if (method.name == methodName) { |
| 96 return method; |
| 97 } |
| 98 } |
| 99 JUnitTestCase.fail( |
| 100 "Could not find method named ${methodName} in ${type.displayName}"); |
| 101 return null; |
| 102 } |
| 103 |
| 104 /** |
| 29 * Assert that the tokens in the actual stream of tokens have the same types a
nd lexemes as the | 105 * Assert that the tokens in the actual stream of tokens have the same types a
nd lexemes as the |
| 30 * tokens in the expected stream of tokens. Note that this does not assert any
thing about the | 106 * tokens in the expected stream of tokens. Note that this does not assert any
thing about the |
| 31 * offsets of the tokens (although the lengths will be equal). | 107 * offsets of the tokens (although the lengths will be equal). |
| 32 * | 108 * |
| 33 * @param expectedStream the head of the stream of tokens that were expected | 109 * @param expectedStream the head of the stream of tokens that were expected |
| 34 * @param actualStream the head of the stream of tokens that were actually fou
nd | 110 * @param actualStream the head of the stream of tokens that were actually fou
nd |
| 35 * @throws AssertionFailedError if the two streams of tokens are not the same | 111 * @throws AssertionFailedError if the two streams of tokens are not the same |
| 36 */ | 112 */ |
| 37 static void assertAllMatch(Token expectedStream, Token actualStream) { | 113 static void assertAllMatch(Token expectedStream, Token actualStream) { |
| 38 Token left = expectedStream; | 114 Token left = expectedStream; |
| 39 Token right = actualStream; | 115 Token right = actualStream; |
| 40 while (left.type != TokenType.EOF && right.type != TokenType.EOF) { | 116 while (left.type != TokenType.EOF && right.type != TokenType.EOF) { |
| 41 assertMatches(left, right); | 117 assertMatches(left, right); |
| 42 left = left.next; | 118 left = left.next; |
| 43 right = right.next; | 119 right = right.next; |
| 44 } | 120 } |
| 45 } | 121 } |
| 46 | 122 |
| 47 /** | 123 /** |
| 48 * Assert that the given collection is non-`null` and has the expected number
of elements. | 124 * Assert that the given collection is non-`null` and has the expected number
of elements. |
| 49 * | 125 * |
| 50 * @param expectedSize the expected number of elements | 126 * @param expectedSize the expected number of elements |
| 51 * @param c the collection being tested | 127 * @param c the collection being tested |
| 52 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected number | 128 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected number |
| 53 * of elements | 129 * of elements |
| 54 */ | 130 */ |
| 55 static void assertCollectionSize(int expectedSize, Iterable c) { | 131 static void assertCollectionSize(int expectedSize, Iterable c) { |
| 56 if (c == null) { | 132 if (c == null) { |
| 57 JUnitTestCase.fail("Expected collection of size ${expectedSize}; found nul
l"); | 133 JUnitTestCase.fail( |
| 134 "Expected collection of size ${expectedSize}; found null"); |
| 58 } else if (c.length != expectedSize) { | 135 } else if (c.length != expectedSize) { |
| 59 JUnitTestCase.fail("Expected collection of size ${expectedSize}; contained
${c.length} elements"); | 136 JUnitTestCase.fail( |
| 137 "Expected collection of size ${expectedSize}; contained ${c.length} el
ements"); |
| 60 } | 138 } |
| 61 } | 139 } |
| 62 | 140 |
| 63 /** | 141 /** |
| 64 * Assert that the given array is non-`null` and contains the expected element
s. The | 142 * Assert that the given array is non-`null` and contains the expected element
s. The |
| 65 * elements can appear in any order. | 143 * elements can appear in any order. |
| 66 * | 144 * |
| 67 * @param array the array being tested | 145 * @param array the array being tested |
| 68 * @param expectedElements the expected elements | 146 * @param expectedElements the expected elements |
| 69 * @throws AssertionFailedError if the array is `null` or does not contain the
expected | 147 * @throws AssertionFailedError if the array is `null` or does not contain the
expected |
| 70 * elements | 148 * elements |
| 71 */ | 149 */ |
| 72 static void assertContains(List<Object> array, List<Object> expectedElements)
{ | 150 static void assertContains(List<Object> array, |
| 151 List<Object> expectedElements) { |
| 73 int expectedSize = expectedElements.length; | 152 int expectedSize = expectedElements.length; |
| 74 if (array == null) { | 153 if (array == null) { |
| 75 JUnitTestCase.fail("Expected array of length ${expectedSize}; found null")
; | 154 JUnitTestCase.fail( |
| 155 "Expected array of length ${expectedSize}; found null"); |
| 76 } | 156 } |
| 77 if (array.length != expectedSize) { | 157 if (array.length != expectedSize) { |
| 78 JUnitTestCase.fail("Expected array of length ${expectedSize}; contained ${
array.length} elements"); | 158 JUnitTestCase.fail( |
| 159 "Expected array of length ${expectedSize}; contained ${array.length} e
lements"); |
| 79 } | 160 } |
| 80 List<bool> found = new List<bool>.filled(expectedSize, false); | 161 List<bool> found = new List<bool>.filled(expectedSize, false); |
| 81 for (int i = 0; i < expectedSize; i++) { | 162 for (int i = 0; i < expectedSize; i++) { |
| 82 _privateAssertContains(array, found, expectedElements[i]); | 163 _privateAssertContains(array, found, expectedElements[i]); |
| 83 } | 164 } |
| 84 } | 165 } |
| 85 | 166 |
| 86 /** | 167 /** |
| 168 * Assert that a given String is equal to an expected value. |
| 169 * |
| 170 * @param expected the expected String value |
| 171 * @param actual the actual String value |
| 172 */ |
| 173 static void assertEqualString(String expected, String actual) { |
| 174 if (actual == null || expected == null) { |
| 175 if (identical(actual, expected)) { |
| 176 return; |
| 177 } |
| 178 if (actual == null) { |
| 179 JUnitTestCase.assertTrueMsg( |
| 180 "Content not as expected: is 'null' expected: ${expected}", |
| 181 false); |
| 182 } else { |
| 183 JUnitTestCase.assertTrueMsg( |
| 184 "Content not as expected: expected 'null' is: ${actual}", |
| 185 false); |
| 186 } |
| 187 } |
| 188 int diffPos = _getDiffPos(expected, actual); |
| 189 if (diffPos != -1) { |
| 190 int diffAhead = Math.max(0, diffPos - _PRINT_RANGE); |
| 191 int diffAfter = Math.min(actual.length, diffPos + _PRINT_RANGE); |
| 192 String diffStr = |
| 193 "${actual.substring(diffAhead, diffPos)}^${actual.substring(diffPos, d
iffAfter)}"; |
| 194 // use detailed message |
| 195 String message = |
| 196 "Content not as expected: is\n${actual}\nDiffers at pos ${diffPos}: ${
diffStr}\nexpected:\n${expected}"; |
| 197 JUnitTestCase.assertEqualsMsg(message, expected, actual); |
| 198 } |
| 199 } |
| 200 |
| 201 /** |
| 87 * Assert that the array of actual values contain exactly the same values as t
hose in the array of | 202 * Assert that the array of actual values contain exactly the same values as t
hose in the array of |
| 88 * expected value, with the exception that the order of the elements is not re
quired to be the | 203 * expected value, with the exception that the order of the elements is not re
quired to be the |
| 89 * same. | 204 * same. |
| 90 * | 205 * |
| 91 * @param expectedValues the values that are expected to be found | 206 * @param expectedValues the values that are expected to be found |
| 92 * @param actualValues the actual values that are being compared against the e
xpected values | 207 * @param actualValues the actual values that are being compared against the e
xpected values |
| 93 */ | 208 */ |
| 94 static void assertEqualsIgnoreOrder(List<Object> expectedValues, List<Object>
actualValues) { | 209 static void assertEqualsIgnoreOrder(List<Object> expectedValues, |
| 210 List<Object> actualValues) { |
| 95 JUnitTestCase.assertNotNull(actualValues); | 211 JUnitTestCase.assertNotNull(actualValues); |
| 96 int expectedLength = expectedValues.length; | 212 int expectedLength = expectedValues.length; |
| 97 JUnitTestCase.assertEquals(expectedLength, actualValues.length); | 213 JUnitTestCase.assertEquals(expectedLength, actualValues.length); |
| 98 List<bool> found = new List<bool>.filled(expectedLength, false); | 214 List<bool> found = new List<bool>.filled(expectedLength, false); |
| 99 for (int i = 0; i < expectedLength; i++) { | 215 for (int i = 0; i < expectedLength; i++) { |
| 100 found[i] = false; | 216 found[i] = false; |
| 101 } | 217 } |
| 102 for (Object actualValue in actualValues) { | 218 for (Object actualValue in actualValues) { |
| 103 bool wasExpected = false; | 219 bool wasExpected = false; |
| 104 for (int i = 0; i < expectedLength; i++) { | 220 for (int i = 0; i < expectedLength; i++) { |
| 105 if (!found[i] && expectedValues[i] == actualValue) { | 221 if (!found[i] && expectedValues[i] == actualValue) { |
| 106 found[i] = true; | 222 found[i] = true; |
| 107 wasExpected = true; | 223 wasExpected = true; |
| 108 break; | 224 break; |
| 109 } | 225 } |
| 110 } | 226 } |
| 111 if (!wasExpected) { | 227 if (!wasExpected) { |
| 112 JUnitTestCase.fail("The actual value ${actualValue} was not expected"); | 228 JUnitTestCase.fail("The actual value ${actualValue} was not expected"); |
| 113 } | 229 } |
| 114 } | 230 } |
| 115 } | 231 } |
| 116 | 232 |
| 117 /** | 233 /** |
| 118 * Assert that a given String is equal to an expected value. | |
| 119 * | |
| 120 * @param expected the expected String value | |
| 121 * @param actual the actual String value | |
| 122 */ | |
| 123 static void assertEqualString(String expected, String actual) { | |
| 124 if (actual == null || expected == null) { | |
| 125 if (identical(actual, expected)) { | |
| 126 return; | |
| 127 } | |
| 128 if (actual == null) { | |
| 129 JUnitTestCase.assertTrueMsg("Content not as expected: is 'null' expected
: ${expected}", false); | |
| 130 } else { | |
| 131 JUnitTestCase.assertTrueMsg("Content not as expected: expected 'null' is
: ${actual}", false); | |
| 132 } | |
| 133 } | |
| 134 int diffPos = _getDiffPos(expected, actual); | |
| 135 if (diffPos != -1) { | |
| 136 int diffAhead = Math.max(0, diffPos - _PRINT_RANGE); | |
| 137 int diffAfter = Math.min(actual.length, diffPos + _PRINT_RANGE); | |
| 138 String diffStr = "${actual.substring(diffAhead, diffPos)}^${actual.substri
ng(diffPos, diffAfter)}"; | |
| 139 // use detailed message | |
| 140 String message = "Content not as expected: is\n${actual}\nDiffers at pos $
{diffPos}: ${diffStr}\nexpected:\n${expected}"; | |
| 141 JUnitTestCase.assertEqualsMsg(message, expected, actual); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * Assert that the given array is non-`null` and has exactly expected elements
. | 234 * Assert that the given array is non-`null` and has exactly expected elements
. |
| 147 * | 235 * |
| 148 * @param array the array being tested | 236 * @param array the array being tested |
| 149 * @param expectedElements the expected elements | 237 * @param expectedElements the expected elements |
| 150 * @throws AssertionFailedError if the array is `null` or does not have the ex
pected | 238 * @throws AssertionFailedError if the array is `null` or does not have the ex
pected |
| 151 * elements | 239 * elements |
| 152 */ | 240 */ |
| 153 static void assertExactElementsInArray(List<Object> array, List<Object> expect
edElements) { | 241 static void assertExactElementsInArray(List<Object> array, |
| 242 List<Object> expectedElements) { |
| 154 int expectedSize = expectedElements.length; | 243 int expectedSize = expectedElements.length; |
| 155 if (array == null) { | 244 if (array == null) { |
| 156 JUnitTestCase.fail("Expected array of size ${expectedSize}; found null"); | 245 JUnitTestCase.fail("Expected array of size ${expectedSize}; found null"); |
| 157 } | 246 } |
| 158 if (array.length != expectedSize) { | 247 if (array.length != expectedSize) { |
| 159 JUnitTestCase.fail("Expected array of size ${expectedSize}; contained ${ar
ray.length} elements"); | 248 JUnitTestCase.fail( |
| 249 "Expected array of size ${expectedSize}; contained ${array.length} ele
ments"); |
| 160 } | 250 } |
| 161 for (int i = 0; i < expectedSize; i++) { | 251 for (int i = 0; i < expectedSize; i++) { |
| 162 Object element = array[i]; | 252 Object element = array[i]; |
| 163 Object expectedElement = expectedElements[i]; | 253 Object expectedElement = expectedElements[i]; |
| 164 if (element != expectedElement) { | 254 if (element != expectedElement) { |
| 165 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme
nt}"); | 255 JUnitTestCase.fail( |
| 256 "Expected ${expectedElement} at [${i}]; found ${element}"); |
| 166 } | 257 } |
| 167 } | 258 } |
| 168 } | 259 } |
| 169 | 260 |
| 170 /** | 261 /** |
| 171 * Assert that the given list is non-`null` and has exactly expected elements. | 262 * Assert that the given list is non-`null` and has exactly expected elements. |
| 172 * | 263 * |
| 173 * @param list the list being tested | 264 * @param list the list being tested |
| 174 * @param expectedElements the expected elements | 265 * @param expectedElements the expected elements |
| 175 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected elements | 266 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected elements |
| 176 */ | 267 */ |
| 177 static void assertExactElementsInList(List list, List<Object> expectedElements
) { | 268 static void assertExactElementsInList(List list, |
| 269 List<Object> expectedElements) { |
| 178 int expectedSize = expectedElements.length; | 270 int expectedSize = expectedElements.length; |
| 179 if (list == null) { | 271 if (list == null) { |
| 180 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); | 272 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); |
| 181 } | 273 } |
| 182 if (list.length != expectedSize) { | 274 if (list.length != expectedSize) { |
| 183 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis
t.length} elements"); | 275 JUnitTestCase.fail( |
| 276 "Expected list of size ${expectedSize}; contained ${list.length} eleme
nts"); |
| 184 } | 277 } |
| 185 for (int i = 0; i < expectedSize; i++) { | 278 for (int i = 0; i < expectedSize; i++) { |
| 186 Object element = list[i]; | 279 Object element = list[i]; |
| 187 Object expectedElement = expectedElements[i]; | 280 Object expectedElement = expectedElements[i]; |
| 188 if (element != expectedElement) { | 281 if (element != expectedElement) { |
| 189 JUnitTestCase.fail("Expected ${expectedElement} at [${i}]; found ${eleme
nt}"); | 282 JUnitTestCase.fail( |
| 283 "Expected ${expectedElement} at [${i}]; found ${element}"); |
| 190 } | 284 } |
| 191 } | 285 } |
| 192 } | 286 } |
| 193 | 287 |
| 194 /** | 288 /** |
| 195 * Assert that the given list is non-`null` and has exactly expected elements. | 289 * Assert that the given list is non-`null` and has exactly expected elements. |
| 196 * | 290 * |
| 197 * @param set the list being tested | 291 * @param set the list being tested |
| 198 * @param expectedElements the expected elements | 292 * @param expectedElements the expected elements |
| 199 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected elements | 293 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected elements |
| 200 */ | 294 */ |
| 201 static void assertExactElementsInSet(Set set, List<Object> expectedElements) { | 295 static void assertExactElementsInSet(Set set, List<Object> expectedElements) { |
| 202 int expectedSize = expectedElements.length; | 296 int expectedSize = expectedElements.length; |
| 203 if (set == null) { | 297 if (set == null) { |
| 204 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); | 298 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); |
| 205 } | 299 } |
| 206 if (set.length != expectedSize) { | 300 if (set.length != expectedSize) { |
| 207 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${set
.length} elements"); | 301 JUnitTestCase.fail( |
| 302 "Expected list of size ${expectedSize}; contained ${set.length} elemen
ts"); |
| 208 } | 303 } |
| 209 for (int i = 0; i < expectedSize; i++) { | 304 for (int i = 0; i < expectedSize; i++) { |
| 210 Object expectedElement = expectedElements[i]; | 305 Object expectedElement = expectedElements[i]; |
| 211 if (!set.contains(expectedElement)) { | 306 if (!set.contains(expectedElement)) { |
| 212 JUnitTestCase.fail("Expected ${expectedElement} in set${set}"); | 307 JUnitTestCase.fail("Expected ${expectedElement} in set${set}"); |
| 213 } | 308 } |
| 214 } | 309 } |
| 215 } | 310 } |
| 216 | 311 |
| 217 /** | 312 /** |
| 218 * Assert that the given object is an instance of the expected class. | 313 * Assert that the given object is an instance of the expected class. |
| 219 * | 314 * |
| 220 * @param expectedClass the class that the object is expected to be an instanc
e of | 315 * @param expectedClass the class that the object is expected to be an instanc
e of |
| 221 * @param object the object being tested | 316 * @param object the object being tested |
| 222 * @return the object that was being tested | 317 * @return the object that was being tested |
| 223 * @throws Exception if the object is not an instance of the expected class | 318 * @throws Exception if the object is not an instance of the expected class |
| 224 */ | 319 */ |
| 225 static Object assertInstanceOf(Predicate<Object> predicate, Type expectedClass
, Object object) { | 320 static Object assertInstanceOf(Predicate<Object> predicate, |
| 321 Type expectedClass, Object object) { |
| 226 if (!predicate(object)) { | 322 if (!predicate(object)) { |
| 227 JUnitTestCase.fail("Expected instance of ${expectedClass.toString()}, foun
d ${(object == null ? "null" : object.runtimeType.toString())}"); | 323 JUnitTestCase.fail( |
| 324 "Expected instance of ${expectedClass.toString()}, found ${(object ==
null ? "null" : object.runtimeType.toString())}"); |
| 228 } | 325 } |
| 229 return object; | 326 return object; |
| 230 } | 327 } |
| 231 | 328 |
| 232 /** | 329 /** |
| 233 * Assert that the given array is non-`null` and has the expected number of el
ements. | 330 * Assert that the given array is non-`null` and has the expected number of el
ements. |
| 234 * | 331 * |
| 235 * @param expectedLength the expected number of elements | 332 * @param expectedLength the expected number of elements |
| 236 * @param array the array being tested | 333 * @param array the array being tested |
| 237 * @throws AssertionFailedError if the array is `null` or does not have the ex
pected number | 334 * @throws AssertionFailedError if the array is `null` or does not have the ex
pected number |
| 238 * of elements | 335 * of elements |
| 239 */ | 336 */ |
| 240 static void assertLength(int expectedLength, List<Object> array) { | 337 static void assertLength(int expectedLength, List<Object> array) { |
| 241 if (array == null) { | 338 if (array == null) { |
| 242 JUnitTestCase.fail("Expected array of length ${expectedLength}; found null
"); | 339 JUnitTestCase.fail( |
| 340 "Expected array of length ${expectedLength}; found null"); |
| 243 } else if (array.length != expectedLength) { | 341 } else if (array.length != expectedLength) { |
| 244 JUnitTestCase.fail("Expected array of length ${expectedLength}; contained
${array.length} elements"); | 342 JUnitTestCase.fail( |
| 343 "Expected array of length ${expectedLength}; contained ${array.length}
elements"); |
| 245 } | 344 } |
| 246 } | 345 } |
| 247 | 346 |
| 248 /** | 347 /** |
| 249 * Assert that the actual token has the same type and lexeme as the expected t
oken. Note that this | 348 * Assert that the actual token has the same type and lexeme as the expected t
oken. Note that this |
| 250 * does not assert anything about the offsets of the tokens (although the leng
ths will be equal). | 349 * does not assert anything about the offsets of the tokens (although the leng
ths will be equal). |
| 251 * | 350 * |
| 252 * @param expectedToken the token that was expected | 351 * @param expectedToken the token that was expected |
| 253 * @param actualToken the token that was found | 352 * @param actualToken the token that was found |
| 254 * @throws AssertionFailedError if the two tokens are not the same | 353 * @throws AssertionFailedError if the two tokens are not the same |
| 255 */ | 354 */ |
| 256 static void assertMatches(Token expectedToken, Token actualToken) { | 355 static void assertMatches(Token expectedToken, Token actualToken) { |
| 257 JUnitTestCase.assertEquals(expectedToken.type, actualToken.type); | 356 JUnitTestCase.assertEquals(expectedToken.type, actualToken.type); |
| 258 if (expectedToken is KeywordToken) { | 357 if (expectedToken is KeywordToken) { |
| 259 assertInstanceOf((obj) => obj is KeywordToken, KeywordToken, actualToken); | 358 assertInstanceOf((obj) => obj is KeywordToken, KeywordToken, actualToken); |
| 260 JUnitTestCase.assertEquals(expectedToken.keyword, (actualToken as KeywordT
oken).keyword); | 359 JUnitTestCase.assertEquals( |
| 360 expectedToken.keyword, |
| 361 (actualToken as KeywordToken).keyword); |
| 261 } else if (expectedToken is StringToken) { | 362 } else if (expectedToken is StringToken) { |
| 262 assertInstanceOf((obj) => obj is StringToken, StringToken, actualToken); | 363 assertInstanceOf((obj) => obj is StringToken, StringToken, actualToken); |
| 263 JUnitTestCase.assertEquals(expectedToken.lexeme, (actualToken as StringTok
en).lexeme); | 364 JUnitTestCase.assertEquals( |
| 365 expectedToken.lexeme, |
| 366 (actualToken as StringToken).lexeme); |
| 264 } | 367 } |
| 265 } | 368 } |
| 266 | 369 |
| 267 /** | 370 /** |
| 268 * Assert that the given list is non-`null` and has the expected number of ele
ments. | 371 * Assert that the given list is non-`null` and has the expected number of ele
ments. |
| 269 * | 372 * |
| 270 * @param expectedSize the expected number of elements | 373 * @param expectedSize the expected number of elements |
| 271 * @param list the list being tested | 374 * @param list the list being tested |
| 272 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected number | 375 * @throws AssertionFailedError if the list is `null` or does not have the exp
ected number |
| 273 * of elements | 376 * of elements |
| 274 */ | 377 */ |
| 275 static void assertSizeOfList(int expectedSize, List list) { | 378 static void assertSizeOfList(int expectedSize, List list) { |
| 276 if (list == null) { | 379 if (list == null) { |
| 277 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); | 380 JUnitTestCase.fail("Expected list of size ${expectedSize}; found null"); |
| 278 } else if (list.length != expectedSize) { | 381 } else if (list.length != expectedSize) { |
| 279 JUnitTestCase.fail("Expected list of size ${expectedSize}; contained ${lis
t.length} elements"); | 382 JUnitTestCase.fail( |
| 383 "Expected list of size ${expectedSize}; contained ${list.length} eleme
nts"); |
| 280 } | 384 } |
| 281 } | 385 } |
| 282 | 386 |
| 283 /** | 387 /** |
| 284 * Assert that the given map is non-`null` and has the expected number of elem
ents. | 388 * Assert that the given map is non-`null` and has the expected number of elem
ents. |
| 285 * | 389 * |
| 286 * @param expectedSize the expected number of elements | 390 * @param expectedSize the expected number of elements |
| 287 * @param map the map being tested | 391 * @param map the map being tested |
| 288 * @throws AssertionFailedError if the map is `null` or does not have the expe
cted number of | 392 * @throws AssertionFailedError if the map is `null` or does not have the expe
cted number of |
| 289 * elements | 393 * elements |
| 290 */ | 394 */ |
| 291 static void assertSizeOfMap(int expectedSize, Map map) { | 395 static void assertSizeOfMap(int expectedSize, Map map) { |
| 292 if (map == null) { | 396 if (map == null) { |
| 293 JUnitTestCase.fail("Expected map of size ${expectedSize}; found null"); | 397 JUnitTestCase.fail("Expected map of size ${expectedSize}; found null"); |
| 294 } else if (map.length != expectedSize) { | 398 } else if (map.length != expectedSize) { |
| 295 JUnitTestCase.fail("Expected map of size ${expectedSize}; contained ${map.
length} elements"); | 399 JUnitTestCase.fail( |
| 400 "Expected map of size ${expectedSize}; contained ${map.length} element
s"); |
| 296 } | 401 } |
| 297 } | 402 } |
| 298 | 403 |
| 299 /** | 404 /** |
| 300 * Assert that the given set is non-`null` and has the expected number of elem
ents. | 405 * Assert that the given set is non-`null` and has the expected number of elem
ents. |
| 301 * | 406 * |
| 302 * @param expectedSize the expected number of elements | 407 * @param expectedSize the expected number of elements |
| 303 * @param set the set being tested | 408 * @param set the set being tested |
| 304 * @throws AssertionFailedError if the set is `null` or does not have the expe
cted number of | 409 * @throws AssertionFailedError if the set is `null` or does not have the expe
cted number of |
| 305 * elements | 410 * elements |
| 306 */ | 411 */ |
| 307 static void assertSizeOfSet(int expectedSize, Set set) { | 412 static void assertSizeOfSet(int expectedSize, Set set) { |
| 308 if (set == null) { | 413 if (set == null) { |
| 309 JUnitTestCase.fail("Expected set of size ${expectedSize}; found null"); | 414 JUnitTestCase.fail("Expected set of size ${expectedSize}; found null"); |
| 310 } else if (set.length != expectedSize) { | 415 } else if (set.length != expectedSize) { |
| 311 JUnitTestCase.fail("Expected set of size ${expectedSize}; contained ${set.
length} elements"); | 416 JUnitTestCase.fail( |
| 417 "Expected set of size ${expectedSize}; contained ${set.length} element
s"); |
| 312 } | 418 } |
| 313 } | 419 } |
| 314 | 420 |
| 315 /** | 421 /** |
| 316 * Convert the given array of lines into a single source string. | 422 * Convert the given array of lines into a single source string. |
| 317 * | 423 * |
| 318 * @param lines the lines to be merged into a single source string | 424 * @param lines the lines to be merged into a single source string |
| 319 * @return the source string composed of the given lines | 425 * @return the source string composed of the given lines |
| 320 */ | 426 */ |
| 321 static String createSource(List<String> lines) { | 427 static String createSource(List<String> lines) { |
| 322 PrintStringWriter writer = new PrintStringWriter(); | 428 PrintStringWriter writer = new PrintStringWriter(); |
| 323 for (String line in lines) { | 429 for (String line in lines) { |
| 324 writer.println(line); | 430 writer.println(line); |
| 325 } | 431 } |
| 326 return writer.toString(); | 432 return writer.toString(); |
| 327 } | 433 } |
| 328 | 434 |
| 435 static dartSuite() { |
| 436 _ut.group('EngineTestCase', () { |
| 437 }); |
| 438 } |
| 439 |
| 329 /** | 440 /** |
| 330 * @return the [AstNode] with requested type at offset of the "prefix". | 441 * @return the [AstNode] with requested type at offset of the "prefix". |
| 331 */ | 442 */ |
| 332 static AstNode findNode(AstNode root, String code, String prefix, Predicate<As
tNode> predicate) { | 443 static AstNode findNode(AstNode root, String code, String prefix, |
| 444 Predicate<AstNode> predicate) { |
| 333 int offset = code.indexOf(prefix); | 445 int offset = code.indexOf(prefix); |
| 334 if (offset == -1) { | 446 if (offset == -1) { |
| 335 throw new IllegalArgumentException("Not found '${prefix}'."); | 447 throw new IllegalArgumentException("Not found '${prefix}'."); |
| 336 } | 448 } |
| 337 AstNode node = new NodeLocator.con1(offset).searchWithin(root); | 449 AstNode node = new NodeLocator.con1(offset).searchWithin(root); |
| 338 return node.getAncestor(predicate); | 450 return node.getAncestor(predicate); |
| 339 } | 451 } |
| 340 | 452 |
| 341 /** | 453 /** |
| 342 * Calculate the offset where the given strings differ. | 454 * Calculate the offset where the given strings differ. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 353 diffPos = i; | 465 diffPos = i; |
| 354 break; | 466 break; |
| 355 } | 467 } |
| 356 } | 468 } |
| 357 if (diffPos == -1 && str1.length != str2.length) { | 469 if (diffPos == -1 && str1.length != str2.length) { |
| 358 diffPos = len1; | 470 diffPos = len1; |
| 359 } | 471 } |
| 360 return diffPos; | 472 return diffPos; |
| 361 } | 473 } |
| 362 | 474 |
| 363 static void _privateAssertContains(List<Object> array, List<bool> found, Objec
t element) { | 475 static void _privateAssertContains(List<Object> array, List<bool> found, |
| 476 Object element) { |
| 364 if (element == null) { | 477 if (element == null) { |
| 365 for (int i = 0; i < array.length; i++) { | 478 for (int i = 0; i < array.length; i++) { |
| 366 if (!found[i]) { | 479 if (!found[i]) { |
| 367 if (array[i] == null) { | 480 if (array[i] == null) { |
| 368 found[i] = true; | 481 found[i] = true; |
| 369 return; | 482 return; |
| 370 } | 483 } |
| 371 } | 484 } |
| 372 } | 485 } |
| 373 JUnitTestCase.fail("Does not contain null"); | 486 JUnitTestCase.fail("Does not contain null"); |
| 374 } else { | 487 } else { |
| 375 for (int i = 0; i < array.length; i++) { | 488 for (int i = 0; i < array.length; i++) { |
| 376 if (!found[i]) { | 489 if (!found[i]) { |
| 377 if (element == array[i]) { | 490 if (element == array[i]) { |
| 378 found[i] = true; | 491 found[i] = true; |
| 379 return; | 492 return; |
| 380 } | 493 } |
| 381 } | 494 } |
| 382 } | 495 } |
| 383 JUnitTestCase.fail("Does not contain ${element}"); | 496 JUnitTestCase.fail("Does not contain ${element}"); |
| 384 } | 497 } |
| 385 } | 498 } |
| 386 | |
| 387 /** | |
| 388 * Assert that the given collection has the same number of elements as the num
ber of specified | |
| 389 * names, and that for each specified name, a corresponding element can be fou
nd in the given | |
| 390 * collection with that name. | |
| 391 * | |
| 392 * @param elements the elements | |
| 393 * @param names the names | |
| 394 */ | |
| 395 void assertNamedElements(List<Element> elements, List<String> names) { | |
| 396 for (String elemName in names) { | |
| 397 bool found = false; | |
| 398 for (Element elem in elements) { | |
| 399 if (elem.name == elemName) { | |
| 400 found = true; | |
| 401 break; | |
| 402 } | |
| 403 } | |
| 404 if (!found) { | |
| 405 JavaStringBuilder msg = new JavaStringBuilder(); | |
| 406 msg.append("Expected element named: "); | |
| 407 msg.append(elemName); | |
| 408 msg.append("\n but found: "); | |
| 409 for (Element elem in elements) { | |
| 410 msg.append(elem.name); | |
| 411 msg.append(", "); | |
| 412 } | |
| 413 JUnitTestCase.fail(msg.toString()); | |
| 414 } | |
| 415 } | |
| 416 assertLength(names.length, elements); | |
| 417 } | |
| 418 | |
| 419 AnalysisContextImpl createAnalysisContext() { | |
| 420 AnalysisContextImpl context = new AnalysisContextImpl(); | |
| 421 context.sourceFactory = new SourceFactory([]); | |
| 422 return context; | |
| 423 } | |
| 424 | |
| 425 /** | |
| 426 * Return the getter in the given type with the given name. Inherited getters
are ignored. | |
| 427 * | |
| 428 * @param type the type in which the getter is declared | |
| 429 * @param getterName the name of the getter to be returned | |
| 430 * @return the property accessor element representing the getter with the give
n name | |
| 431 */ | |
| 432 PropertyAccessorElement getGetter(InterfaceType type, String getterName) { | |
| 433 for (PropertyAccessorElement accessor in type.element.accessors) { | |
| 434 if (accessor.isGetter && accessor.name == getterName) { | |
| 435 return accessor; | |
| 436 } | |
| 437 } | |
| 438 JUnitTestCase.fail("Could not find getter named ${getterName} in ${type.disp
layName}"); | |
| 439 return null; | |
| 440 } | |
| 441 | |
| 442 /** | |
| 443 * Return the method in the given type with the given name. Inherited methods
are ignored. | |
| 444 * | |
| 445 * @param type the type in which the method is declared | |
| 446 * @param methodName the name of the method to be returned | |
| 447 * @return the method element representing the method with the given name | |
| 448 */ | |
| 449 MethodElement getMethod(InterfaceType type, String methodName) { | |
| 450 for (MethodElement method in type.element.methods) { | |
| 451 if (method.name == methodName) { | |
| 452 return method; | |
| 453 } | |
| 454 } | |
| 455 JUnitTestCase.fail("Could not find method named ${methodName} in ${type.disp
layName}"); | |
| 456 return null; | |
| 457 } | |
| 458 | |
| 459 static dartSuite() { | |
| 460 _ut.group('EngineTestCase', () { | |
| 461 }); | |
| 462 } | |
| 463 } | 499 } |
| 464 | 500 |
| 465 /** | 501 /** |
| 466 * Instances of the class `GatheringErrorListener` implement an error listener t
hat collects | 502 * Instances of the class `GatheringErrorListener` implement an error listener t
hat collects |
| 467 * all of the errors passed to it for later examination. | 503 * all of the errors passed to it for later examination. |
| 468 */ | 504 */ |
| 469 class GatheringErrorListener implements AnalysisErrorListener { | 505 class GatheringErrorListener implements AnalysisErrorListener { |
| 470 /** | 506 /** |
| 507 * An empty array of errors used when no errors are expected. |
| 508 */ |
| 509 static List<AnalysisError> _NO_ERRORS = new List<AnalysisError>(0); |
| 510 |
| 511 /** |
| 471 * The source being parsed. | 512 * The source being parsed. |
| 472 */ | 513 */ |
| 473 final String _rawSource; | 514 final String _rawSource; |
| 474 | 515 |
| 475 /** | 516 /** |
| 476 * The source being parsed after inserting a marker at the beginning and end o
f the range of the | 517 * The source being parsed after inserting a marker at the beginning and end o
f the range of the |
| 477 * most recent error. | 518 * most recent error. |
| 478 */ | 519 */ |
| 479 String _markedSource; | 520 String _markedSource; |
| 480 | 521 |
| 481 /** | 522 /** |
| 482 * A list containing the errors that were collected. | 523 * A list containing the errors that were collected. |
| 483 */ | 524 */ |
| 484 List<AnalysisError> _errors = new List<AnalysisError>(); | 525 List<AnalysisError> _errors = new List<AnalysisError>(); |
| 485 | 526 |
| 486 /** | 527 /** |
| 487 * A table mapping sources to the line information for the source. | 528 * A table mapping sources to the line information for the source. |
| 488 */ | 529 */ |
| 489 HashMap<Source, LineInfo> _lineInfoMap = new HashMap<Source, LineInfo>(); | 530 HashMap<Source, LineInfo> _lineInfoMap = new HashMap<Source, LineInfo>(); |
| 490 | 531 |
| 491 /** | 532 /** |
| 492 * An empty array of errors used when no errors are expected. | |
| 493 */ | |
| 494 static List<AnalysisError> _NO_ERRORS = new List<AnalysisError>(0); | |
| 495 | |
| 496 /** | |
| 497 * Initialize a newly created error listener to collect errors. | 533 * Initialize a newly created error listener to collect errors. |
| 498 */ | 534 */ |
| 499 GatheringErrorListener() : this.con1(null); | 535 GatheringErrorListener() : this.con1(null); |
| 500 | 536 |
| 501 /** | 537 /** |
| 502 * Initialize a newly created error listener to collect errors. | 538 * Initialize a newly created error listener to collect errors. |
| 503 */ | 539 */ |
| 504 GatheringErrorListener.con1(this._rawSource) { | 540 GatheringErrorListener.con1(this._rawSource) { |
| 505 this._markedSource = _rawSource; | 541 this._markedSource = _rawSource; |
| 506 } | 542 } |
| 507 | 543 |
| 508 /** | 544 /** |
| 545 * Return the errors that were collected. |
| 546 * |
| 547 * @return the errors that were collected |
| 548 */ |
| 549 List<AnalysisError> get errors => _errors; |
| 550 |
| 551 /** |
| 552 * Return `true` if at least one error has been gathered. |
| 553 * |
| 554 * @return `true` if at least one error has been gathered |
| 555 */ |
| 556 bool get hasErrors => _errors.length > 0; |
| 557 |
| 558 /** |
| 509 * Add all of the given errors to this listener. | 559 * Add all of the given errors to this listener. |
| 510 * | 560 * |
| 511 * @param the errors to be added | 561 * @param the errors to be added |
| 512 */ | 562 */ |
| 513 void addAll(List<AnalysisError> errors) { | 563 void addAll(List<AnalysisError> errors) { |
| 514 for (AnalysisError error in errors) { | 564 for (AnalysisError error in errors) { |
| 515 onError(error); | 565 onError(error); |
| 516 } | 566 } |
| 517 } | 567 } |
| 518 | 568 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 * @param expectedErrorCodes the error codes of the errors that should have be
en gathered | 607 * @param expectedErrorCodes the error codes of the errors that should have be
en gathered |
| 558 * @throws AssertionFailedError if a different number of errors have been gath
ered than were | 608 * @throws AssertionFailedError if a different number of errors have been gath
ered than were |
| 559 * expected | 609 * expected |
| 560 */ | 610 */ |
| 561 void assertErrorsWithCodes(List<ErrorCode> expectedErrorCodes) { | 611 void assertErrorsWithCodes(List<ErrorCode> expectedErrorCodes) { |
| 562 JavaStringBuilder builder = new JavaStringBuilder(); | 612 JavaStringBuilder builder = new JavaStringBuilder(); |
| 563 // | 613 // |
| 564 // Verify that the expected error codes have a non-empty message. | 614 // Verify that the expected error codes have a non-empty message. |
| 565 // | 615 // |
| 566 for (ErrorCode errorCode in expectedErrorCodes) { | 616 for (ErrorCode errorCode in expectedErrorCodes) { |
| 567 JUnitTestCase.assertFalseMsg("Empty error code message", errorCode.message
.isEmpty); | 617 JUnitTestCase.assertFalseMsg( |
| 618 "Empty error code message", |
| 619 errorCode.message.isEmpty); |
| 568 } | 620 } |
| 569 // | 621 // |
| 570 // Compute the expected number of each type of error. | 622 // Compute the expected number of each type of error. |
| 571 // | 623 // |
| 572 HashMap<ErrorCode, int> expectedCounts = new HashMap<ErrorCode, int>(); | 624 HashMap<ErrorCode, int> expectedCounts = new HashMap<ErrorCode, int>(); |
| 573 for (ErrorCode code in expectedErrorCodes) { | 625 for (ErrorCode code in expectedErrorCodes) { |
| 574 int count = expectedCounts[code]; | 626 int count = expectedCounts[code]; |
| 575 if (count == null) { | 627 if (count == null) { |
| 576 count = 1; | 628 count = 1; |
| 577 } else { | 629 } else { |
| 578 count = count + 1; | 630 count = count + 1; |
| 579 } | 631 } |
| 580 expectedCounts[code] = count; | 632 expectedCounts[code] = count; |
| 581 } | 633 } |
| 582 // | 634 // |
| 583 // Compute the actual number of each type of error. | 635 // Compute the actual number of each type of error. |
| 584 // | 636 // |
| 585 HashMap<ErrorCode, List<AnalysisError>> errorsByCode = new HashMap<ErrorCode
, List<AnalysisError>>(); | 637 HashMap<ErrorCode, List<AnalysisError>> errorsByCode = |
| 638 new HashMap<ErrorCode, List<AnalysisError>>(); |
| 586 for (AnalysisError error in _errors) { | 639 for (AnalysisError error in _errors) { |
| 587 ErrorCode code = error.errorCode; | 640 ErrorCode code = error.errorCode; |
| 588 List<AnalysisError> list = errorsByCode[code]; | 641 List<AnalysisError> list = errorsByCode[code]; |
| 589 if (list == null) { | 642 if (list == null) { |
| 590 list = new List<AnalysisError>(); | 643 list = new List<AnalysisError>(); |
| 591 errorsByCode[code] = list; | 644 errorsByCode[code] = list; |
| 592 } | 645 } |
| 593 list.add(error); | 646 list.add(error); |
| 594 } | 647 } |
| 595 // | 648 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 612 builder.append("; "); | 665 builder.append("; "); |
| 613 } | 666 } |
| 614 builder.append(expectedCount); | 667 builder.append(expectedCount); |
| 615 builder.append(" errors of type "); | 668 builder.append(" errors of type "); |
| 616 builder.append("${code.runtimeType.toString()}.${code}"); | 669 builder.append("${code.runtimeType.toString()}.${code}"); |
| 617 builder.append(", found "); | 670 builder.append(", found "); |
| 618 builder.append(actualCount); | 671 builder.append(actualCount); |
| 619 } | 672 } |
| 620 } | 673 } |
| 621 // | 674 // |
| 622 // Check that there are no more errors in the actual-errors map, otherwise,
record message. | 675 |
| 676 |
| 677 |
| 678 |
| 679 // Check that there are no more errors in the actual-errors map, otherwi
se, record message. |
| 623 // | 680 // |
| 624 for (MapEntry<ErrorCode, List<AnalysisError>> entry in getMapEntrySet(errors
ByCode)) { | 681 for (MapEntry<ErrorCode, List<AnalysisError>> entry in getMapEntrySet( |
| 682 errorsByCode)) { |
| 625 ErrorCode code = entry.getKey(); | 683 ErrorCode code = entry.getKey(); |
| 626 List<AnalysisError> actualErrors = entry.getValue(); | 684 List<AnalysisError> actualErrors = entry.getValue(); |
| 627 int actualCount = actualErrors.length; | 685 int actualCount = actualErrors.length; |
| 628 if (builder.length == 0) { | 686 if (builder.length == 0) { |
| 629 builder.append("Expected "); | 687 builder.append("Expected "); |
| 630 } else { | 688 } else { |
| 631 builder.append("; "); | 689 builder.append("; "); |
| 632 } | 690 } |
| 633 builder.append("0 errors of type "); | 691 builder.append("0 errors of type "); |
| 634 builder.append("${code.runtimeType.toString()}.${code}"); | 692 builder.append("${code.runtimeType.toString()}.${code}"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 } | 728 } |
| 671 int actualErrorCount = 0; | 729 int actualErrorCount = 0; |
| 672 int actualWarningCount = 0; | 730 int actualWarningCount = 0; |
| 673 for (AnalysisError error in _errors) { | 731 for (AnalysisError error in _errors) { |
| 674 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) { | 732 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) { |
| 675 actualErrorCount++; | 733 actualErrorCount++; |
| 676 } else { | 734 } else { |
| 677 actualWarningCount++; | 735 actualWarningCount++; |
| 678 } | 736 } |
| 679 } | 737 } |
| 680 if (expectedErrorCount != actualErrorCount || expectedWarningCount != actual
WarningCount) { | 738 if (expectedErrorCount != actualErrorCount || |
| 681 JUnitTestCase.fail("Expected ${expectedErrorCount} errors and ${expectedWa
rningCount} warnings, found ${actualErrorCount} errors and ${actualWarningCount}
warnings"); | 739 expectedWarningCount != actualWarningCount) { |
| 740 JUnitTestCase.fail( |
| 741 "Expected ${expectedErrorCount} errors and ${expectedWarningCount} war
nings, found ${actualErrorCount} errors and ${actualWarningCount} warnings"); |
| 682 } | 742 } |
| 683 } | 743 } |
| 684 | 744 |
| 685 /** | 745 /** |
| 686 * Assert that no errors have been gathered. | 746 * Assert that no errors have been gathered. |
| 687 * | 747 * |
| 688 * @throws AssertionFailedError if any errors have been gathered | 748 * @throws AssertionFailedError if any errors have been gathered |
| 689 */ | 749 */ |
| 690 void assertNoErrors() { | 750 void assertNoErrors() { |
| 691 assertErrors(_NO_ERRORS); | 751 assertErrors(_NO_ERRORS); |
| 692 } | 752 } |
| 693 | 753 |
| 694 /** | 754 /** |
| 695 * Return the errors that were collected. | |
| 696 * | |
| 697 * @return the errors that were collected | |
| 698 */ | |
| 699 List<AnalysisError> get errors => _errors; | |
| 700 | |
| 701 /** | |
| 702 * Return the line information associated with the given source, or `null` if
no line | 755 * Return the line information associated with the given source, or `null` if
no line |
| 703 * information has been associated with the source. | 756 * information has been associated with the source. |
| 704 * | 757 * |
| 705 * @param source the source with which the line information is associated | 758 * @param source the source with which the line information is associated |
| 706 * @return the line information associated with the source | 759 * @return the line information associated with the source |
| 707 */ | 760 */ |
| 708 LineInfo getLineInfo(Source source) => _lineInfoMap[source]; | 761 LineInfo getLineInfo(Source source) => _lineInfoMap[source]; |
| 709 | 762 |
| 710 /** | 763 /** |
| 711 * Return `true` if an error with the given error code has been gathered. | 764 * Return `true` if an error with the given error code has been gathered. |
| 712 * | 765 * |
| 713 * @param errorCode the error code being searched for | 766 * @param errorCode the error code being searched for |
| 714 * @return `true` if an error with the given error code has been gathered | 767 * @return `true` if an error with the given error code has been gathered |
| 715 */ | 768 */ |
| 716 bool hasError(ErrorCode errorCode) { | 769 bool hasError(ErrorCode errorCode) { |
| 717 for (AnalysisError error in _errors) { | 770 for (AnalysisError error in _errors) { |
| 718 if (identical(error.errorCode, errorCode)) { | 771 if (identical(error.errorCode, errorCode)) { |
| 719 return true; | 772 return true; |
| 720 } | 773 } |
| 721 } | 774 } |
| 722 return false; | 775 return false; |
| 723 } | 776 } |
| 724 | 777 |
| 725 /** | |
| 726 * Return `true` if at least one error has been gathered. | |
| 727 * | |
| 728 * @return `true` if at least one error has been gathered | |
| 729 */ | |
| 730 bool get hasErrors => _errors.length > 0; | |
| 731 | |
| 732 @override | 778 @override |
| 733 void onError(AnalysisError error) { | 779 void onError(AnalysisError error) { |
| 734 if (_rawSource != null) { | 780 if (_rawSource != null) { |
| 735 int left = error.offset; | 781 int left = error.offset; |
| 736 int right = left + error.length - 1; | 782 int right = left + error.length - 1; |
| 737 _markedSource = "${_rawSource.substring(0, left)}^${_rawSource.substring(l
eft, right)}^${_rawSource.substring(right)}"; | 783 _markedSource = |
| 784 "${_rawSource.substring(0, left)}^${_rawSource.substring(left, right)}
^${_rawSource.substring(right)}"; |
| 738 } | 785 } |
| 739 _errors.add(error); | 786 _errors.add(error); |
| 740 } | 787 } |
| 741 | 788 |
| 742 /** | 789 /** |
| 743 * Set the line information associated with the given source to the given info
rmation. | 790 * Set the line information associated with the given source to the given info
rmation. |
| 744 * | 791 * |
| 745 * @param source the source with which the line information is associated | 792 * @param source the source with which the line information is associated |
| 746 * @param lineStarts the line start information to be associated with the sour
ce | 793 * @param lineStarts the line start information to be associated with the sour
ce |
| 747 */ | 794 */ |
| 748 void setLineInfo(Source source, List<int> lineStarts) { | 795 void setLineInfo(Source source, List<int> lineStarts) { |
| 749 _lineInfoMap[source] = new LineInfo(lineStarts); | 796 _lineInfoMap[source] = new LineInfo(lineStarts); |
| 750 } | 797 } |
| 751 | 798 |
| 752 /** | 799 /** |
| 753 * Return `true` if the two errors are equivalent. | 800 * Return `true` if the two errors are equivalent. |
| 754 * | 801 * |
| 755 * @param firstError the first error being compared | 802 * @param firstError the first error being compared |
| 756 * @param secondError the second error being compared | 803 * @param secondError the second error being compared |
| 757 * @return `true` if the two errors are equivalent | 804 * @return `true` if the two errors are equivalent |
| 758 */ | 805 */ |
| 759 bool _equalErrors(AnalysisError firstError, AnalysisError secondError) => iden
tical(firstError.errorCode, secondError.errorCode) && firstError.offset == secon
dError.offset && firstError.length == secondError.length && _equalSources(firstE
rror.source, secondError.source); | 806 bool _equalErrors(AnalysisError firstError, AnalysisError secondError) => |
| 807 identical(firstError.errorCode, secondError.errorCode) && |
| 808 firstError.offset == secondError.offset && |
| 809 firstError.length == secondError.length && |
| 810 _equalSources(firstError.source, secondError.source); |
| 760 | 811 |
| 761 /** | 812 /** |
| 762 * Return `true` if the two sources are equivalent. | 813 * Return `true` if the two sources are equivalent. |
| 763 * | 814 * |
| 764 * @param firstSource the first source being compared | 815 * @param firstSource the first source being compared |
| 765 * @param secondSource the second source being compared | 816 * @param secondSource the second source being compared |
| 766 * @return `true` if the two sources are equivalent | 817 * @return `true` if the two sources are equivalent |
| 767 */ | 818 */ |
| 768 bool _equalSources(Source firstSource, Source secondSource) { | 819 bool _equalSources(Source firstSource, Source secondSource) { |
| 769 if (firstSource == null) { | 820 if (firstSource == null) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 786 PrintStringWriter writer = new PrintStringWriter(); | 837 PrintStringWriter writer = new PrintStringWriter(); |
| 787 writer.print("Expected "); | 838 writer.print("Expected "); |
| 788 writer.print(expectedErrors.length); | 839 writer.print(expectedErrors.length); |
| 789 writer.print(" errors:"); | 840 writer.print(" errors:"); |
| 790 for (AnalysisError error in expectedErrors) { | 841 for (AnalysisError error in expectedErrors) { |
| 791 Source source = error.source; | 842 Source source = error.source; |
| 792 LineInfo lineInfo = _lineInfoMap[source]; | 843 LineInfo lineInfo = _lineInfoMap[source]; |
| 793 writer.newLine(); | 844 writer.newLine(); |
| 794 if (lineInfo == null) { | 845 if (lineInfo == null) { |
| 795 int offset = error.offset; | 846 int offset = error.offset; |
| 796 writer.printf(" %s %s (%d..%d)", [ | 847 writer.printf( |
| 797 source == null ? "" : source.shortName, | 848 " %s %s (%d..%d)", |
| 798 error.errorCode, | 849 [ |
| 799 offset, | 850 source == null ? "" : source.shortName, |
| 800 offset + error.length]); | 851 error.errorCode, |
| 852 offset, |
| 853 offset + error.length]); |
| 801 } else { | 854 } else { |
| 802 LineInfo_Location location = lineInfo.getLocation(error.offset); | 855 LineInfo_Location location = lineInfo.getLocation(error.offset); |
| 803 writer.printf(" %s %s (%d, %d/%d)", [ | 856 writer.printf( |
| 804 source == null ? "" : source.shortName, | 857 " %s %s (%d, %d/%d)", |
| 805 error.errorCode, | 858 [ |
| 806 location.lineNumber, | 859 source == null ? "" : source.shortName, |
| 807 location.columnNumber, | 860 error.errorCode, |
| 808 error.length]); | 861 location.lineNumber, |
| 862 location.columnNumber, |
| 863 error.length]); |
| 809 } | 864 } |
| 810 } | 865 } |
| 811 writer.newLine(); | 866 writer.newLine(); |
| 812 writer.print("found "); | 867 writer.print("found "); |
| 813 writer.print(_errors.length); | 868 writer.print(_errors.length); |
| 814 writer.print(" errors:"); | 869 writer.print(" errors:"); |
| 815 for (AnalysisError error in _errors) { | 870 for (AnalysisError error in _errors) { |
| 816 Source source = error.source; | 871 Source source = error.source; |
| 817 LineInfo lineInfo = _lineInfoMap[source]; | 872 LineInfo lineInfo = _lineInfoMap[source]; |
| 818 writer.newLine(); | 873 writer.newLine(); |
| 819 if (lineInfo == null) { | 874 if (lineInfo == null) { |
| 820 int offset = error.offset; | 875 int offset = error.offset; |
| 821 writer.printf(" %s %s (%d..%d): %s", [ | 876 writer.printf( |
| 822 source == null ? "" : source.shortName, | 877 " %s %s (%d..%d): %s", |
| 823 error.errorCode, | 878 [ |
| 824 offset, | 879 source == null ? "" : source.shortName, |
| 825 offset + error.length, | 880 error.errorCode, |
| 826 error.message]); | 881 offset, |
| 882 offset + error.length, |
| 883 error.message]); |
| 827 } else { | 884 } else { |
| 828 LineInfo_Location location = lineInfo.getLocation(error.offset); | 885 LineInfo_Location location = lineInfo.getLocation(error.offset); |
| 829 writer.printf(" %s %s (%d, %d/%d): %s", [ | 886 writer.printf( |
| 830 source == null ? "" : source.shortName, | 887 " %s %s (%d, %d/%d): %s", |
| 831 error.errorCode, | 888 [ |
| 832 location.lineNumber, | 889 source == null ? "" : source.shortName, |
| 833 location.columnNumber, | 890 error.errorCode, |
| 834 error.length, | 891 location.lineNumber, |
| 835 error.message]); | 892 location.columnNumber, |
| 893 error.length, |
| 894 error.message]); |
| 836 } | 895 } |
| 837 } | 896 } |
| 838 JUnitTestCase.fail(writer.toString()); | 897 JUnitTestCase.fail(writer.toString()); |
| 839 } | 898 } |
| 840 | 899 |
| 841 /** | 900 /** |
| 842 * Search through the given list of errors for an error that is equal to the t
arget error. If one | 901 * Search through the given list of errors for an error that is equal to the t
arget error. If one |
| 843 * is found, remove it from the list and return `true`, otherwise return `fals
e` | 902 * is found, remove it from the list and return `true`, otherwise return `fals
e` |
| 844 * without modifying the list. | 903 * without modifying the list. |
| 845 * | 904 * |
| 846 * @param errors the errors through which we are searching | 905 * @param errors the errors through which we are searching |
| 847 * @param targetError the error being searched for | 906 * @param targetError the error being searched for |
| 848 * @return `true` if the error is found and removed from the list | 907 * @return `true` if the error is found and removed from the list |
| 849 */ | 908 */ |
| 850 bool _foundAndRemoved(List<AnalysisError> errors, AnalysisError targetError) { | 909 bool _foundAndRemoved(List<AnalysisError> errors, AnalysisError targetError) { |
| 851 for (AnalysisError error in errors) { | 910 for (AnalysisError error in errors) { |
| 852 if (_equalErrors(error, targetError)) { | 911 if (_equalErrors(error, targetError)) { |
| 853 errors.remove(error); | 912 errors.remove(error); |
| 854 return true; | 913 return true; |
| 855 } | 914 } |
| 856 } | 915 } |
| 857 return false; | 916 return false; |
| 858 } | 917 } |
| 859 } | 918 } |
| 860 | 919 |
| 861 main() { | 920 /** |
| 921 * Instances of the class [TestLogger] implement a logger that can be used by |
| 922 * tests. |
| 923 */ |
| 924 class TestLogger implements Logger { |
| 925 /** |
| 926 * The number of error messages that were logged. |
| 927 */ |
| 928 int errorCount = 0; |
| 929 |
| 930 /** |
| 931 * The number of informational messages that were logged. |
| 932 */ |
| 933 int infoCount = 0; |
| 934 |
| 935 @override |
| 936 void logError(String message) { |
| 937 errorCount++; |
| 938 } |
| 939 |
| 940 @override |
| 941 void logError2(String message, Exception exception) { |
| 942 errorCount++; |
| 943 } |
| 944 |
| 945 @override |
| 946 void logInformation(String message) { |
| 947 infoCount++; |
| 948 } |
| 949 |
| 950 @override |
| 951 void logInformation2(String message, Exception exception) { |
| 952 infoCount++; |
| 953 } |
| 862 } | 954 } |
| 863 | 955 |
| 956 |
| 864 class TestSource implements Source { | 957 class TestSource implements Source { |
| 865 String _name; | 958 String _name; |
| 866 TestSource([this._name = '/test.dart']); | 959 String _contents; |
| 867 int get hashCode => 0; | 960 TestSource([this._name = '/test.dart', this._contents]); |
| 868 bool operator ==(Object object) { | 961 TimestampedData<String> get contents { |
| 869 return object is TestSource; | 962 return new TimestampedData<String>(0, _contents); |
| 870 } | 963 } |
| 871 AnalysisContext get context { | 964 String get encoding { |
| 872 throw new UnsupportedOperationException(); | |
| 873 } | |
| 874 void getContentsToReceiver(Source_ContentReceiver receiver) { | |
| 875 throw new UnsupportedOperationException(); | 965 throw new UnsupportedOperationException(); |
| 876 } | 966 } |
| 877 String get fullName { | 967 String get fullName { |
| 878 return _name; | 968 return _name; |
| 879 } | 969 } |
| 970 int get hashCode => 0; |
| 971 bool get isInSystemLibrary { |
| 972 return false; |
| 973 } |
| 974 int get modificationStamp { |
| 975 return 0; |
| 976 } |
| 880 String get shortName { | 977 String get shortName { |
| 881 return _name; | 978 return _name; |
| 882 } | 979 } |
| 883 String get encoding { | 980 Uri get uri { |
| 884 throw new UnsupportedOperationException(); | 981 throw new UnsupportedOperationException(); |
| 885 } | 982 } |
| 886 int get modificationStamp { | 983 UriKind get uriKind { |
| 887 throw new UnsupportedOperationException(); | 984 throw new UnsupportedOperationException(); |
| 888 } | 985 } |
| 986 bool operator ==(Object object) { |
| 987 return object is TestSource; |
| 988 } |
| 889 bool exists() => true; | 989 bool exists() => true; |
| 890 bool get isInSystemLibrary { | 990 void getContentsToReceiver(Source_ContentReceiver receiver) { |
| 891 throw new UnsupportedOperationException(); | 991 throw new UnsupportedOperationException(); |
| 892 } | 992 } |
| 893 Source resolve(String uri) { | 993 Source resolve(String uri) { |
| 894 throw new UnsupportedOperationException(); | 994 throw new UnsupportedOperationException(); |
| 895 } | 995 } |
| 896 Uri resolveRelativeUri(Uri uri) { | 996 Uri resolveRelativeUri(Uri uri) { |
| 897 throw new UnsupportedOperationException(); | 997 return new Uri(scheme: 'file', path: _name).resolveUri(uri); |
| 898 } | |
| 899 UriKind get uriKind { | |
| 900 throw new UnsupportedOperationException(); | |
| 901 } | |
| 902 Uri get uri { | |
| 903 throw new UnsupportedOperationException(); | |
| 904 } | |
| 905 TimestampedData<String> get contents { | |
| 906 throw new UnsupportedOperationException(); | |
| 907 } | 998 } |
| 908 } | 999 } |
| OLD | NEW |