| 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 library test.computer.element; | 5 library test.computer.element; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/constants.dart'; |
| 7 import 'package:analysis_server/src/protocol_server.dart'; | 8 import 'package:analysis_server/src/protocol_server.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart' as engine; |
| 9 import 'package:analyzer/src/generated/element.dart' as engine; | 10 import 'package:analyzer/src/generated/element.dart' as engine; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/error.dart' as engine; |
| 11 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; | 12 import 'package:analyzer/src/generated/source.dart' as engine; |
| 13 import 'package:typed_mock/typed_mock.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 13 | 15 |
| 14 import '../abstract_context.dart'; | 16 import 'abstract_context.dart'; |
| 15 import '../reflective_tests.dart'; | 17 import 'mocks.dart'; |
| 18 import 'reflective_tests.dart'; |
| 16 | 19 |
| 17 | 20 |
| 18 | 21 |
| 19 main() { | 22 main() { |
| 20 groupSep = ' | '; | 23 groupSep = ' | '; |
| 24 runReflectiveTests(AnalysisErrorTest); |
| 21 runReflectiveTests(ElementTest); | 25 runReflectiveTests(ElementTest); |
| 22 runReflectiveTests(ElementKindTest); | 26 runReflectiveTests(ElementKindTest); |
| 23 } | 27 } |
| 24 | 28 |
| 25 | 29 |
| 30 class AnalysisErrorMock extends TypedMock implements engine.AnalysisError { |
| 31 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 32 } |
| 33 |
| 34 |
| 35 @ReflectiveTestCase() |
| 36 class AnalysisErrorTest { |
| 37 engine.Source source = new MockSource(); |
| 38 engine.LineInfo lineInfo; |
| 39 engine.AnalysisError engineError = new AnalysisErrorMock(); |
| 40 |
| 41 void setUp() { |
| 42 // prepare Source |
| 43 when(source.fullName).thenReturn('foo.dart'); |
| 44 // prepare LineInfo |
| 45 lineInfo = new engine.LineInfo([0, 5, 9, 20]); |
| 46 // prepare AnalysisError |
| 47 when(engineError.source).thenReturn(source); |
| 48 when( |
| 49 engineError.errorCode).thenReturn(engine.CompileTimeErrorCode.AMBIGUOUS_
EXPORT); |
| 50 when(engineError.message).thenReturn('my message'); |
| 51 when(engineError.offset).thenReturn(10); |
| 52 when(engineError.length).thenReturn(20); |
| 53 } |
| 54 |
| 55 void tearDown() { |
| 56 source = null; |
| 57 engineError = null; |
| 58 } |
| 59 |
| 60 void test_fromEngine_hasCorrection() { |
| 61 when(engineError.correction).thenReturn('my correction'); |
| 62 AnalysisError error = newAnalysisError_fromEngine(lineInfo, engineError); |
| 63 expect(error.toJson(), { |
| 64 SEVERITY: 'ERROR', |
| 65 TYPE: 'COMPILE_TIME_ERROR', |
| 66 LOCATION: { |
| 67 FILE: 'foo.dart', |
| 68 OFFSET: 10, |
| 69 LENGTH: 20, |
| 70 START_LINE: 3, |
| 71 START_COLUMN: 2 |
| 72 }, |
| 73 MESSAGE: 'my message', |
| 74 CORRECTION: 'my correction' |
| 75 }); |
| 76 } |
| 77 |
| 78 void test_fromEngine_noCorrection() { |
| 79 when(engineError.correction).thenReturn(null); |
| 80 AnalysisError error = newAnalysisError_fromEngine(lineInfo, engineError); |
| 81 expect(error.toJson(), { |
| 82 SEVERITY: 'ERROR', |
| 83 TYPE: 'COMPILE_TIME_ERROR', |
| 84 LOCATION: { |
| 85 FILE: 'foo.dart', |
| 86 OFFSET: 10, |
| 87 LENGTH: 20, |
| 88 START_LINE: 3, |
| 89 START_COLUMN: 2 |
| 90 }, |
| 91 MESSAGE: 'my message' |
| 92 }); |
| 93 } |
| 94 |
| 95 void test_fromEngine_noLineInfo() { |
| 96 when(engineError.correction).thenReturn(null); |
| 97 AnalysisError error = newAnalysisError_fromEngine(null, engineError); |
| 98 expect(error.toJson(), { |
| 99 SEVERITY: 'ERROR', |
| 100 TYPE: 'COMPILE_TIME_ERROR', |
| 101 LOCATION: { |
| 102 FILE: 'foo.dart', |
| 103 OFFSET: 10, |
| 104 LENGTH: 20, |
| 105 START_LINE: -1, |
| 106 START_COLUMN: -1 |
| 107 }, |
| 108 MESSAGE: 'my message' |
| 109 }); |
| 110 } |
| 111 } |
| 112 |
| 113 |
| 26 class ElementKindTest { | 114 class ElementKindTest { |
| 27 void test_fromEngine() { | 115 void test_fromEngine() { |
| 28 expect( | 116 expect( |
| 29 newElementKind_fromEngine(engine.ElementKind.CLASS), | 117 newElementKind_fromEngine(engine.ElementKind.CLASS), |
| 30 ElementKind.CLASS); | 118 ElementKind.CLASS); |
| 31 expect( | 119 expect( |
| 32 newElementKind_fromEngine(engine.ElementKind.COMPILATION_UNIT), | 120 newElementKind_fromEngine(engine.ElementKind.COMPILATION_UNIT), |
| 33 ElementKind.COMPILATION_UNIT); | 121 ElementKind.COMPILATION_UNIT); |
| 34 expect( | 122 expect( |
| 35 newElementKind_fromEngine(engine.ElementKind.CONSTRUCTOR), | 123 newElementKind_fromEngine(engine.ElementKind.CONSTRUCTOR), |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); | 209 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); |
| 122 expect( | 210 expect( |
| 123 ElementKind.COMPILATION_UNIT.toString(), | 211 ElementKind.COMPILATION_UNIT.toString(), |
| 124 'ElementKind.COMPILATION_UNIT'); | 212 'ElementKind.COMPILATION_UNIT'); |
| 125 } | 213 } |
| 126 } | 214 } |
| 127 | 215 |
| 128 | 216 |
| 129 @ReflectiveTestCase() | 217 @ReflectiveTestCase() |
| 130 class ElementTest extends AbstractContextTest { | 218 class ElementTest extends AbstractContextTest { |
| 131 engine.Element findElementInUnit(CompilationUnit unit, String name, | 219 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, |
| 132 [engine.ElementKind kind]) { | 220 [engine.ElementKind kind]) { |
| 133 return findChildElement(unit.element, name, kind); | 221 return findChildElement(unit.element, name, kind); |
| 134 } | 222 } |
| 135 | 223 |
| 136 void test_fromElement_CLASS() { | 224 void test_fromElement_CLASS() { |
| 137 Source source = addSource('/test.dart', ''' | 225 engine.Source source = addSource('/test.dart', ''' |
| 138 @deprecated | 226 @deprecated |
| 139 abstract class _MyClass {}'''); | 227 abstract class _MyClass {}'''); |
| 140 CompilationUnit unit = resolveLibraryUnit(source); | 228 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 141 engine.ClassElement engineElement = findElementInUnit(unit, '_MyClass'); | 229 engine.ClassElement engineElement = findElementInUnit(unit, '_MyClass'); |
| 142 // create notification Element | 230 // create notification Element |
| 143 Element element = newElement_fromEngine(engineElement); | 231 Element element = newElement_fromEngine(engineElement); |
| 144 expect(element.kind, ElementKind.CLASS); | 232 expect(element.kind, ElementKind.CLASS); |
| 145 expect(element.name, '_MyClass'); | 233 expect(element.name, '_MyClass'); |
| 146 { | 234 { |
| 147 Location location = element.location; | 235 Location location = element.location; |
| 148 expect(location.file, '/test.dart'); | 236 expect(location.file, '/test.dart'); |
| 149 expect(location.offset, 27); | 237 expect(location.offset, 27); |
| 150 expect(location.length, '_MyClass'.length); | 238 expect(location.length, '_MyClass'.length); |
| 151 expect(location.startLine, 2); | 239 expect(location.startLine, 2); |
| 152 expect(location.startColumn, 16); | 240 expect(location.startColumn, 16); |
| 153 } | 241 } |
| 154 expect( | 242 expect( |
| 155 element.flags, | 243 element.flags, |
| 156 Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE); | 244 Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE); |
| 157 } | 245 } |
| 158 | 246 |
| 159 void test_fromElement_CONSTRUCTOR() { | 247 void test_fromElement_CONSTRUCTOR() { |
| 160 Source source = addSource('/test.dart', ''' | 248 engine.Source source = addSource('/test.dart', ''' |
| 161 class A { | 249 class A { |
| 162 const A.myConstructor(int a, [String b]); | 250 const A.myConstructor(int a, [String b]); |
| 163 }'''); | 251 }'''); |
| 164 CompilationUnit unit = resolveLibraryUnit(source); | 252 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 165 engine.ConstructorElement engineElement = | 253 engine.ConstructorElement engineElement = |
| 166 findElementInUnit(unit, 'myConstructor'); | 254 findElementInUnit(unit, 'myConstructor'); |
| 167 // create notification Element | 255 // create notification Element |
| 168 Element element = newElement_fromEngine(engineElement); | 256 Element element = newElement_fromEngine(engineElement); |
| 169 expect(element.kind, ElementKind.CONSTRUCTOR); | 257 expect(element.kind, ElementKind.CONSTRUCTOR); |
| 170 expect(element.name, 'myConstructor'); | 258 expect(element.name, 'myConstructor'); |
| 171 { | 259 { |
| 172 Location location = element.location; | 260 Location location = element.location; |
| 173 expect(location.file, '/test.dart'); | 261 expect(location.file, '/test.dart'); |
| 174 expect(location.offset, 20); | 262 expect(location.offset, 20); |
| 175 expect(location.length, 'myConstructor'.length); | 263 expect(location.length, 'myConstructor'.length); |
| 176 expect(location.startLine, 2); | 264 expect(location.startLine, 2); |
| 177 expect(location.startColumn, 11); | 265 expect(location.startColumn, 11); |
| 178 } | 266 } |
| 179 expect(element.parameters, '(int a, [String b])'); | 267 expect(element.parameters, '(int a, [String b])'); |
| 180 expect(element.returnType, 'A'); | 268 expect(element.returnType, 'A'); |
| 181 expect(element.flags, Element.FLAG_CONST); | 269 expect(element.flags, Element.FLAG_CONST); |
| 182 } | 270 } |
| 183 | 271 |
| 184 void test_fromElement_FIELD() { | 272 void test_fromElement_FIELD() { |
| 185 Source source = addSource('/test.dart', ''' | 273 engine.Source source = addSource('/test.dart', ''' |
| 186 class A { | 274 class A { |
| 187 static const myField = 42; | 275 static const myField = 42; |
| 188 }'''); | 276 }'''); |
| 189 CompilationUnit unit = resolveLibraryUnit(source); | 277 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 190 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | 278 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); |
| 191 // create notification Element | 279 // create notification Element |
| 192 Element element = newElement_fromEngine(engineElement); | 280 Element element = newElement_fromEngine(engineElement); |
| 193 expect(element.kind, ElementKind.FIELD); | 281 expect(element.kind, ElementKind.FIELD); |
| 194 expect(element.name, 'myField'); | 282 expect(element.name, 'myField'); |
| 195 { | 283 { |
| 196 Location location = element.location; | 284 Location location = element.location; |
| 197 expect(location.file, '/test.dart'); | 285 expect(location.file, '/test.dart'); |
| 198 expect(location.offset, 25); | 286 expect(location.offset, 25); |
| 199 expect(location.length, 'myField'.length); | 287 expect(location.length, 'myField'.length); |
| 200 expect(location.startLine, 2); | 288 expect(location.startLine, 2); |
| 201 expect(location.startColumn, 16); | 289 expect(location.startColumn, 16); |
| 202 } | 290 } |
| 203 expect(element.parameters, isNull); | 291 expect(element.parameters, isNull); |
| 204 expect(element.returnType, isNull); | 292 expect(element.returnType, isNull); |
| 205 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 293 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 206 } | 294 } |
| 207 | 295 |
| 208 void test_fromElement_GETTER() { | 296 void test_fromElement_GETTER() { |
| 209 Source source = addSource('/test.dart', ''' | 297 engine.Source source = addSource('/test.dart', ''' |
| 210 class A { | 298 class A { |
| 211 String myGetter => 42; | 299 String myGetter => 42; |
| 212 }'''); | 300 }'''); |
| 213 CompilationUnit unit = resolveLibraryUnit(source); | 301 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 214 engine.PropertyAccessorElement engineElement = | 302 engine.PropertyAccessorElement engineElement = |
| 215 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); | 303 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); |
| 216 // create notification Element | 304 // create notification Element |
| 217 Element element = newElement_fromEngine(engineElement); | 305 Element element = newElement_fromEngine(engineElement); |
| 218 expect(element.kind, ElementKind.GETTER); | 306 expect(element.kind, ElementKind.GETTER); |
| 219 expect(element.name, 'myGetter'); | 307 expect(element.name, 'myGetter'); |
| 220 { | 308 { |
| 221 Location location = element.location; | 309 Location location = element.location; |
| 222 expect(location.file, '/test.dart'); | 310 expect(location.file, '/test.dart'); |
| 223 expect(location.offset, 19); | 311 expect(location.offset, 19); |
| 224 expect(location.length, 'myGetter'.length); | 312 expect(location.length, 'myGetter'.length); |
| 225 expect(location.startLine, 2); | 313 expect(location.startLine, 2); |
| 226 expect(location.startColumn, 10); | 314 expect(location.startColumn, 10); |
| 227 } | 315 } |
| 228 expect(element.parameters, '()'); | 316 expect(element.parameters, '()'); |
| 229 expect(element.returnType, 'String'); | 317 expect(element.returnType, 'String'); |
| 230 expect(element.flags, 0); | 318 expect(element.flags, 0); |
| 231 } | 319 } |
| 232 | 320 |
| 233 void test_fromElement_LABEL() { | 321 void test_fromElement_LABEL() { |
| 234 Source source = addSource('/test.dart', ''' | 322 engine.Source source = addSource('/test.dart', ''' |
| 235 main() { | 323 main() { |
| 236 myLabel: | 324 myLabel: |
| 237 while (true) { | 325 while (true) { |
| 238 break myLabel; | 326 break myLabel; |
| 239 } | 327 } |
| 240 }'''); | 328 }'''); |
| 241 CompilationUnit unit = resolveLibraryUnit(source); | 329 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 242 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); | 330 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); |
| 243 // create notification Element | 331 // create notification Element |
| 244 Element element = newElement_fromEngine(engineElement); | 332 Element element = newElement_fromEngine(engineElement); |
| 245 expect(element.kind, ElementKind.LABEL); | 333 expect(element.kind, ElementKind.LABEL); |
| 246 expect(element.name, 'myLabel'); | 334 expect(element.name, 'myLabel'); |
| 247 { | 335 { |
| 248 Location location = element.location; | 336 Location location = element.location; |
| 249 expect(location.file, '/test.dart'); | 337 expect(location.file, '/test.dart'); |
| 250 expect(location.offset, 9); | 338 expect(location.offset, 9); |
| 251 expect(location.length, 'myLabel'.length); | 339 expect(location.length, 'myLabel'.length); |
| 252 expect(location.startLine, 2); | 340 expect(location.startLine, 2); |
| 253 expect(location.startColumn, 1); | 341 expect(location.startColumn, 1); |
| 254 } | 342 } |
| 255 expect(element.parameters, isNull); | 343 expect(element.parameters, isNull); |
| 256 expect(element.returnType, isNull); | 344 expect(element.returnType, isNull); |
| 257 expect(element.flags, 0); | 345 expect(element.flags, 0); |
| 258 } | 346 } |
| 259 | 347 |
| 260 void test_fromElement_METHOD() { | 348 void test_fromElement_METHOD() { |
| 261 Source source = addSource('/test.dart', ''' | 349 engine.Source source = addSource('/test.dart', ''' |
| 262 class A { | 350 class A { |
| 263 static List<String> myMethod(int a, {String b}) { | 351 static List<String> myMethod(int a, {String b}) { |
| 264 return null; | 352 return null; |
| 265 } | 353 } |
| 266 }'''); | 354 }'''); |
| 267 CompilationUnit unit = resolveLibraryUnit(source); | 355 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 268 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | 356 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); |
| 269 // create notification Element | 357 // create notification Element |
| 270 Element element = newElement_fromEngine(engineElement); | 358 Element element = newElement_fromEngine(engineElement); |
| 271 expect(element.kind, ElementKind.METHOD); | 359 expect(element.kind, ElementKind.METHOD); |
| 272 expect(element.name, 'myMethod'); | 360 expect(element.name, 'myMethod'); |
| 273 { | 361 { |
| 274 Location location = element.location; | 362 Location location = element.location; |
| 275 expect(location.file, '/test.dart'); | 363 expect(location.file, '/test.dart'); |
| 276 expect(location.offset, 32); | 364 expect(location.offset, 32); |
| 277 expect(location.length, 'myGetter'.length); | 365 expect(location.length, 'myGetter'.length); |
| 278 expect(location.startLine, 2); | 366 expect(location.startLine, 2); |
| 279 expect(location.startColumn, 23); | 367 expect(location.startColumn, 23); |
| 280 } | 368 } |
| 281 expect(element.parameters, '(int a, {String b})'); | 369 expect(element.parameters, '(int a, {String b})'); |
| 282 expect(element.returnType, 'List<String>'); | 370 expect(element.returnType, 'List<String>'); |
| 283 expect(element.flags, Element.FLAG_STATIC); | 371 expect(element.flags, Element.FLAG_STATIC); |
| 284 } | 372 } |
| 285 | 373 |
| 286 void test_fromElement_SETTER() { | 374 void test_fromElement_SETTER() { |
| 287 Source source = addSource('/test.dart', ''' | 375 engine.Source source = addSource('/test.dart', ''' |
| 288 class A { | 376 class A { |
| 289 set mySetter(String x) {} | 377 set mySetter(String x) {} |
| 290 }'''); | 378 }'''); |
| 291 CompilationUnit unit = resolveLibraryUnit(source); | 379 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 292 engine.FieldElement engineFieldElement = | 380 engine.FieldElement engineFieldElement = |
| 293 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); | 381 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); |
| 294 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; | 382 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; |
| 295 // create notification Element | 383 // create notification Element |
| 296 Element element = newElement_fromEngine(engineElement); | 384 Element element = newElement_fromEngine(engineElement); |
| 297 expect(element.kind, ElementKind.SETTER); | 385 expect(element.kind, ElementKind.SETTER); |
| 298 expect(element.name, 'mySetter'); | 386 expect(element.name, 'mySetter'); |
| 299 { | 387 { |
| 300 Location location = element.location; | 388 Location location = element.location; |
| 301 expect(location.file, '/test.dart'); | 389 expect(location.file, '/test.dart'); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 314 // create notification Element | 402 // create notification Element |
| 315 Element element = newElement_fromEngine(engineElement); | 403 Element element = newElement_fromEngine(engineElement); |
| 316 expect(element.kind, ElementKind.UNKNOWN); | 404 expect(element.kind, ElementKind.UNKNOWN); |
| 317 expect(element.name, 'dynamic'); | 405 expect(element.name, 'dynamic'); |
| 318 expect(element.location, isNull); | 406 expect(element.location, isNull); |
| 319 expect(element.parameters, isNull); | 407 expect(element.parameters, isNull); |
| 320 expect(element.returnType, isNull); | 408 expect(element.returnType, isNull); |
| 321 expect(element.flags, 0); | 409 expect(element.flags, 0); |
| 322 } | 410 } |
| 323 } | 411 } |
| OLD | NEW |