| 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 'dart:mirrors'; | 7 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 8 | 8 import 'package:analysis_server/plugin/protocol/protocol_dart.dart'; |
| 9 import 'package:analysis_server/src/constants.dart'; | |
| 10 import 'package:analysis_server/src/protocol_server.dart'; | |
| 11 import 'package:analysis_server/src/services/search/search_engine.dart'; | |
| 12 import 'package:analyzer/src/generated/ast.dart' as engine; | 9 import 'package:analyzer/src/generated/ast.dart' as engine; |
| 13 import 'package:analyzer/src/generated/element.dart' as engine; | 10 import 'package:analyzer/src/generated/element.dart' as engine; |
| 14 import 'package:analyzer/src/generated/error.dart' as engine; | 11 import 'package:analyzer/src/generated/error.dart' as engine; |
| 15 import 'package:analyzer/src/generated/source.dart' as engine; | 12 import 'package:analyzer/src/generated/source.dart' as engine; |
| 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 17 import 'package:typed_mock/typed_mock.dart'; | |
| 18 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 19 | 15 |
| 20 import 'abstract_context.dart'; | 16 import '../abstract_context.dart'; |
| 21 import 'mocks.dart'; | 17 import '../utils.dart'; |
| 22 import 'utils.dart'; | |
| 23 | 18 |
| 24 main() { | 19 main() { |
| 25 initializeTestEnvironment(); | 20 initializeTestEnvironment(); |
| 26 defineReflectiveTests(AnalysisErrorTest); | |
| 27 defineReflectiveTests(ElementTest); | 21 defineReflectiveTests(ElementTest); |
| 28 defineReflectiveTests(ElementKindTest); | 22 defineReflectiveTests(ElementKindTest); |
| 29 defineReflectiveTests(EnumTest); | |
| 30 } | |
| 31 | |
| 32 class AnalysisErrorMock extends TypedMock implements engine.AnalysisError { | |
| 33 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
| 34 } | |
| 35 | |
| 36 @reflectiveTest | |
| 37 class AnalysisErrorTest { | |
| 38 engine.Source source = new MockSource(); | |
| 39 engine.LineInfo lineInfo; | |
| 40 engine.AnalysisError engineError = new AnalysisErrorMock(); | |
| 41 | |
| 42 void setUp() { | |
| 43 // prepare Source | |
| 44 when(source.fullName).thenReturn('foo.dart'); | |
| 45 // prepare LineInfo | |
| 46 lineInfo = new engine.LineInfo([0, 5, 9, 20]); | |
| 47 // prepare AnalysisError | |
| 48 when(engineError.source).thenReturn(source); | |
| 49 when(engineError.errorCode) | |
| 50 .thenReturn(engine.CompileTimeErrorCode.AMBIGUOUS_EXPORT); | |
| 51 when(engineError.message).thenReturn('my message'); | |
| 52 when(engineError.offset).thenReturn(10); | |
| 53 when(engineError.length).thenReturn(20); | |
| 54 } | |
| 55 | |
| 56 void tearDown() { | |
| 57 source = null; | |
| 58 engineError = null; | |
| 59 } | |
| 60 | |
| 61 void test_fromEngine_hasCorrection() { | |
| 62 when(engineError.correction).thenReturn('my correction'); | |
| 63 AnalysisError error = newAnalysisError_fromEngine(lineInfo, engineError); | |
| 64 expect(error.toJson(), { | |
| 65 SEVERITY: 'ERROR', | |
| 66 TYPE: 'COMPILE_TIME_ERROR', | |
| 67 LOCATION: { | |
| 68 FILE: 'foo.dart', | |
| 69 OFFSET: 10, | |
| 70 LENGTH: 20, | |
| 71 START_LINE: 3, | |
| 72 START_COLUMN: 2 | |
| 73 }, | |
| 74 MESSAGE: 'my message', | |
| 75 CORRECTION: 'my correction', | |
| 76 HAS_FIX : false | |
| 77 }); | |
| 78 } | |
| 79 | |
| 80 void test_fromEngine_noCorrection() { | |
| 81 when(engineError.correction).thenReturn(null); | |
| 82 AnalysisError error = newAnalysisError_fromEngine(lineInfo, engineError); | |
| 83 expect(error.toJson(), { | |
| 84 SEVERITY: 'ERROR', | |
| 85 TYPE: 'COMPILE_TIME_ERROR', | |
| 86 LOCATION: { | |
| 87 FILE: 'foo.dart', | |
| 88 OFFSET: 10, | |
| 89 LENGTH: 20, | |
| 90 START_LINE: 3, | |
| 91 START_COLUMN: 2 | |
| 92 }, | |
| 93 MESSAGE: 'my message', | |
| 94 HAS_FIX : false | |
| 95 }); | |
| 96 } | |
| 97 | |
| 98 void test_fromEngine_noLineInfo() { | |
| 99 when(engineError.correction).thenReturn(null); | |
| 100 AnalysisError error = newAnalysisError_fromEngine(null, engineError); | |
| 101 expect(error.toJson(), { | |
| 102 SEVERITY: 'ERROR', | |
| 103 TYPE: 'COMPILE_TIME_ERROR', | |
| 104 LOCATION: { | |
| 105 FILE: 'foo.dart', | |
| 106 OFFSET: 10, | |
| 107 LENGTH: 20, | |
| 108 START_LINE: -1, | |
| 109 START_COLUMN: -1 | |
| 110 }, | |
| 111 MESSAGE: 'my message', | |
| 112 HAS_FIX : false | |
| 113 }); | |
| 114 } | |
| 115 } | 23 } |
| 116 | 24 |
| 117 @reflectiveTest | 25 @reflectiveTest |
| 118 class ElementKindTest { | 26 class ElementKindTest { |
| 119 void test_fromEngine() { | 27 void test_fromEngine() { |
| 28 expect(convertElementKind(engine.ElementKind.CLASS), ElementKind.CLASS); |
| 29 expect(convertElementKind(engine.ElementKind.COMPILATION_UNIT), |
| 30 ElementKind.COMPILATION_UNIT); |
| 31 expect(convertElementKind(engine.ElementKind.CONSTRUCTOR), |
| 32 ElementKind.CONSTRUCTOR); |
| 33 expect(convertElementKind(engine.ElementKind.FIELD), ElementKind.FIELD); |
| 120 expect( | 34 expect( |
| 121 newElementKind_fromEngine(engine.ElementKind.CLASS), ElementKind.CLASS); | 35 convertElementKind(engine.ElementKind.FUNCTION), ElementKind.FUNCTION); |
| 122 expect(newElementKind_fromEngine(engine.ElementKind.COMPILATION_UNIT), | 36 expect(convertElementKind(engine.ElementKind.FUNCTION_TYPE_ALIAS), |
| 123 ElementKind.COMPILATION_UNIT); | |
| 124 expect(newElementKind_fromEngine(engine.ElementKind.CONSTRUCTOR), | |
| 125 ElementKind.CONSTRUCTOR); | |
| 126 expect( | |
| 127 newElementKind_fromEngine(engine.ElementKind.FIELD), ElementKind.FIELD); | |
| 128 expect(newElementKind_fromEngine(engine.ElementKind.FUNCTION), | |
| 129 ElementKind.FUNCTION); | |
| 130 expect(newElementKind_fromEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), | |
| 131 ElementKind.FUNCTION_TYPE_ALIAS); | 37 ElementKind.FUNCTION_TYPE_ALIAS); |
| 132 expect(newElementKind_fromEngine(engine.ElementKind.GETTER), | 38 expect(convertElementKind(engine.ElementKind.GETTER), ElementKind.GETTER); |
| 133 ElementKind.GETTER); | 39 expect(convertElementKind(engine.ElementKind.LABEL), ElementKind.LABEL); |
| 134 expect( | 40 expect(convertElementKind(engine.ElementKind.LIBRARY), ElementKind.LIBRARY); |
| 135 newElementKind_fromEngine(engine.ElementKind.LABEL), ElementKind.LABEL); | 41 expect(convertElementKind(engine.ElementKind.LOCAL_VARIABLE), |
| 136 expect(newElementKind_fromEngine(engine.ElementKind.LIBRARY), | |
| 137 ElementKind.LIBRARY); | |
| 138 expect(newElementKind_fromEngine(engine.ElementKind.LOCAL_VARIABLE), | |
| 139 ElementKind.LOCAL_VARIABLE); | 42 ElementKind.LOCAL_VARIABLE); |
| 140 expect(newElementKind_fromEngine(engine.ElementKind.METHOD), | 43 expect(convertElementKind(engine.ElementKind.METHOD), ElementKind.METHOD); |
| 141 ElementKind.METHOD); | 44 expect(convertElementKind(engine.ElementKind.PARAMETER), |
| 142 expect(newElementKind_fromEngine(engine.ElementKind.PARAMETER), | |
| 143 ElementKind.PARAMETER); | 45 ElementKind.PARAMETER); |
| 144 expect(newElementKind_fromEngine(engine.ElementKind.SETTER), | 46 expect(convertElementKind(engine.ElementKind.SETTER), ElementKind.SETTER); |
| 145 ElementKind.SETTER); | 47 expect(convertElementKind(engine.ElementKind.TOP_LEVEL_VARIABLE), |
| 146 expect(newElementKind_fromEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), | |
| 147 ElementKind.TOP_LEVEL_VARIABLE); | 48 ElementKind.TOP_LEVEL_VARIABLE); |
| 148 expect(newElementKind_fromEngine(engine.ElementKind.TYPE_PARAMETER), | 49 expect(convertElementKind(engine.ElementKind.TYPE_PARAMETER), |
| 149 ElementKind.TYPE_PARAMETER); | 50 ElementKind.TYPE_PARAMETER); |
| 150 } | 51 } |
| 151 | 52 |
| 152 void test_string_constructor() { | 53 void test_string_constructor() { |
| 153 expect(new ElementKind(ElementKind.CLASS.name), ElementKind.CLASS); | 54 expect(new ElementKind(ElementKind.CLASS.name), ElementKind.CLASS); |
| 154 expect(new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), | 55 expect(new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), |
| 155 ElementKind.CLASS_TYPE_ALIAS); | 56 ElementKind.CLASS_TYPE_ALIAS); |
| 156 expect(new ElementKind(ElementKind.COMPILATION_UNIT.name), | 57 expect(new ElementKind(ElementKind.COMPILATION_UNIT.name), |
| 157 ElementKind.COMPILATION_UNIT); | 58 ElementKind.COMPILATION_UNIT); |
| 158 expect( | 59 expect( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 engine.Source source = addSource( | 101 engine.Source source = addSource( |
| 201 '/test.dart', | 102 '/test.dart', |
| 202 ''' | 103 ''' |
| 203 @deprecated | 104 @deprecated |
| 204 abstract class _A {} | 105 abstract class _A {} |
| 205 class B<K, V> {}'''); | 106 class B<K, V> {}'''); |
| 206 engine.CompilationUnit unit = resolveLibraryUnit(source); | 107 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 207 { | 108 { |
| 208 engine.ClassElement engineElement = findElementInUnit(unit, '_A'); | 109 engine.ClassElement engineElement = findElementInUnit(unit, '_A'); |
| 209 // create notification Element | 110 // create notification Element |
| 210 Element element = newElement_fromEngine(engineElement); | 111 Element element = convertElement(engineElement); |
| 211 expect(element.kind, ElementKind.CLASS); | 112 expect(element.kind, ElementKind.CLASS); |
| 212 expect(element.name, '_A'); | 113 expect(element.name, '_A'); |
| 213 expect(element.typeParameters, isNull); | 114 expect(element.typeParameters, isNull); |
| 214 { | 115 { |
| 215 Location location = element.location; | 116 Location location = element.location; |
| 216 expect(location.file, '/test.dart'); | 117 expect(location.file, '/test.dart'); |
| 217 expect(location.offset, 27); | 118 expect(location.offset, 27); |
| 218 expect(location.length, '_A'.length); | 119 expect(location.length, '_A'.length); |
| 219 expect(location.startLine, 2); | 120 expect(location.startLine, 2); |
| 220 expect(location.startColumn, 16); | 121 expect(location.startColumn, 16); |
| 221 } | 122 } |
| 222 expect(element.parameters, isNull); | 123 expect(element.parameters, isNull); |
| 223 expect( | 124 expect( |
| 224 element.flags, | 125 element.flags, |
| 225 Element.FLAG_ABSTRACT | | 126 Element.FLAG_ABSTRACT | |
| 226 Element.FLAG_DEPRECATED | | 127 Element.FLAG_DEPRECATED | |
| 227 Element.FLAG_PRIVATE); | 128 Element.FLAG_PRIVATE); |
| 228 } | 129 } |
| 229 { | 130 { |
| 230 engine.ClassElement engineElement = findElementInUnit(unit, 'B'); | 131 engine.ClassElement engineElement = findElementInUnit(unit, 'B'); |
| 231 // create notification Element | 132 // create notification Element |
| 232 Element element = newElement_fromEngine(engineElement); | 133 Element element = convertElement(engineElement); |
| 233 expect(element.kind, ElementKind.CLASS); | 134 expect(element.kind, ElementKind.CLASS); |
| 234 expect(element.name, 'B'); | 135 expect(element.name, 'B'); |
| 235 expect(element.typeParameters, '<K, V>'); | 136 expect(element.typeParameters, '<K, V>'); |
| 236 expect(element.flags, 0); | 137 expect(element.flags, 0); |
| 237 } | 138 } |
| 238 } | 139 } |
| 239 | 140 |
| 240 void test_fromElement_CONSTRUCTOR() { | 141 void test_fromElement_CONSTRUCTOR() { |
| 241 engine.Source source = addSource( | 142 engine.Source source = addSource( |
| 242 '/test.dart', | 143 '/test.dart', |
| 243 ''' | 144 ''' |
| 244 class A { | 145 class A { |
| 245 const A.myConstructor(int a, [String b]); | 146 const A.myConstructor(int a, [String b]); |
| 246 }'''); | 147 }'''); |
| 247 engine.CompilationUnit unit = resolveLibraryUnit(source); | 148 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 248 engine.ConstructorElement engineElement = | 149 engine.ConstructorElement engineElement = |
| 249 findElementInUnit(unit, 'myConstructor'); | 150 findElementInUnit(unit, 'myConstructor'); |
| 250 // create notification Element | 151 // create notification Element |
| 251 Element element = newElement_fromEngine(engineElement); | 152 Element element = convertElement(engineElement); |
| 252 expect(element.kind, ElementKind.CONSTRUCTOR); | 153 expect(element.kind, ElementKind.CONSTRUCTOR); |
| 253 expect(element.name, 'myConstructor'); | 154 expect(element.name, 'myConstructor'); |
| 254 expect(element.typeParameters, isNull); | 155 expect(element.typeParameters, isNull); |
| 255 { | 156 { |
| 256 Location location = element.location; | 157 Location location = element.location; |
| 257 expect(location.file, '/test.dart'); | 158 expect(location.file, '/test.dart'); |
| 258 expect(location.offset, 20); | 159 expect(location.offset, 20); |
| 259 expect(location.length, 'myConstructor'.length); | 160 expect(location.length, 'myConstructor'.length); |
| 260 expect(location.startLine, 2); | 161 expect(location.startLine, 2); |
| 261 expect(location.startColumn, 11); | 162 expect(location.startColumn, 11); |
| 262 } | 163 } |
| 263 expect(element.parameters, '(int a, [String b])'); | 164 expect(element.parameters, '(int a, [String b])'); |
| 264 expect(element.returnType, 'A'); | 165 expect(element.returnType, 'A'); |
| 265 expect(element.flags, Element.FLAG_CONST); | 166 expect(element.flags, Element.FLAG_CONST); |
| 266 } | 167 } |
| 267 | 168 |
| 268 void test_fromElement_dynamic() { | 169 void test_fromElement_dynamic() { |
| 269 var engineElement = engine.DynamicElementImpl.instance; | 170 var engineElement = engine.DynamicElementImpl.instance; |
| 270 // create notification Element | 171 // create notification Element |
| 271 Element element = newElement_fromEngine(engineElement); | 172 Element element = convertElement(engineElement); |
| 272 expect(element.kind, ElementKind.UNKNOWN); | 173 expect(element.kind, ElementKind.UNKNOWN); |
| 273 expect(element.name, 'dynamic'); | 174 expect(element.name, 'dynamic'); |
| 274 expect(element.location, isNull); | 175 expect(element.location, isNull); |
| 275 expect(element.parameters, isNull); | 176 expect(element.parameters, isNull); |
| 276 expect(element.returnType, isNull); | 177 expect(element.returnType, isNull); |
| 277 expect(element.flags, 0); | 178 expect(element.flags, 0); |
| 278 } | 179 } |
| 279 | 180 |
| 280 void test_fromElement_ENUM() { | 181 void test_fromElement_ENUM() { |
| 281 engine.Source source = addSource( | 182 engine.Source source = addSource( |
| 282 '/test.dart', | 183 '/test.dart', |
| 283 ''' | 184 ''' |
| 284 @deprecated | 185 @deprecated |
| 285 enum _E1 { one, two } | 186 enum _E1 { one, two } |
| 286 enum E2 { three, four }'''); | 187 enum E2 { three, four }'''); |
| 287 engine.CompilationUnit unit = resolveLibraryUnit(source); | 188 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 288 { | 189 { |
| 289 engine.ClassElement engineElement = findElementInUnit(unit, '_E1'); | 190 engine.ClassElement engineElement = findElementInUnit(unit, '_E1'); |
| 290 expect(engineElement.isDeprecated, isTrue); | 191 expect(engineElement.isDeprecated, isTrue); |
| 291 // create notification Element | 192 // create notification Element |
| 292 Element element = newElement_fromEngine(engineElement); | 193 Element element = convertElement(engineElement); |
| 293 expect(element.kind, ElementKind.ENUM); | 194 expect(element.kind, ElementKind.ENUM); |
| 294 expect(element.name, '_E1'); | 195 expect(element.name, '_E1'); |
| 295 expect(element.typeParameters, isNull); | 196 expect(element.typeParameters, isNull); |
| 296 { | 197 { |
| 297 Location location = element.location; | 198 Location location = element.location; |
| 298 expect(location.file, '/test.dart'); | 199 expect(location.file, '/test.dart'); |
| 299 expect(location.offset, 17); | 200 expect(location.offset, 17); |
| 300 expect(location.length, '_E1'.length); | 201 expect(location.length, '_E1'.length); |
| 301 expect(location.startLine, 2); | 202 expect(location.startLine, 2); |
| 302 expect(location.startColumn, 6); | 203 expect(location.startColumn, 6); |
| 303 } | 204 } |
| 304 expect(element.parameters, isNull); | 205 expect(element.parameters, isNull); |
| 305 expect( | 206 expect( |
| 306 element.flags, | 207 element.flags, |
| 307 (engineElement.isDeprecated ? Element.FLAG_DEPRECATED : 0) | | 208 (engineElement.isDeprecated ? Element.FLAG_DEPRECATED : 0) | |
| 308 Element.FLAG_PRIVATE); | 209 Element.FLAG_PRIVATE); |
| 309 } | 210 } |
| 310 { | 211 { |
| 311 engine.ClassElement engineElement = findElementInUnit(unit, 'E2'); | 212 engine.ClassElement engineElement = findElementInUnit(unit, 'E2'); |
| 312 // create notification Element | 213 // create notification Element |
| 313 Element element = newElement_fromEngine(engineElement); | 214 Element element = convertElement(engineElement); |
| 314 expect(element.kind, ElementKind.ENUM); | 215 expect(element.kind, ElementKind.ENUM); |
| 315 expect(element.name, 'E2'); | 216 expect(element.name, 'E2'); |
| 316 expect(element.typeParameters, isNull); | 217 expect(element.typeParameters, isNull); |
| 317 expect(element.flags, 0); | 218 expect(element.flags, 0); |
| 318 } | 219 } |
| 319 } | 220 } |
| 320 | 221 |
| 321 void test_fromElement_ENUM_CONSTANT() { | 222 void test_fromElement_ENUM_CONSTANT() { |
| 322 engine.Source source = addSource( | 223 engine.Source source = addSource( |
| 323 '/test.dart', | 224 '/test.dart', |
| 324 ''' | 225 ''' |
| 325 @deprecated | 226 @deprecated |
| 326 enum _E1 { one, two } | 227 enum _E1 { one, two } |
| 327 enum E2 { three, four }'''); | 228 enum E2 { three, four }'''); |
| 328 engine.CompilationUnit unit = resolveLibraryUnit(source); | 229 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 329 { | 230 { |
| 330 engine.FieldElement engineElement = findElementInUnit(unit, 'one'); | 231 engine.FieldElement engineElement = findElementInUnit(unit, 'one'); |
| 331 // create notification Element | 232 // create notification Element |
| 332 Element element = newElement_fromEngine(engineElement); | 233 Element element = convertElement(engineElement); |
| 333 expect(element.kind, ElementKind.ENUM_CONSTANT); | 234 expect(element.kind, ElementKind.ENUM_CONSTANT); |
| 334 expect(element.name, 'one'); | 235 expect(element.name, 'one'); |
| 335 { | 236 { |
| 336 Location location = element.location; | 237 Location location = element.location; |
| 337 expect(location.file, '/test.dart'); | 238 expect(location.file, '/test.dart'); |
| 338 expect(location.offset, 23); | 239 expect(location.offset, 23); |
| 339 expect(location.length, 'one'.length); | 240 expect(location.length, 'one'.length); |
| 340 expect(location.startLine, 2); | 241 expect(location.startLine, 2); |
| 341 expect(location.startColumn, 12); | 242 expect(location.startColumn, 12); |
| 342 } | 243 } |
| 343 expect(element.parameters, isNull); | 244 expect(element.parameters, isNull); |
| 344 expect(element.returnType, '_E1'); | 245 expect(element.returnType, '_E1'); |
| 345 // TODO(danrubel) determine why enum constant is not marked as deprecated | 246 // TODO(danrubel) determine why enum constant is not marked as deprecated |
| 346 //engine.ClassElement classElement = engineElement.enclosingElement; | 247 //engine.ClassElement classElement = engineElement.enclosingElement; |
| 347 //expect(classElement.isDeprecated, isTrue); | 248 //expect(classElement.isDeprecated, isTrue); |
| 348 expect( | 249 expect( |
| 349 element.flags, | 250 element.flags, |
| 350 // Element.FLAG_DEPRECATED | | 251 // Element.FLAG_DEPRECATED | |
| 351 Element.FLAG_CONST | Element.FLAG_STATIC); | 252 Element.FLAG_CONST | Element.FLAG_STATIC); |
| 352 } | 253 } |
| 353 { | 254 { |
| 354 engine.FieldElement engineElement = findElementInUnit(unit, 'three'); | 255 engine.FieldElement engineElement = findElementInUnit(unit, 'three'); |
| 355 // create notification Element | 256 // create notification Element |
| 356 Element element = newElement_fromEngine(engineElement); | 257 Element element = convertElement(engineElement); |
| 357 expect(element.kind, ElementKind.ENUM_CONSTANT); | 258 expect(element.kind, ElementKind.ENUM_CONSTANT); |
| 358 expect(element.name, 'three'); | 259 expect(element.name, 'three'); |
| 359 { | 260 { |
| 360 Location location = element.location; | 261 Location location = element.location; |
| 361 expect(location.file, '/test.dart'); | 262 expect(location.file, '/test.dart'); |
| 362 expect(location.offset, 44); | 263 expect(location.offset, 44); |
| 363 expect(location.length, 'three'.length); | 264 expect(location.length, 'three'.length); |
| 364 expect(location.startLine, 3); | 265 expect(location.startLine, 3); |
| 365 expect(location.startColumn, 11); | 266 expect(location.startColumn, 11); |
| 366 } | 267 } |
| 367 expect(element.parameters, isNull); | 268 expect(element.parameters, isNull); |
| 368 expect(element.returnType, 'E2'); | 269 expect(element.returnType, 'E2'); |
| 369 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 270 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 370 } | 271 } |
| 371 { | 272 { |
| 372 engine.FieldElement engineElement = findElementInUnit(unit, 'index'); | 273 engine.FieldElement engineElement = findElementInUnit(unit, 'index'); |
| 373 // create notification Element | 274 // create notification Element |
| 374 Element element = newElement_fromEngine(engineElement); | 275 Element element = convertElement(engineElement); |
| 375 expect(element.kind, ElementKind.FIELD); | 276 expect(element.kind, ElementKind.FIELD); |
| 376 expect(element.name, 'index'); | 277 expect(element.name, 'index'); |
| 377 { | 278 { |
| 378 Location location = element.location; | 279 Location location = element.location; |
| 379 expect(location.file, '/test.dart'); | 280 expect(location.file, '/test.dart'); |
| 380 expect(location.offset, -1); | 281 expect(location.offset, -1); |
| 381 expect(location.length, 'index'.length); | 282 expect(location.length, 'index'.length); |
| 382 expect(location.startLine, 1); | 283 expect(location.startLine, 1); |
| 383 expect(location.startColumn, 0); | 284 expect(location.startColumn, 0); |
| 384 } | 285 } |
| 385 expect(element.parameters, isNull); | 286 expect(element.parameters, isNull); |
| 386 expect(element.returnType, 'int'); | 287 expect(element.returnType, 'int'); |
| 387 expect(element.flags, Element.FLAG_FINAL); | 288 expect(element.flags, Element.FLAG_FINAL); |
| 388 } | 289 } |
| 389 { | 290 { |
| 390 engine.FieldElement engineElement = findElementInUnit(unit, 'values'); | 291 engine.FieldElement engineElement = findElementInUnit(unit, 'values'); |
| 391 // create notification Element | 292 // create notification Element |
| 392 Element element = newElement_fromEngine(engineElement); | 293 Element element = convertElement(engineElement); |
| 393 expect(element.kind, ElementKind.FIELD); | 294 expect(element.kind, ElementKind.FIELD); |
| 394 expect(element.name, 'values'); | 295 expect(element.name, 'values'); |
| 395 { | 296 { |
| 396 Location location = element.location; | 297 Location location = element.location; |
| 397 expect(location.file, '/test.dart'); | 298 expect(location.file, '/test.dart'); |
| 398 expect(location.offset, -1); | 299 expect(location.offset, -1); |
| 399 expect(location.length, 'values'.length); | 300 expect(location.length, 'values'.length); |
| 400 expect(location.startLine, 1); | 301 expect(location.startLine, 1); |
| 401 expect(location.startColumn, 0); | 302 expect(location.startColumn, 0); |
| 402 } | 303 } |
| 403 expect(element.parameters, isNull); | 304 expect(element.parameters, isNull); |
| 404 expect(element.returnType, 'List<E2>'); | 305 expect(element.returnType, 'List<E2>'); |
| 405 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 306 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 406 } | 307 } |
| 407 } | 308 } |
| 408 | 309 |
| 409 void test_fromElement_FIELD() { | 310 void test_fromElement_FIELD() { |
| 410 engine.Source source = addSource( | 311 engine.Source source = addSource( |
| 411 '/test.dart', | 312 '/test.dart', |
| 412 ''' | 313 ''' |
| 413 class A { | 314 class A { |
| 414 static const myField = 42; | 315 static const myField = 42; |
| 415 }'''); | 316 }'''); |
| 416 engine.CompilationUnit unit = resolveLibraryUnit(source); | 317 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 417 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | 318 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); |
| 418 // create notification Element | 319 // create notification Element |
| 419 Element element = newElement_fromEngine(engineElement); | 320 Element element = convertElement(engineElement); |
| 420 expect(element.kind, ElementKind.FIELD); | 321 expect(element.kind, ElementKind.FIELD); |
| 421 expect(element.name, 'myField'); | 322 expect(element.name, 'myField'); |
| 422 { | 323 { |
| 423 Location location = element.location; | 324 Location location = element.location; |
| 424 expect(location.file, '/test.dart'); | 325 expect(location.file, '/test.dart'); |
| 425 expect(location.offset, 25); | 326 expect(location.offset, 25); |
| 426 expect(location.length, 'myField'.length); | 327 expect(location.length, 'myField'.length); |
| 427 expect(location.startLine, 2); | 328 expect(location.startLine, 2); |
| 428 expect(location.startColumn, 16); | 329 expect(location.startColumn, 16); |
| 429 } | 330 } |
| 430 expect(element.parameters, isNull); | 331 expect(element.parameters, isNull); |
| 431 expect(element.returnType, 'dynamic'); | 332 expect(element.returnType, 'dynamic'); |
| 432 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 333 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 433 } | 334 } |
| 434 | 335 |
| 435 void test_fromElement_FUNCTION_TYPE_ALIAS() { | 336 void test_fromElement_FUNCTION_TYPE_ALIAS() { |
| 436 engine.Source source = addSource( | 337 engine.Source source = addSource( |
| 437 '/test.dart', | 338 '/test.dart', |
| 438 ''' | 339 ''' |
| 439 typedef int F<T>(String x); | 340 typedef int F<T>(String x); |
| 440 '''); | 341 '''); |
| 441 engine.CompilationUnit unit = resolveLibraryUnit(source); | 342 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 442 engine.FunctionTypeAliasElement engineElement = | 343 engine.FunctionTypeAliasElement engineElement = |
| 443 findElementInUnit(unit, 'F'); | 344 findElementInUnit(unit, 'F'); |
| 444 // create notification Element | 345 // create notification Element |
| 445 Element element = newElement_fromEngine(engineElement); | 346 Element element = convertElement(engineElement); |
| 446 expect(element.kind, ElementKind.FUNCTION_TYPE_ALIAS); | 347 expect(element.kind, ElementKind.FUNCTION_TYPE_ALIAS); |
| 447 expect(element.name, 'F'); | 348 expect(element.name, 'F'); |
| 448 expect(element.typeParameters, '<T>'); | 349 expect(element.typeParameters, '<T>'); |
| 449 { | 350 { |
| 450 Location location = element.location; | 351 Location location = element.location; |
| 451 expect(location.file, '/test.dart'); | 352 expect(location.file, '/test.dart'); |
| 452 expect(location.offset, 12); | 353 expect(location.offset, 12); |
| 453 expect(location.length, 'F'.length); | 354 expect(location.length, 'F'.length); |
| 454 expect(location.startLine, 1); | 355 expect(location.startLine, 1); |
| 455 expect(location.startColumn, 13); | 356 expect(location.startColumn, 13); |
| 456 } | 357 } |
| 457 expect(element.parameters, '(String x)'); | 358 expect(element.parameters, '(String x)'); |
| 458 expect(element.returnType, 'int'); | 359 expect(element.returnType, 'int'); |
| 459 expect(element.flags, 0); | 360 expect(element.flags, 0); |
| 460 } | 361 } |
| 461 | 362 |
| 462 void test_fromElement_GETTER() { | 363 void test_fromElement_GETTER() { |
| 463 engine.Source source = addSource( | 364 engine.Source source = addSource( |
| 464 '/test.dart', | 365 '/test.dart', |
| 465 ''' | 366 ''' |
| 466 class A { | 367 class A { |
| 467 String get myGetter => 42; | 368 String get myGetter => 42; |
| 468 }'''); | 369 }'''); |
| 469 engine.CompilationUnit unit = resolveLibraryUnit(source); | 370 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 470 engine.PropertyAccessorElement engineElement = | 371 engine.PropertyAccessorElement engineElement = |
| 471 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); | 372 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); |
| 472 // create notification Element | 373 // create notification Element |
| 473 Element element = newElement_fromEngine(engineElement); | 374 Element element = convertElement(engineElement); |
| 474 expect(element.kind, ElementKind.GETTER); | 375 expect(element.kind, ElementKind.GETTER); |
| 475 expect(element.name, 'myGetter'); | 376 expect(element.name, 'myGetter'); |
| 476 { | 377 { |
| 477 Location location = element.location; | 378 Location location = element.location; |
| 478 expect(location.file, '/test.dart'); | 379 expect(location.file, '/test.dart'); |
| 479 expect(location.offset, 23); | 380 expect(location.offset, 23); |
| 480 expect(location.length, 'myGetter'.length); | 381 expect(location.length, 'myGetter'.length); |
| 481 expect(location.startLine, 2); | 382 expect(location.startLine, 2); |
| 482 expect(location.startColumn, 14); | 383 expect(location.startColumn, 14); |
| 483 } | 384 } |
| 484 expect(element.parameters, isNull); | 385 expect(element.parameters, isNull); |
| 485 expect(element.returnType, 'String'); | 386 expect(element.returnType, 'String'); |
| 486 expect(element.flags, 0); | 387 expect(element.flags, 0); |
| 487 } | 388 } |
| 488 | 389 |
| 489 void test_fromElement_LABEL() { | 390 void test_fromElement_LABEL() { |
| 490 engine.Source source = addSource( | 391 engine.Source source = addSource( |
| 491 '/test.dart', | 392 '/test.dart', |
| 492 ''' | 393 ''' |
| 493 main() { | 394 main() { |
| 494 myLabel: | 395 myLabel: |
| 495 while (true) { | 396 while (true) { |
| 496 break myLabel; | 397 break myLabel; |
| 497 } | 398 } |
| 498 }'''); | 399 }'''); |
| 499 engine.CompilationUnit unit = resolveLibraryUnit(source); | 400 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 500 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); | 401 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); |
| 501 // create notification Element | 402 // create notification Element |
| 502 Element element = newElement_fromEngine(engineElement); | 403 Element element = convertElement(engineElement); |
| 503 expect(element.kind, ElementKind.LABEL); | 404 expect(element.kind, ElementKind.LABEL); |
| 504 expect(element.name, 'myLabel'); | 405 expect(element.name, 'myLabel'); |
| 505 { | 406 { |
| 506 Location location = element.location; | 407 Location location = element.location; |
| 507 expect(location.file, '/test.dart'); | 408 expect(location.file, '/test.dart'); |
| 508 expect(location.offset, 9); | 409 expect(location.offset, 9); |
| 509 expect(location.length, 'myLabel'.length); | 410 expect(location.length, 'myLabel'.length); |
| 510 expect(location.startLine, 2); | 411 expect(location.startLine, 2); |
| 511 expect(location.startColumn, 1); | 412 expect(location.startColumn, 1); |
| 512 } | 413 } |
| 513 expect(element.parameters, isNull); | 414 expect(element.parameters, isNull); |
| 514 expect(element.returnType, isNull); | 415 expect(element.returnType, isNull); |
| 515 expect(element.flags, 0); | 416 expect(element.flags, 0); |
| 516 } | 417 } |
| 517 | 418 |
| 518 void test_fromElement_METHOD() { | 419 void test_fromElement_METHOD() { |
| 519 engine.Source source = addSource( | 420 engine.Source source = addSource( |
| 520 '/test.dart', | 421 '/test.dart', |
| 521 ''' | 422 ''' |
| 522 class A { | 423 class A { |
| 523 static List<String> myMethod(int a, {String b, int c}) { | 424 static List<String> myMethod(int a, {String b, int c}) { |
| 524 return null; | 425 return null; |
| 525 } | 426 } |
| 526 }'''); | 427 }'''); |
| 527 engine.CompilationUnit unit = resolveLibraryUnit(source); | 428 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 528 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | 429 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); |
| 529 // create notification Element | 430 // create notification Element |
| 530 Element element = newElement_fromEngine(engineElement); | 431 Element element = convertElement(engineElement); |
| 531 expect(element.kind, ElementKind.METHOD); | 432 expect(element.kind, ElementKind.METHOD); |
| 532 expect(element.name, 'myMethod'); | 433 expect(element.name, 'myMethod'); |
| 533 { | 434 { |
| 534 Location location = element.location; | 435 Location location = element.location; |
| 535 expect(location.file, '/test.dart'); | 436 expect(location.file, '/test.dart'); |
| 536 expect(location.offset, 32); | 437 expect(location.offset, 32); |
| 537 expect(location.length, 'myGetter'.length); | 438 expect(location.length, 'myGetter'.length); |
| 538 expect(location.startLine, 2); | 439 expect(location.startLine, 2); |
| 539 expect(location.startColumn, 23); | 440 expect(location.startColumn, 23); |
| 540 } | 441 } |
| 541 expect(element.parameters, '(int a, {String b, int c})'); | 442 expect(element.parameters, '(int a, {String b, int c})'); |
| 542 expect(element.returnType, 'List<String>'); | 443 expect(element.returnType, 'List<String>'); |
| 543 expect(element.flags, Element.FLAG_STATIC); | 444 expect(element.flags, Element.FLAG_STATIC); |
| 544 } | 445 } |
| 545 | 446 |
| 546 void test_fromElement_SETTER() { | 447 void test_fromElement_SETTER() { |
| 547 engine.Source source = addSource( | 448 engine.Source source = addSource( |
| 548 '/test.dart', | 449 '/test.dart', |
| 549 ''' | 450 ''' |
| 550 class A { | 451 class A { |
| 551 set mySetter(String x) {} | 452 set mySetter(String x) {} |
| 552 }'''); | 453 }'''); |
| 553 engine.CompilationUnit unit = resolveLibraryUnit(source); | 454 engine.CompilationUnit unit = resolveLibraryUnit(source); |
| 554 engine.FieldElement engineFieldElement = | 455 engine.FieldElement engineFieldElement = |
| 555 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); | 456 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); |
| 556 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; | 457 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; |
| 557 // create notification Element | 458 // create notification Element |
| 558 Element element = newElement_fromEngine(engineElement); | 459 Element element = convertElement(engineElement); |
| 559 expect(element.kind, ElementKind.SETTER); | 460 expect(element.kind, ElementKind.SETTER); |
| 560 expect(element.name, 'mySetter'); | 461 expect(element.name, 'mySetter'); |
| 561 { | 462 { |
| 562 Location location = element.location; | 463 Location location = element.location; |
| 563 expect(location.file, '/test.dart'); | 464 expect(location.file, '/test.dart'); |
| 564 expect(location.offset, 16); | 465 expect(location.offset, 16); |
| 565 expect(location.length, 'mySetter'.length); | 466 expect(location.length, 'mySetter'.length); |
| 566 expect(location.startLine, 2); | 467 expect(location.startLine, 2); |
| 567 expect(location.startColumn, 7); | 468 expect(location.startColumn, 7); |
| 568 } | 469 } |
| 569 expect(element.parameters, '(String x)'); | 470 expect(element.parameters, '(String x)'); |
| 570 expect(element.returnType, isNull); | 471 expect(element.returnType, isNull); |
| 571 expect(element.flags, 0); | 472 expect(element.flags, 0); |
| 572 } | 473 } |
| 573 } | 474 } |
| 574 | |
| 575 @reflectiveTest | |
| 576 class EnumTest { | |
| 577 void test_AnalysisErrorSeverity() { | |
| 578 new EnumTester<engine.ErrorSeverity, AnalysisErrorSeverity>().run( | |
| 579 (engine.ErrorSeverity engineErrorSeverity) => | |
| 580 new AnalysisErrorSeverity(engineErrorSeverity.name), | |
| 581 exceptions: {engine.ErrorSeverity.NONE: null}); | |
| 582 } | |
| 583 | |
| 584 void test_AnalysisErrorType() { | |
| 585 new EnumTester<engine.ErrorType, AnalysisErrorType>().run( | |
| 586 (engine.ErrorType engineErrorType) => | |
| 587 new AnalysisErrorType(engineErrorType.name)); | |
| 588 } | |
| 589 | |
| 590 void test_ElementKind() { | |
| 591 new EnumTester<engine.ElementKind, ElementKind>() | |
| 592 .run(newElementKind_fromEngine, exceptions: { | |
| 593 // TODO(paulberry): do any of the exceptions below constitute bugs? | |
| 594 engine.ElementKind.DYNAMIC: ElementKind.UNKNOWN, | |
| 595 engine.ElementKind.EMBEDDED_HTML_SCRIPT: ElementKind.UNKNOWN, | |
| 596 engine.ElementKind.ERROR: ElementKind.UNKNOWN, | |
| 597 engine.ElementKind.EXPORT: ElementKind.UNKNOWN, | |
| 598 engine.ElementKind.EXTERNAL_HTML_SCRIPT: ElementKind.UNKNOWN, | |
| 599 engine.ElementKind.HTML: ElementKind.UNKNOWN, | |
| 600 engine.ElementKind.IMPORT: ElementKind.UNKNOWN, | |
| 601 engine.ElementKind.NAME: ElementKind.UNKNOWN, | |
| 602 engine.ElementKind.UNIVERSE: ElementKind.UNKNOWN | |
| 603 }); | |
| 604 } | |
| 605 | |
| 606 void test_SearchResultKind() { | |
| 607 // TODO(paulberry): why does the MatchKind class exist at all? Can't we | |
| 608 // use SearchResultKind inside the analysis server? | |
| 609 new EnumTester<MatchKind, SearchResultKind>() | |
| 610 .run(newSearchResultKind_fromEngine); | |
| 611 } | |
| 612 } | |
| 613 | |
| 614 /** | |
| 615 * Helper class for testing the correspondence between an analysis engine enum | |
| 616 * and an analysis server API enum. | |
| 617 */ | |
| 618 class EnumTester<EngineEnum, ApiEnum extends Enum> { | |
| 619 /** | |
| 620 * Test that the function [convert] properly converts all possible values of | |
| 621 * [EngineEnum] to an [ApiEnum] with the same name, with the exceptions noted | |
| 622 * in [exceptions]. For each key in [exceptions], if the corresponding value | |
| 623 * is null, then we check that converting the given key results in an error. | |
| 624 * If the corresponding value is an [ApiEnum], then we check that converting | |
| 625 * the given key results in the given value. | |
| 626 */ | |
| 627 void run(ApiEnum convert(EngineEnum value), | |
| 628 {Map<EngineEnum, ApiEnum> exceptions: const {}}) { | |
| 629 ClassMirror engineClass = reflectClass(EngineEnum); | |
| 630 engineClass.staticMembers.forEach((Symbol symbol, MethodMirror method) { | |
| 631 if (symbol == #values) { | |
| 632 return; | |
| 633 } | |
| 634 if (!method.isGetter) { | |
| 635 return; | |
| 636 } | |
| 637 String enumName = MirrorSystem.getName(symbol); | |
| 638 EngineEnum engineValue = engineClass.getField(symbol).reflectee; | |
| 639 expect(engineValue, new isInstanceOf<EngineEnum>()); | |
| 640 if (exceptions.containsKey(engineValue)) { | |
| 641 ApiEnum expectedResult = exceptions[engineValue]; | |
| 642 if (expectedResult == null) { | |
| 643 expect(() { | |
| 644 convert(engineValue); | |
| 645 }, throws); | |
| 646 } else { | |
| 647 ApiEnum apiValue = convert(engineValue); | |
| 648 expect(apiValue, equals(expectedResult)); | |
| 649 } | |
| 650 } else { | |
| 651 ApiEnum apiValue = convert(engineValue); | |
| 652 expect(apiValue.name, equals(enumName)); | |
| 653 } | |
| 654 }); | |
| 655 } | |
| 656 } | |
| OLD | NEW |