OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 engine.declaration_resolver_test; | 5 library engine.declaration_resolver_test; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
9 import 'package:analyzer/src/dart/ast/ast.dart'; | 9 import 'package:analyzer/src/dart/ast/ast.dart'; |
10 import 'package:analyzer/src/dart/ast/utilities.dart'; | 10 import 'package:analyzer/src/dart/ast/utilities.dart'; |
(...skipping 27 matching lines...) Expand all Loading... |
38 return EngineTestCase.findNode( | 38 return EngineTestCase.findNode( |
39 root, code, search, (n) => n is SimpleIdentifier); | 39 root, code, search, (n) => n is SimpleIdentifier); |
40 } | 40 } |
41 | 41 |
42 @reflectiveTest | 42 @reflectiveTest |
43 class DeclarationResolverMetadataTest extends ResolverTestCase { | 43 class DeclarationResolverMetadataTest extends ResolverTestCase { |
44 String code; | 44 String code; |
45 CompilationUnit unit; | 45 CompilationUnit unit; |
46 CompilationUnit unit2; | 46 CompilationUnit unit2; |
47 | 47 |
48 void checkMetadata(String search) { | 48 void checkMetadata(String search, {bool expectDifferent: false}) { |
49 NodeList<Annotation> metadata = _findMetadata(unit, search); | 49 NodeList<Annotation> metadata = _findMetadata(unit, search); |
50 NodeList<Annotation> metadata2 = _findMetadata(unit2, search); | 50 NodeList<Annotation> metadata2 = _findMetadata(unit2, search); |
51 expect(metadata, isNotEmpty); | 51 expect(metadata, isNotEmpty); |
52 for (int i = 0; i < metadata.length; i++) { | 52 for (int i = 0; i < metadata.length; i++) { |
53 expect( | 53 Matcher expectation = same(metadata[i].elementAnnotation); |
54 metadata2[i].elementAnnotation, same(metadata[i].elementAnnotation)); | 54 if (expectDifferent) { |
| 55 expectation = isNot(expectation); |
| 56 } |
| 57 expect(metadata2[i].elementAnnotation, expectation); |
55 } | 58 } |
56 } | 59 } |
57 | 60 |
58 void setupCode(String code) { | 61 void setupCode(String code) { |
59 this.code = code; | 62 this.code = code; |
60 unit = resolveSource(code + ' const a = null;'); | 63 unit = resolveSource(code + ' const a = null;'); |
61 unit2 = _cloneResolveUnit(unit); | 64 unit2 = _cloneResolveUnit(unit); |
62 } | 65 } |
63 | 66 |
64 void test_metadata_classDeclaration() { | 67 void test_metadata_classDeclaration() { |
(...skipping 11 matching lines...) Expand all Loading... |
76 checkMetadata('x'); | 79 checkMetadata('x'); |
77 } | 80 } |
78 | 81 |
79 void test_metadata_constructorDeclaration_unnamed() { | 82 void test_metadata_constructorDeclaration_unnamed() { |
80 setupCode('class C { @a C(); }'); | 83 setupCode('class C { @a C(); }'); |
81 checkMetadata('C()'); | 84 checkMetadata('C()'); |
82 } | 85 } |
83 | 86 |
84 void test_metadata_declaredIdentifier() { | 87 void test_metadata_declaredIdentifier() { |
85 setupCode('f(x, y) { for (@a var x in y) {} }'); | 88 setupCode('f(x, y) { for (@a var x in y) {} }'); |
86 checkMetadata('var'); | 89 checkMetadata('var', expectDifferent: true); |
87 } | 90 } |
88 | 91 |
89 void test_metadata_enumDeclaration() { | 92 void test_metadata_enumDeclaration() { |
90 setupCode('@a enum E { v }'); | 93 setupCode('@a enum E { v }'); |
91 checkMetadata('E'); | 94 checkMetadata('E'); |
92 } | 95 } |
93 | 96 |
94 void test_metadata_exportDirective() { | 97 void test_metadata_exportDirective() { |
95 addNamedSource('/foo.dart', 'class C {}'); | 98 addNamedSource('/foo.dart', 'class C {}'); |
96 setupCode('@a export "foo.dart";'); | 99 setupCode('@a export "foo.dart";'); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 setupCode('f() { @a g() {} }'); | 171 setupCode('f() { @a g() {} }'); |
169 // Note: metadata on local function declarations is ignored by the | 172 // Note: metadata on local function declarations is ignored by the |
170 // analyzer. TODO(paulberry): is this a bug? | 173 // analyzer. TODO(paulberry): is this a bug? |
171 FunctionDeclaration node = EngineTestCase.findNode( | 174 FunctionDeclaration node = EngineTestCase.findNode( |
172 unit, code, 'g', (AstNode n) => n is FunctionDeclaration); | 175 unit, code, 'g', (AstNode n) => n is FunctionDeclaration); |
173 expect((node as FunctionDeclarationImpl).metadata, isEmpty); | 176 expect((node as FunctionDeclarationImpl).metadata, isEmpty); |
174 } | 177 } |
175 | 178 |
176 void test_metadata_localVariableDeclaration() { | 179 void test_metadata_localVariableDeclaration() { |
177 setupCode('f() { @a int x; }'); | 180 setupCode('f() { @a int x; }'); |
178 checkMetadata('x'); | 181 checkMetadata('x', expectDifferent: true); |
179 } | 182 } |
180 | 183 |
181 void test_metadata_methodDeclaration_getter() { | 184 void test_metadata_methodDeclaration_getter() { |
182 setupCode('class C { @a get m => null; }'); | 185 setupCode('class C { @a get m => null; }'); |
183 checkMetadata('m'); | 186 checkMetadata('m'); |
184 } | 187 } |
185 | 188 |
186 void test_metadata_methodDeclaration_method() { | 189 void test_metadata_methodDeclaration_method() { |
187 setupCode('class C { @a m() {} }'); | 190 setupCode('class C { @a m() {} }'); |
188 checkMetadata('m'); | 191 checkMetadata('m'); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 } | 254 } |
252 } | 255 } |
253 | 256 |
254 @reflectiveTest | 257 @reflectiveTest |
255 class DeclarationResolverTest extends ResolverTestCase { | 258 class DeclarationResolverTest extends ResolverTestCase { |
256 @override | 259 @override |
257 void setUp() { | 260 void setUp() { |
258 super.setUp(); | 261 super.setUp(); |
259 } | 262 } |
260 | 263 |
| 264 void test_closure_inside_catch_block() { |
| 265 String code = ''' |
| 266 f() { |
| 267 try { |
| 268 } catch (e) { |
| 269 return () => null; |
| 270 } |
| 271 } |
| 272 '''; |
| 273 CompilationUnit unit = resolveSource(code); |
| 274 // re-resolve |
| 275 _cloneResolveUnit(unit); |
| 276 // no other validations than built into DeclarationResolver |
| 277 } |
| 278 |
| 279 void test_closure_inside_labeled_statement() { |
| 280 String code = ''' |
| 281 f(b) { |
| 282 foo: while (true) { |
| 283 if (b) { |
| 284 break foo; |
| 285 } |
| 286 return () => null; |
| 287 } |
| 288 } |
| 289 '''; |
| 290 CompilationUnit unit = resolveSource(code); |
| 291 // re-resolve |
| 292 _cloneResolveUnit(unit); |
| 293 // no other validations than built into DeclarationResolver |
| 294 } |
| 295 |
| 296 void test_closure_inside_switch_case() { |
| 297 String code = ''' |
| 298 void f(k, m) { |
| 299 switch (k) { |
| 300 case 0: |
| 301 m.forEach((key, value) {}); |
| 302 break; |
| 303 } |
| 304 } |
| 305 '''; |
| 306 CompilationUnit unit = resolveSource(code); |
| 307 // re-resolve |
| 308 _cloneResolveUnit(unit); |
| 309 // no other validations than built into DeclarationResolver |
| 310 } |
| 311 |
| 312 void test_closure_inside_switch_default() { |
| 313 String code = ''' |
| 314 void f(k, m) { |
| 315 switch (k) { |
| 316 default: |
| 317 m.forEach((key, value) {}); |
| 318 break; |
| 319 } |
| 320 } |
| 321 '''; |
| 322 CompilationUnit unit = resolveSource(code); |
| 323 // re-resolve |
| 324 _cloneResolveUnit(unit); |
| 325 // no other validations than built into DeclarationResolver |
| 326 } |
| 327 |
261 void test_enumConstant_partiallyResolved() { | 328 void test_enumConstant_partiallyResolved() { |
262 String code = r''' | 329 String code = r''' |
263 enum Fruit {apple, pear} | 330 enum Fruit {apple, pear} |
264 '''; | 331 '''; |
265 Source source = addNamedSource('/test.dart', code); | 332 Source source = addNamedSource('/test.dart', code); |
266 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); | 333 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); |
267 analysisContext.computeResult(source, LIBRARY_ELEMENT1); | 334 analysisContext.computeResult(source, LIBRARY_ELEMENT1); |
268 CompilationUnit unit = | 335 CompilationUnit unit = |
269 analysisContext.computeResult(target, RESOLVED_UNIT1); | 336 analysisContext.computeResult(target, RESOLVED_UNIT1); |
270 _cloneResolveUnit(unit); | 337 _cloneResolveUnit(unit); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 expect(element.type.toString(), "<T>(T, T) → T"); | 603 expect(element.type.toString(), "<T>(T, T) → T"); |
537 expect(t.element, same(tElement)); | 604 expect(t.element, same(tElement)); |
538 | 605 |
539 // re-resolve | 606 // re-resolve |
540 CompilationUnit unit2 = _cloneResolveUnit(unit); | 607 CompilationUnit unit2 = _cloneResolveUnit(unit); |
541 node = _findSimpleIdentifier(unit2, code, 'max').parent; | 608 node = _findSimpleIdentifier(unit2, code, 'max').parent; |
542 t = node.typeParameters.typeParameters[0]; | 609 t = node.typeParameters.typeParameters[0]; |
543 expect(t.element, same(tElement)); | 610 expect(t.element, same(tElement)); |
544 } | 611 } |
545 } | 612 } |
OLD | NEW |