| 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/computer/element.dart'; | 7 import 'package:analysis_server/src/computer/element.dart'; |
| 8 import 'package:analysis_server/src/constants.dart'; | 8 import 'package:analysis_server/src/constants.dart'; |
| 9 import 'package:analysis_testing/abstract_context.dart'; | 9 import 'package:analysis_testing/abstract_context.dart'; |
| 10 import 'package:analysis_testing/reflective_tests.dart'; | 10 import 'package:analysis_testing/reflective_tests.dart'; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 | 29 |
| 30 class ElementKindTest { | 30 class ElementKindTest { |
| 31 void test_toString() { | 31 void test_toString() { |
| 32 expect(ElementKind.CLASS.toString(), 'CLASS'); | 32 expect(ElementKind.CLASS.toString(), 'CLASS'); |
| 33 expect(ElementKind.COMPILATION_UNIT.toString(), 'COMPILATION_UNIT'); | 33 expect(ElementKind.COMPILATION_UNIT.toString(), 'COMPILATION_UNIT'); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void test_valueOf() { | 36 void test_valueOf() { |
| 37 expect(ElementKind.valueOf(ElementKind.CLASS.name), ElementKind.CLASS); | 37 expect(ElementKind.valueOf(ElementKind.CLASS.name), ElementKind.CLASS); |
| 38 expect(ElementKind.valueOf(ElementKind.CLASS_TYPE_ALIAS.name), | 38 expect( |
| 39 ElementKind.valueOf(ElementKind.CLASS_TYPE_ALIAS.name), |
| 39 ElementKind.CLASS_TYPE_ALIAS); | 40 ElementKind.CLASS_TYPE_ALIAS); |
| 40 expect(ElementKind.valueOf(ElementKind.COMPILATION_UNIT.name), | 41 expect( |
| 42 ElementKind.valueOf(ElementKind.COMPILATION_UNIT.name), |
| 41 ElementKind.COMPILATION_UNIT); | 43 ElementKind.COMPILATION_UNIT); |
| 42 expect(ElementKind.valueOf(ElementKind.CONSTRUCTOR.name), | 44 expect( |
| 45 ElementKind.valueOf(ElementKind.CONSTRUCTOR.name), |
| 43 ElementKind.CONSTRUCTOR); | 46 ElementKind.CONSTRUCTOR); |
| 44 expect(ElementKind.valueOf(ElementKind.FIELD.name), ElementKind.FIELD); | 47 expect(ElementKind.valueOf(ElementKind.FIELD.name), ElementKind.FIELD); |
| 45 expect(ElementKind.valueOf(ElementKind.FUNCTION.name), | 48 expect( |
| 49 ElementKind.valueOf(ElementKind.FUNCTION.name), |
| 46 ElementKind.FUNCTION); | 50 ElementKind.FUNCTION); |
| 47 expect(ElementKind.valueOf(ElementKind.FUNCTION_TYPE_ALIAS.name), | 51 expect( |
| 52 ElementKind.valueOf(ElementKind.FUNCTION_TYPE_ALIAS.name), |
| 48 ElementKind.FUNCTION_TYPE_ALIAS); | 53 ElementKind.FUNCTION_TYPE_ALIAS); |
| 49 expect(ElementKind.valueOf(ElementKind.GETTER.name), ElementKind.GETTER); | 54 expect(ElementKind.valueOf(ElementKind.GETTER.name), ElementKind.GETTER); |
| 50 expect(ElementKind.valueOf(ElementKind.LIBRARY.name), ElementKind.LIBRARY); | 55 expect(ElementKind.valueOf(ElementKind.LIBRARY.name), ElementKind.LIBRARY); |
| 51 expect(ElementKind.valueOf(ElementKind.LOCAL_VARIABLE.name), | 56 expect( |
| 57 ElementKind.valueOf(ElementKind.LOCAL_VARIABLE.name), |
| 52 ElementKind.LOCAL_VARIABLE); | 58 ElementKind.LOCAL_VARIABLE); |
| 53 expect(ElementKind.valueOf(ElementKind.METHOD.name), ElementKind.METHOD); | 59 expect(ElementKind.valueOf(ElementKind.METHOD.name), ElementKind.METHOD); |
| 60 expect( |
| 61 ElementKind.valueOf(ElementKind.PARAMETER.name), |
| 62 ElementKind.PARAMETER); |
| 54 expect(ElementKind.valueOf(ElementKind.SETTER.name), ElementKind.SETTER); | 63 expect(ElementKind.valueOf(ElementKind.SETTER.name), ElementKind.SETTER); |
| 55 expect(ElementKind.valueOf(ElementKind.TOP_LEVEL_VARIABLE.name), | 64 expect( |
| 65 ElementKind.valueOf(ElementKind.TOP_LEVEL_VARIABLE.name), |
| 56 ElementKind.TOP_LEVEL_VARIABLE); | 66 ElementKind.TOP_LEVEL_VARIABLE); |
| 57 expect(ElementKind.valueOf(ElementKind.UNIT_TEST_CASE.name), | 67 expect( |
| 68 ElementKind.valueOf(ElementKind.TYPE_PARAMETER.name), |
| 69 ElementKind.TYPE_PARAMETER); |
| 70 expect( |
| 71 ElementKind.valueOf(ElementKind.UNIT_TEST_CASE.name), |
| 58 ElementKind.UNIT_TEST_CASE); | 72 ElementKind.UNIT_TEST_CASE); |
| 59 expect(ElementKind.valueOf(ElementKind.UNIT_TEST_GROUP.name), | 73 expect( |
| 74 ElementKind.valueOf(ElementKind.UNIT_TEST_GROUP.name), |
| 60 ElementKind.UNIT_TEST_GROUP); | 75 ElementKind.UNIT_TEST_GROUP); |
| 61 expect(ElementKind.valueOf(ElementKind.UNKNOWN.name), ElementKind.UNKNOWN); | 76 expect(ElementKind.valueOf(ElementKind.UNKNOWN.name), ElementKind.UNKNOWN); |
| 62 expect(() { | 77 expect(() { |
| 63 ElementKind.valueOf('no-such-kind'); | 78 ElementKind.valueOf('no-such-kind'); |
| 64 }, throws); | 79 }, throws); |
| 65 } | 80 } |
| 66 | 81 |
| 67 void test_valueOfEngine() { | 82 void test_valueOfEngine() { |
| 68 expect(ElementKind.valueOfEngine(engine.ElementKind.CLASS), | 83 expect( |
| 84 ElementKind.valueOfEngine(engine.ElementKind.CLASS), |
| 69 ElementKind.CLASS); | 85 ElementKind.CLASS); |
| 70 expect(ElementKind.valueOfEngine(engine.ElementKind.COMPILATION_UNIT), | 86 expect( |
| 87 ElementKind.valueOfEngine(engine.ElementKind.COMPILATION_UNIT), |
| 71 ElementKind.COMPILATION_UNIT); | 88 ElementKind.COMPILATION_UNIT); |
| 72 expect(ElementKind.valueOfEngine(engine.ElementKind.CONSTRUCTOR), | 89 expect( |
| 90 ElementKind.valueOfEngine(engine.ElementKind.CONSTRUCTOR), |
| 73 ElementKind.CONSTRUCTOR); | 91 ElementKind.CONSTRUCTOR); |
| 74 expect(ElementKind.valueOfEngine(engine.ElementKind.FIELD), | 92 expect( |
| 93 ElementKind.valueOfEngine(engine.ElementKind.FIELD), |
| 75 ElementKind.FIELD); | 94 ElementKind.FIELD); |
| 76 expect(ElementKind.valueOfEngine(engine.ElementKind.FUNCTION), | 95 expect( |
| 96 ElementKind.valueOfEngine(engine.ElementKind.FUNCTION), |
| 77 ElementKind.FUNCTION); | 97 ElementKind.FUNCTION); |
| 78 expect(ElementKind.valueOfEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), | 98 expect( |
| 99 ElementKind.valueOfEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), |
| 79 ElementKind.FUNCTION_TYPE_ALIAS); | 100 ElementKind.FUNCTION_TYPE_ALIAS); |
| 80 expect(ElementKind.valueOfEngine(engine.ElementKind.GETTER), | 101 expect( |
| 102 ElementKind.valueOfEngine(engine.ElementKind.GETTER), |
| 81 ElementKind.GETTER); | 103 ElementKind.GETTER); |
| 82 expect(ElementKind.valueOfEngine(engine.ElementKind.LIBRARY), | 104 expect( |
| 105 ElementKind.valueOfEngine(engine.ElementKind.LIBRARY), |
| 83 ElementKind.LIBRARY); | 106 ElementKind.LIBRARY); |
| 84 expect(ElementKind.valueOfEngine(engine.ElementKind.LOCAL_VARIABLE), | 107 expect( |
| 108 ElementKind.valueOfEngine(engine.ElementKind.LOCAL_VARIABLE), |
| 85 ElementKind.LOCAL_VARIABLE); | 109 ElementKind.LOCAL_VARIABLE); |
| 86 expect(ElementKind.valueOfEngine(engine.ElementKind.METHOD), | 110 expect( |
| 111 ElementKind.valueOfEngine(engine.ElementKind.METHOD), |
| 87 ElementKind.METHOD); | 112 ElementKind.METHOD); |
| 88 expect(ElementKind.valueOfEngine(engine.ElementKind.SETTER), | 113 expect( |
| 114 ElementKind.valueOfEngine(engine.ElementKind.PARAMETER), |
| 115 ElementKind.PARAMETER); |
| 116 expect( |
| 117 ElementKind.valueOfEngine(engine.ElementKind.SETTER), |
| 89 ElementKind.SETTER); | 118 ElementKind.SETTER); |
| 90 expect(ElementKind.valueOfEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), | 119 expect( |
| 120 ElementKind.valueOfEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), |
| 91 ElementKind.TOP_LEVEL_VARIABLE); | 121 ElementKind.TOP_LEVEL_VARIABLE); |
| 92 expect(ElementKind.valueOfEngine(engine.ElementKind.ANGULAR_COMPONENT), | 122 expect( |
| 123 ElementKind.valueOfEngine(engine.ElementKind.TYPE_PARAMETER), |
| 124 ElementKind.TYPE_PARAMETER); |
| 125 expect( |
| 126 ElementKind.valueOfEngine(engine.ElementKind.ANGULAR_COMPONENT), |
| 93 ElementKind.UNKNOWN); | 127 ElementKind.UNKNOWN); |
| 94 } | 128 } |
| 95 } | 129 } |
| 96 | 130 |
| 97 | 131 |
| 98 @ReflectiveTestCase() | 132 @ReflectiveTestCase() |
| 99 class ElementTest extends AbstractContextTest { | 133 class ElementTest extends AbstractContextTest { |
| 100 engine.Element findElementInUnit(CompilationUnit unit, String name, | 134 engine.Element findElementInUnit(CompilationUnit unit, String name, |
| 101 [engine.ElementKind kind]) { | 135 [engine.ElementKind kind]) { |
| 102 return findChildElement(unit.element, name, kind); | 136 return findChildElement(unit.element, name, kind); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 113 expect(element.kind, ElementKind.CLASS); | 147 expect(element.kind, ElementKind.CLASS); |
| 114 expect(element.name, '_MyClass'); | 148 expect(element.name, '_MyClass'); |
| 115 { | 149 { |
| 116 Location location = element.location; | 150 Location location = element.location; |
| 117 expect(location.file, '/test.dart'); | 151 expect(location.file, '/test.dart'); |
| 118 expect(location.offset, 27); | 152 expect(location.offset, 27); |
| 119 expect(location.length, '_MyClass'.length); | 153 expect(location.length, '_MyClass'.length); |
| 120 expect(location.startLine, 2); | 154 expect(location.startLine, 2); |
| 121 expect(location.startColumn, 16); | 155 expect(location.startColumn, 16); |
| 122 } | 156 } |
| 123 expect(element.flags, Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | | 157 expect( |
| 124 Element.FLAG_PRIVATE); | 158 element.flags, |
| 159 Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE); |
| 125 } | 160 } |
| 126 | 161 |
| 127 void test_fromElement_CONSTRUCTOR() { | 162 void test_fromElement_CONSTRUCTOR() { |
| 128 Source source = addSource('/test.dart', ''' | 163 Source source = addSource('/test.dart', ''' |
| 129 class A { | 164 class A { |
| 130 const A.myConstructor(int a, [String b]); | 165 const A.myConstructor(int a, [String b]); |
| 131 }'''); | 166 }'''); |
| 132 CompilationUnit unit = resolveLibraryUnit(source); | 167 CompilationUnit unit = resolveLibraryUnit(source); |
| 133 engine.ConstructorElement engineElement = findElementInUnit(unit, | 168 engine.ConstructorElement engineElement = |
| 134 'myConstructor'); | 169 findElementInUnit(unit, 'myConstructor'); |
| 135 // create notification Element | 170 // create notification Element |
| 136 Element element = new Element.fromEngine(engineElement); | 171 Element element = new Element.fromEngine(engineElement); |
| 137 expect(element.kind, ElementKind.CONSTRUCTOR); | 172 expect(element.kind, ElementKind.CONSTRUCTOR); |
| 138 expect(element.name, 'myConstructor'); | 173 expect(element.name, 'myConstructor'); |
| 139 { | 174 { |
| 140 Location location = element.location; | 175 Location location = element.location; |
| 141 expect(location.file, '/test.dart'); | 176 expect(location.file, '/test.dart'); |
| 142 expect(location.offset, 20); | 177 expect(location.offset, 20); |
| 143 expect(location.length, 'myConstructor'.length); | 178 expect(location.length, 'myConstructor'.length); |
| 144 expect(location.startLine, 2); | 179 expect(location.startLine, 2); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 172 expect(element.returnType, isNull); | 207 expect(element.returnType, isNull); |
| 173 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 208 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 174 } | 209 } |
| 175 | 210 |
| 176 void test_fromElement_GETTER() { | 211 void test_fromElement_GETTER() { |
| 177 Source source = addSource('/test.dart', ''' | 212 Source source = addSource('/test.dart', ''' |
| 178 class A { | 213 class A { |
| 179 String myGetter => 42; | 214 String myGetter => 42; |
| 180 }'''); | 215 }'''); |
| 181 CompilationUnit unit = resolveLibraryUnit(source); | 216 CompilationUnit unit = resolveLibraryUnit(source); |
| 182 engine.PropertyAccessorElement engineElement = findElementInUnit(unit, | 217 engine.PropertyAccessorElement engineElement = |
| 183 'myGetter', engine.ElementKind.GETTER); | 218 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); |
| 184 // create notification Element | 219 // create notification Element |
| 185 Element element = new Element.fromEngine(engineElement); | 220 Element element = new Element.fromEngine(engineElement); |
| 186 expect(element.kind, ElementKind.GETTER); | 221 expect(element.kind, ElementKind.GETTER); |
| 187 expect(element.name, 'myGetter'); | 222 expect(element.name, 'myGetter'); |
| 188 { | 223 { |
| 189 Location location = element.location; | 224 Location location = element.location; |
| 190 expect(location.file, '/test.dart'); | 225 expect(location.file, '/test.dart'); |
| 191 expect(location.offset, 19); | 226 expect(location.offset, 19); |
| 192 expect(location.length, 'myGetter'.length); | 227 expect(location.length, 'myGetter'.length); |
| 193 expect(location.startLine, 2); | 228 expect(location.startLine, 2); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 218 expect(location.length, 'myGetter'.length); | 253 expect(location.length, 'myGetter'.length); |
| 219 expect(location.startLine, 2); | 254 expect(location.startLine, 2); |
| 220 expect(location.startColumn, 23); | 255 expect(location.startColumn, 23); |
| 221 } | 256 } |
| 222 expect(element.parameters, '(int a, {String b})'); | 257 expect(element.parameters, '(int a, {String b})'); |
| 223 expect(element.returnType, 'List<String>'); | 258 expect(element.returnType, 'List<String>'); |
| 224 expect(element.flags, Element.FLAG_STATIC); | 259 expect(element.flags, Element.FLAG_STATIC); |
| 225 } | 260 } |
| 226 | 261 |
| 227 void test_fromJson() { | 262 void test_fromJson() { |
| 228 var flags = Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE | | 263 var flags = |
| 264 Element.FLAG_DEPRECATED | |
| 265 Element.FLAG_PRIVATE | |
| 229 Element.FLAG_STATIC; | 266 Element.FLAG_STATIC; |
| 230 var json = { | 267 var json = { |
| 231 KIND: 'METHOD', | 268 KIND: 'METHOD', |
| 232 NAME: 'my name', | 269 NAME: 'my name', |
| 233 LOCATION: { | 270 LOCATION: { |
| 234 FILE: '/project/file.dart', | 271 FILE: '/project/file.dart', |
| 235 OFFSET: 1, | 272 OFFSET: 1, |
| 236 LENGTH: 2, | 273 LENGTH: 2, |
| 237 START_LINE: 3, | 274 START_LINE: 3, |
| 238 START_COLUMN: 4, | 275 START_COLUMN: 4, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 265 var json = { | 302 var json = { |
| 266 KIND: 'METHOD', | 303 KIND: 'METHOD', |
| 267 NAME: 'my name', | 304 NAME: 'my name', |
| 268 LOCATION: { | 305 LOCATION: { |
| 269 FILE: '/project/file.dart', | 306 FILE: '/project/file.dart', |
| 270 OFFSET: 1, | 307 OFFSET: 1, |
| 271 LENGTH: 2, | 308 LENGTH: 2, |
| 272 START_LINE: 3, | 309 START_LINE: 3, |
| 273 START_COLUMN: 4, | 310 START_COLUMN: 4, |
| 274 }, | 311 }, |
| 275 FLAGS: Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE | | 312 FLAGS: Element.FLAG_DEPRECATED | |
| 313 Element.FLAG_PRIVATE | |
| 276 Element.FLAG_STATIC, | 314 Element.FLAG_STATIC, |
| 277 PARAMETERS: '(int a, String b)', | 315 PARAMETERS: '(int a, String b)', |
| 278 RETURN_TYPE: 'List<String>' | 316 RETURN_TYPE: 'List<String>' |
| 279 }; | 317 }; |
| 280 Element element = new Element.fromJson(json); | 318 Element element = new Element.fromJson(json); |
| 281 expect(element.toJson(), equals(json)); | 319 expect(element.toJson(), equals(json)); |
| 282 } | 320 } |
| 283 } | 321 } |
| OLD | NEW |