| 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/protocol.dart'; | 7 import 'package:analysis_server/src/protocol_server.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart' as engine; | 9 import 'package:analyzer/src/generated/element.dart' as engine; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; | 11 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 | 13 |
| 14 import '../abstract_context.dart'; | 14 import '../abstract_context.dart'; |
| 15 import '../reflective_tests.dart'; | 15 import '../reflective_tests.dart'; |
| 16 | 16 |
| 17 | 17 |
| 18 | 18 |
| 19 main() { | 19 main() { |
| 20 groupSep = ' | '; | 20 groupSep = ' | '; |
| 21 runReflectiveTests(ElementTest); | 21 runReflectiveTests(ElementTest); |
| 22 runReflectiveTests(ElementKindTest); | 22 runReflectiveTests(ElementKindTest); |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 class ElementKindTest { | 26 class ElementKindTest { |
| 27 void test_fromEngine() { | 27 void test_fromEngine() { |
| 28 expect( | 28 expect( |
| 29 new ElementKind.fromEngine(engine.ElementKind.CLASS), | 29 newElementKind_fromEngine(engine.ElementKind.CLASS), |
| 30 ElementKind.CLASS); | 30 ElementKind.CLASS); |
| 31 expect( | 31 expect( |
| 32 new ElementKind.fromEngine(engine.ElementKind.COMPILATION_UNIT), | 32 newElementKind_fromEngine(engine.ElementKind.COMPILATION_UNIT), |
| 33 ElementKind.COMPILATION_UNIT); | 33 ElementKind.COMPILATION_UNIT); |
| 34 expect( | 34 expect( |
| 35 new ElementKind.fromEngine(engine.ElementKind.CONSTRUCTOR), | 35 newElementKind_fromEngine(engine.ElementKind.CONSTRUCTOR), |
| 36 ElementKind.CONSTRUCTOR); | 36 ElementKind.CONSTRUCTOR); |
| 37 expect( | 37 expect( |
| 38 new ElementKind.fromEngine(engine.ElementKind.FIELD), | 38 newElementKind_fromEngine(engine.ElementKind.FIELD), |
| 39 ElementKind.FIELD); | 39 ElementKind.FIELD); |
| 40 expect( | 40 expect( |
| 41 new ElementKind.fromEngine(engine.ElementKind.FUNCTION), | 41 newElementKind_fromEngine(engine.ElementKind.FUNCTION), |
| 42 ElementKind.FUNCTION); | 42 ElementKind.FUNCTION); |
| 43 expect( | 43 expect( |
| 44 new ElementKind.fromEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), | 44 newElementKind_fromEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), |
| 45 ElementKind.FUNCTION_TYPE_ALIAS); | 45 ElementKind.FUNCTION_TYPE_ALIAS); |
| 46 expect( | 46 expect( |
| 47 new ElementKind.fromEngine(engine.ElementKind.GETTER), | 47 newElementKind_fromEngine(engine.ElementKind.GETTER), |
| 48 ElementKind.GETTER); | 48 ElementKind.GETTER); |
| 49 expect( | 49 expect( |
| 50 new ElementKind.fromEngine(engine.ElementKind.LABEL), | 50 newElementKind_fromEngine(engine.ElementKind.LABEL), |
| 51 ElementKind.LABEL); | 51 ElementKind.LABEL); |
| 52 expect( | 52 expect( |
| 53 new ElementKind.fromEngine(engine.ElementKind.LIBRARY), | 53 newElementKind_fromEngine(engine.ElementKind.LIBRARY), |
| 54 ElementKind.LIBRARY); | 54 ElementKind.LIBRARY); |
| 55 expect( | 55 expect( |
| 56 new ElementKind.fromEngine(engine.ElementKind.LOCAL_VARIABLE), | 56 newElementKind_fromEngine(engine.ElementKind.LOCAL_VARIABLE), |
| 57 ElementKind.LOCAL_VARIABLE); | 57 ElementKind.LOCAL_VARIABLE); |
| 58 expect( | 58 expect( |
| 59 new ElementKind.fromEngine(engine.ElementKind.METHOD), | 59 newElementKind_fromEngine(engine.ElementKind.METHOD), |
| 60 ElementKind.METHOD); | 60 ElementKind.METHOD); |
| 61 expect( | 61 expect( |
| 62 new ElementKind.fromEngine(engine.ElementKind.PARAMETER), | 62 newElementKind_fromEngine(engine.ElementKind.PARAMETER), |
| 63 ElementKind.PARAMETER); | 63 ElementKind.PARAMETER); |
| 64 expect( | 64 expect( |
| 65 new ElementKind.fromEngine(engine.ElementKind.SETTER), | 65 newElementKind_fromEngine(engine.ElementKind.SETTER), |
| 66 ElementKind.SETTER); | 66 ElementKind.SETTER); |
| 67 expect( | 67 expect( |
| 68 new ElementKind.fromEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), | 68 newElementKind_fromEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), |
| 69 ElementKind.TOP_LEVEL_VARIABLE); | 69 ElementKind.TOP_LEVEL_VARIABLE); |
| 70 expect( | 70 expect( |
| 71 new ElementKind.fromEngine(engine.ElementKind.TYPE_PARAMETER), | 71 newElementKind_fromEngine(engine.ElementKind.TYPE_PARAMETER), |
| 72 ElementKind.TYPE_PARAMETER); | 72 ElementKind.TYPE_PARAMETER); |
| 73 expect( | 73 expect( |
| 74 new ElementKind.fromEngine(engine.ElementKind.ANGULAR_COMPONENT), | 74 newElementKind_fromEngine(engine.ElementKind.ANGULAR_COMPONENT), |
| 75 ElementKind.UNKNOWN); | 75 ElementKind.UNKNOWN); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void test_string_constructor() { | 78 void test_string_constructor() { |
| 79 expect(new ElementKind(ElementKind.CLASS.name), ElementKind.CLASS); | 79 expect(new ElementKind(ElementKind.CLASS.name), ElementKind.CLASS); |
| 80 expect( | 80 expect( |
| 81 new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), | 81 new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), |
| 82 ElementKind.CLASS_TYPE_ALIAS); | 82 ElementKind.CLASS_TYPE_ALIAS); |
| 83 expect( | 83 expect( |
| 84 new ElementKind(ElementKind.COMPILATION_UNIT.name), | 84 new ElementKind(ElementKind.COMPILATION_UNIT.name), |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 return findChildElement(unit.element, name, kind); | 133 return findChildElement(unit.element, name, kind); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void test_fromElement_CLASS() { | 136 void test_fromElement_CLASS() { |
| 137 Source source = addSource('/test.dart', ''' | 137 Source source = addSource('/test.dart', ''' |
| 138 @deprecated | 138 @deprecated |
| 139 abstract class _MyClass {}'''); | 139 abstract class _MyClass {}'''); |
| 140 CompilationUnit unit = resolveLibraryUnit(source); | 140 CompilationUnit unit = resolveLibraryUnit(source); |
| 141 engine.ClassElement engineElement = findElementInUnit(unit, '_MyClass'); | 141 engine.ClassElement engineElement = findElementInUnit(unit, '_MyClass'); |
| 142 // create notification Element | 142 // create notification Element |
| 143 Element element = new Element.fromEngine(engineElement); | 143 Element element = newElement_fromEngine(engineElement); |
| 144 expect(element.kind, ElementKind.CLASS); | 144 expect(element.kind, ElementKind.CLASS); |
| 145 expect(element.name, '_MyClass'); | 145 expect(element.name, '_MyClass'); |
| 146 { | 146 { |
| 147 Location location = element.location; | 147 Location location = element.location; |
| 148 expect(location.file, '/test.dart'); | 148 expect(location.file, '/test.dart'); |
| 149 expect(location.offset, 27); | 149 expect(location.offset, 27); |
| 150 expect(location.length, '_MyClass'.length); | 150 expect(location.length, '_MyClass'.length); |
| 151 expect(location.startLine, 2); | 151 expect(location.startLine, 2); |
| 152 expect(location.startColumn, 16); | 152 expect(location.startColumn, 16); |
| 153 } | 153 } |
| 154 expect( | 154 expect( |
| 155 element.flags, | 155 element.flags, |
| 156 Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE); | 156 Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void test_fromElement_CONSTRUCTOR() { | 159 void test_fromElement_CONSTRUCTOR() { |
| 160 Source source = addSource('/test.dart', ''' | 160 Source source = addSource('/test.dart', ''' |
| 161 class A { | 161 class A { |
| 162 const A.myConstructor(int a, [String b]); | 162 const A.myConstructor(int a, [String b]); |
| 163 }'''); | 163 }'''); |
| 164 CompilationUnit unit = resolveLibraryUnit(source); | 164 CompilationUnit unit = resolveLibraryUnit(source); |
| 165 engine.ConstructorElement engineElement = | 165 engine.ConstructorElement engineElement = |
| 166 findElementInUnit(unit, 'myConstructor'); | 166 findElementInUnit(unit, 'myConstructor'); |
| 167 // create notification Element | 167 // create notification Element |
| 168 Element element = new Element.fromEngine(engineElement); | 168 Element element = newElement_fromEngine(engineElement); |
| 169 expect(element.kind, ElementKind.CONSTRUCTOR); | 169 expect(element.kind, ElementKind.CONSTRUCTOR); |
| 170 expect(element.name, 'myConstructor'); | 170 expect(element.name, 'myConstructor'); |
| 171 { | 171 { |
| 172 Location location = element.location; | 172 Location location = element.location; |
| 173 expect(location.file, '/test.dart'); | 173 expect(location.file, '/test.dart'); |
| 174 expect(location.offset, 20); | 174 expect(location.offset, 20); |
| 175 expect(location.length, 'myConstructor'.length); | 175 expect(location.length, 'myConstructor'.length); |
| 176 expect(location.startLine, 2); | 176 expect(location.startLine, 2); |
| 177 expect(location.startColumn, 11); | 177 expect(location.startColumn, 11); |
| 178 } | 178 } |
| 179 expect(element.parameters, '(int a, [String b])'); | 179 expect(element.parameters, '(int a, [String b])'); |
| 180 expect(element.returnType, 'A'); | 180 expect(element.returnType, 'A'); |
| 181 expect(element.flags, Element.FLAG_CONST); | 181 expect(element.flags, Element.FLAG_CONST); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void test_fromElement_FIELD() { | 184 void test_fromElement_FIELD() { |
| 185 Source source = addSource('/test.dart', ''' | 185 Source source = addSource('/test.dart', ''' |
| 186 class A { | 186 class A { |
| 187 static const myField = 42; | 187 static const myField = 42; |
| 188 }'''); | 188 }'''); |
| 189 CompilationUnit unit = resolveLibraryUnit(source); | 189 CompilationUnit unit = resolveLibraryUnit(source); |
| 190 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | 190 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); |
| 191 // create notification Element | 191 // create notification Element |
| 192 Element element = new Element.fromEngine(engineElement); | 192 Element element = newElement_fromEngine(engineElement); |
| 193 expect(element.kind, ElementKind.FIELD); | 193 expect(element.kind, ElementKind.FIELD); |
| 194 expect(element.name, 'myField'); | 194 expect(element.name, 'myField'); |
| 195 { | 195 { |
| 196 Location location = element.location; | 196 Location location = element.location; |
| 197 expect(location.file, '/test.dart'); | 197 expect(location.file, '/test.dart'); |
| 198 expect(location.offset, 25); | 198 expect(location.offset, 25); |
| 199 expect(location.length, 'myField'.length); | 199 expect(location.length, 'myField'.length); |
| 200 expect(location.startLine, 2); | 200 expect(location.startLine, 2); |
| 201 expect(location.startColumn, 16); | 201 expect(location.startColumn, 16); |
| 202 } | 202 } |
| 203 expect(element.parameters, isNull); | 203 expect(element.parameters, isNull); |
| 204 expect(element.returnType, isNull); | 204 expect(element.returnType, isNull); |
| 205 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 205 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 206 } | 206 } |
| 207 | 207 |
| 208 void test_fromElement_GETTER() { | 208 void test_fromElement_GETTER() { |
| 209 Source source = addSource('/test.dart', ''' | 209 Source source = addSource('/test.dart', ''' |
| 210 class A { | 210 class A { |
| 211 String myGetter => 42; | 211 String myGetter => 42; |
| 212 }'''); | 212 }'''); |
| 213 CompilationUnit unit = resolveLibraryUnit(source); | 213 CompilationUnit unit = resolveLibraryUnit(source); |
| 214 engine.PropertyAccessorElement engineElement = | 214 engine.PropertyAccessorElement engineElement = |
| 215 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); | 215 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); |
| 216 // create notification Element | 216 // create notification Element |
| 217 Element element = new Element.fromEngine(engineElement); | 217 Element element = newElement_fromEngine(engineElement); |
| 218 expect(element.kind, ElementKind.GETTER); | 218 expect(element.kind, ElementKind.GETTER); |
| 219 expect(element.name, 'myGetter'); | 219 expect(element.name, 'myGetter'); |
| 220 { | 220 { |
| 221 Location location = element.location; | 221 Location location = element.location; |
| 222 expect(location.file, '/test.dart'); | 222 expect(location.file, '/test.dart'); |
| 223 expect(location.offset, 19); | 223 expect(location.offset, 19); |
| 224 expect(location.length, 'myGetter'.length); | 224 expect(location.length, 'myGetter'.length); |
| 225 expect(location.startLine, 2); | 225 expect(location.startLine, 2); |
| 226 expect(location.startColumn, 10); | 226 expect(location.startColumn, 10); |
| 227 } | 227 } |
| 228 expect(element.parameters, '()'); | 228 expect(element.parameters, '()'); |
| 229 expect(element.returnType, 'String'); | 229 expect(element.returnType, 'String'); |
| 230 expect(element.flags, 0); | 230 expect(element.flags, 0); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void test_fromElement_LABEL() { | 233 void test_fromElement_LABEL() { |
| 234 Source source = addSource('/test.dart', ''' | 234 Source source = addSource('/test.dart', ''' |
| 235 main() { | 235 main() { |
| 236 myLabel: | 236 myLabel: |
| 237 while (true) { | 237 while (true) { |
| 238 break myLabel; | 238 break myLabel; |
| 239 } | 239 } |
| 240 }'''); | 240 }'''); |
| 241 CompilationUnit unit = resolveLibraryUnit(source); | 241 CompilationUnit unit = resolveLibraryUnit(source); |
| 242 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); | 242 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); |
| 243 // create notification Element | 243 // create notification Element |
| 244 Element element = new Element.fromEngine(engineElement); | 244 Element element = newElement_fromEngine(engineElement); |
| 245 expect(element.kind, ElementKind.LABEL); | 245 expect(element.kind, ElementKind.LABEL); |
| 246 expect(element.name, 'myLabel'); | 246 expect(element.name, 'myLabel'); |
| 247 { | 247 { |
| 248 Location location = element.location; | 248 Location location = element.location; |
| 249 expect(location.file, '/test.dart'); | 249 expect(location.file, '/test.dart'); |
| 250 expect(location.offset, 9); | 250 expect(location.offset, 9); |
| 251 expect(location.length, 'myLabel'.length); | 251 expect(location.length, 'myLabel'.length); |
| 252 expect(location.startLine, 2); | 252 expect(location.startLine, 2); |
| 253 expect(location.startColumn, 1); | 253 expect(location.startColumn, 1); |
| 254 } | 254 } |
| 255 expect(element.parameters, isNull); | 255 expect(element.parameters, isNull); |
| 256 expect(element.returnType, isNull); | 256 expect(element.returnType, isNull); |
| 257 expect(element.flags, 0); | 257 expect(element.flags, 0); |
| 258 } | 258 } |
| 259 | 259 |
| 260 void test_fromElement_METHOD() { | 260 void test_fromElement_METHOD() { |
| 261 Source source = addSource('/test.dart', ''' | 261 Source source = addSource('/test.dart', ''' |
| 262 class A { | 262 class A { |
| 263 static List<String> myMethod(int a, {String b}) { | 263 static List<String> myMethod(int a, {String b}) { |
| 264 return null; | 264 return null; |
| 265 } | 265 } |
| 266 }'''); | 266 }'''); |
| 267 CompilationUnit unit = resolveLibraryUnit(source); | 267 CompilationUnit unit = resolveLibraryUnit(source); |
| 268 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | 268 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); |
| 269 // create notification Element | 269 // create notification Element |
| 270 Element element = new Element.fromEngine(engineElement); | 270 Element element = newElement_fromEngine(engineElement); |
| 271 expect(element.kind, ElementKind.METHOD); | 271 expect(element.kind, ElementKind.METHOD); |
| 272 expect(element.name, 'myMethod'); | 272 expect(element.name, 'myMethod'); |
| 273 { | 273 { |
| 274 Location location = element.location; | 274 Location location = element.location; |
| 275 expect(location.file, '/test.dart'); | 275 expect(location.file, '/test.dart'); |
| 276 expect(location.offset, 32); | 276 expect(location.offset, 32); |
| 277 expect(location.length, 'myGetter'.length); | 277 expect(location.length, 'myGetter'.length); |
| 278 expect(location.startLine, 2); | 278 expect(location.startLine, 2); |
| 279 expect(location.startColumn, 23); | 279 expect(location.startColumn, 23); |
| 280 } | 280 } |
| 281 expect(element.parameters, '(int a, {String b})'); | 281 expect(element.parameters, '(int a, {String b})'); |
| 282 expect(element.returnType, 'List<String>'); | 282 expect(element.returnType, 'List<String>'); |
| 283 expect(element.flags, Element.FLAG_STATIC); | 283 expect(element.flags, Element.FLAG_STATIC); |
| 284 } | 284 } |
| 285 | 285 |
| 286 void test_fromElement_SETTER() { | 286 void test_fromElement_SETTER() { |
| 287 Source source = addSource('/test.dart', ''' | 287 Source source = addSource('/test.dart', ''' |
| 288 class A { | 288 class A { |
| 289 set mySetter(String x) {} | 289 set mySetter(String x) {} |
| 290 }'''); | 290 }'''); |
| 291 CompilationUnit unit = resolveLibraryUnit(source); | 291 CompilationUnit unit = resolveLibraryUnit(source); |
| 292 engine.FieldElement engineFieldElement = | 292 engine.FieldElement engineFieldElement = |
| 293 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); | 293 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); |
| 294 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; | 294 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; |
| 295 // create notification Element | 295 // create notification Element |
| 296 Element element = new Element.fromEngine(engineElement); | 296 Element element = newElement_fromEngine(engineElement); |
| 297 expect(element.kind, ElementKind.SETTER); | 297 expect(element.kind, ElementKind.SETTER); |
| 298 expect(element.name, 'mySetter'); | 298 expect(element.name, 'mySetter'); |
| 299 { | 299 { |
| 300 Location location = element.location; | 300 Location location = element.location; |
| 301 expect(location.file, '/test.dart'); | 301 expect(location.file, '/test.dart'); |
| 302 expect(location.offset, 16); | 302 expect(location.offset, 16); |
| 303 expect(location.length, 'mySetter'.length); | 303 expect(location.length, 'mySetter'.length); |
| 304 expect(location.startLine, 2); | 304 expect(location.startLine, 2); |
| 305 expect(location.startColumn, 7); | 305 expect(location.startColumn, 7); |
| 306 } | 306 } |
| 307 expect(element.parameters, '(String x)'); | 307 expect(element.parameters, '(String x)'); |
| 308 expect(element.returnType, isNull); | 308 expect(element.returnType, isNull); |
| 309 expect(element.flags, 0); | 309 expect(element.flags, 0); |
| 310 } | 310 } |
| 311 | 311 |
| 312 void test_fromElement_dynamic() { | 312 void test_fromElement_dynamic() { |
| 313 var engineElement = engine.DynamicElementImpl.instance; | 313 var engineElement = engine.DynamicElementImpl.instance; |
| 314 // create notification Element | 314 // create notification Element |
| 315 Element element = new Element.fromEngine(engineElement); | 315 Element element = newElement_fromEngine(engineElement); |
| 316 expect(element.kind, ElementKind.UNKNOWN); | 316 expect(element.kind, ElementKind.UNKNOWN); |
| 317 expect(element.name, 'dynamic'); | 317 expect(element.name, 'dynamic'); |
| 318 expect(element.location, isNull); | 318 expect(element.location, isNull); |
| 319 expect(element.parameters, isNull); | 319 expect(element.parameters, isNull); |
| 320 expect(element.returnType, isNull); | 320 expect(element.returnType, isNull); |
| 321 expect(element.flags, 0); | 321 expect(element.flags, 0); |
| 322 } | 322 } |
| 323 } | 323 } |
| OLD | NEW |