| 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/utilities.dart'; | 10 import 'package:analyzer/src/dart/ast/utilities.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
| 11 import 'package:analyzer/src/generated/resolver.dart'; | 12 import 'package:analyzer/src/generated/resolver.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 13 | 14 |
| 14 import '../reflective_tests.dart'; | 15 import '../reflective_tests.dart'; |
| 15 import '../utils.dart'; | 16 import '../utils.dart'; |
| 16 import 'resolver_test.dart'; | 17 import 'resolver_test.dart'; |
| 17 import 'test_support.dart'; | 18 import 'test_support.dart'; |
| 18 | 19 |
| 19 main() { | 20 main() { |
| 20 initializeTestEnvironment(); | 21 initializeTestEnvironment(); |
| 22 runReflectiveTests(DeclarationResolverMetadataTest); |
| 21 runReflectiveTests(DeclarationResolverTest); | 23 runReflectiveTests(DeclarationResolverTest); |
| 22 runReflectiveTests(StrongModeDeclarationResolverTest); | 24 runReflectiveTests(StrongModeDeclarationResolverTest); |
| 23 } | 25 } |
| 24 | 26 |
| 25 CompilationUnit _cloneResolveUnit(CompilationUnit unit) { | 27 CompilationUnit _cloneResolveUnit(CompilationUnit unit) { |
| 26 CompilationUnit clonedUnit = AstCloner.clone(unit); | 28 CompilationUnit clonedUnit = AstCloner.clone(unit); |
| 27 new DeclarationResolver().resolve(clonedUnit, unit.element); | 29 new DeclarationResolver().resolve(clonedUnit, unit.element); |
| 28 return clonedUnit; | 30 return clonedUnit; |
| 29 } | 31 } |
| 30 | 32 |
| 31 SimpleIdentifier _findSimpleIdentifier( | 33 SimpleIdentifier _findSimpleIdentifier( |
| 32 AstNode root, String code, String search) { | 34 AstNode root, String code, String search) { |
| 33 return EngineTestCase.findNode( | 35 return EngineTestCase.findNode( |
| 34 root, code, search, (n) => n is SimpleIdentifier); | 36 root, code, search, (n) => n is SimpleIdentifier); |
| 35 } | 37 } |
| 36 | 38 |
| 37 @reflectiveTest | 39 @reflectiveTest |
| 40 class DeclarationResolverMetadataTest extends ResolverTestCase { |
| 41 String code; |
| 42 CompilationUnit unit; |
| 43 CompilationUnit unit2; |
| 44 |
| 45 void checkMetadata(String search) { |
| 46 NodeList<Annotation> metadata = _findMetadata(unit, search); |
| 47 NodeList<Annotation> metadata2 = _findMetadata(unit2, search); |
| 48 expect(metadata, isNotEmpty); |
| 49 for (int i = 0; i < metadata.length; i++) { |
| 50 expect( |
| 51 metadata2[i].elementAnnotation, same(metadata[i].elementAnnotation)); |
| 52 } |
| 53 } |
| 54 |
| 55 void setupCode(String code) { |
| 56 this.code = code; |
| 57 unit = resolveSource(code + ' const a = null;'); |
| 58 unit2 = _cloneResolveUnit(unit); |
| 59 } |
| 60 |
| 61 void test_metadata_classDeclaration() { |
| 62 setupCode('@a class C {}'); |
| 63 checkMetadata('C'); |
| 64 } |
| 65 |
| 66 void test_metadata_classTypeAlias() { |
| 67 setupCode('@a class C = D with E; class D {} class E {}'); |
| 68 checkMetadata('C'); |
| 69 } |
| 70 |
| 71 void test_metadata_constructorDeclaration_named() { |
| 72 setupCode('class C { @a C.x(); }'); |
| 73 checkMetadata('x'); |
| 74 } |
| 75 |
| 76 void test_metadata_constructorDeclaration_unnamed() { |
| 77 setupCode('class C { @a C(); }'); |
| 78 checkMetadata('C()'); |
| 79 } |
| 80 |
| 81 void test_metadata_declaredIdentifier() { |
| 82 setupCode('f(x, y) { for (@a var x in y) {} }'); |
| 83 checkMetadata('var'); |
| 84 } |
| 85 |
| 86 void test_metadata_enumDeclaration() { |
| 87 setupCode('@a enum E { v }'); |
| 88 checkMetadata('E'); |
| 89 } |
| 90 |
| 91 void test_metadata_exportDirective() { |
| 92 addNamedSource('/foo.dart', 'class C {}'); |
| 93 setupCode('@a export "foo.dart";'); |
| 94 checkMetadata('export'); |
| 95 } |
| 96 |
| 97 void test_metadata_fieldDeclaration() { |
| 98 setupCode('class C { @a int x; }'); |
| 99 checkMetadata('x'); |
| 100 } |
| 101 |
| 102 void test_metadata_fieldFormalParameter() { |
| 103 setupCode('class C { var x; C(@a this.x); }'); |
| 104 checkMetadata('this'); |
| 105 } |
| 106 |
| 107 void test_metadata_fieldFormalParameter_withDefault() { |
| 108 setupCode('class C { var x; C([@a this.x = null]); }'); |
| 109 checkMetadata('this'); |
| 110 } |
| 111 |
| 112 void test_metadata_functionDeclaration_function() { |
| 113 setupCode('@a f() {}'); |
| 114 checkMetadata('f'); |
| 115 } |
| 116 |
| 117 void test_metadata_functionDeclaration_getter() { |
| 118 setupCode('@a get f() => null;'); |
| 119 checkMetadata('f'); |
| 120 } |
| 121 |
| 122 void test_metadata_functionDeclaration_setter() { |
| 123 setupCode('@a set f(value) {}'); |
| 124 checkMetadata('f'); |
| 125 } |
| 126 |
| 127 void test_metadata_functionTypeAlias() { |
| 128 setupCode('@a typedef F();'); |
| 129 checkMetadata('F'); |
| 130 } |
| 131 |
| 132 void test_metadata_functionTypedFormalParameter() { |
| 133 setupCode('f(@a g()) {}'); |
| 134 checkMetadata('g'); |
| 135 } |
| 136 |
| 137 void test_metadata_functionTypedFormalParameter_withDefault() { |
| 138 setupCode('f([@a g() = null]) {}'); |
| 139 checkMetadata('g'); |
| 140 } |
| 141 |
| 142 void test_metadata_importDirective() { |
| 143 addNamedSource('/foo.dart', 'class C {}'); |
| 144 setupCode('@a import "foo.dart";'); |
| 145 checkMetadata('import'); |
| 146 } |
| 147 |
| 148 void test_metadata_libraryDirective() { |
| 149 setupCode('@a library L;'); |
| 150 checkMetadata('L'); |
| 151 } |
| 152 |
| 153 void test_metadata_localFunctionDeclaration() { |
| 154 setupCode('f() { @a g() {} }'); |
| 155 // Note: metadata on local function declarations is ignored by the |
| 156 // analyzer. TODO(paulberry): is this a bug? |
| 157 FunctionDeclaration node = EngineTestCase.findNode( |
| 158 unit, code, 'g', (AstNode n) => n is FunctionDeclaration); |
| 159 expect((node as FunctionDeclarationImpl).metadata, isEmpty); |
| 160 } |
| 161 |
| 162 void test_metadata_localVariableDeclaration() { |
| 163 setupCode('f() { @a int x; }'); |
| 164 checkMetadata('x'); |
| 165 } |
| 166 |
| 167 void test_metadata_methodDeclaration_getter() { |
| 168 setupCode('class C { @a get m => null; }'); |
| 169 checkMetadata('m'); |
| 170 } |
| 171 |
| 172 void test_metadata_methodDeclaration_method() { |
| 173 setupCode('class C { @a m() {} }'); |
| 174 checkMetadata('m'); |
| 175 } |
| 176 |
| 177 void test_metadata_methodDeclaration_setter() { |
| 178 setupCode('class C { @a set m(value) {} }'); |
| 179 checkMetadata('m'); |
| 180 } |
| 181 |
| 182 void test_metadata_partDirective() { |
| 183 addNamedSource('/foo.dart', 'part of L;'); |
| 184 setupCode('library L; @a part "foo.dart";'); |
| 185 checkMetadata('part'); |
| 186 } |
| 187 |
| 188 void test_metadata_simpleFormalParameter() { |
| 189 setupCode('f(@a x) {}) {}'); |
| 190 checkMetadata('x'); |
| 191 } |
| 192 |
| 193 void test_metadata_simpleFormalParameter_withDefault() { |
| 194 setupCode('f([@a x = null]) {}'); |
| 195 checkMetadata('x'); |
| 196 } |
| 197 |
| 198 void test_metadata_topLevelVariableDeclaration() { |
| 199 setupCode('@a int x;'); |
| 200 checkMetadata('x'); |
| 201 } |
| 202 |
| 203 void test_metadata_typeParameter_ofClass() { |
| 204 setupCode('class C<@a T> {}'); |
| 205 checkMetadata('T'); |
| 206 } |
| 207 |
| 208 void test_metadata_typeParameter_ofClassTypeAlias() { |
| 209 setupCode('class C<@a T> = D with E; class D {} class E {}'); |
| 210 checkMetadata('T'); |
| 211 } |
| 212 |
| 213 void test_metadata_typeParameter_ofFunction() { |
| 214 setupCode('f<@a T>() {}'); |
| 215 checkMetadata('T'); |
| 216 } |
| 217 |
| 218 void test_metadata_typeParameter_ofTypedef() { |
| 219 setupCode('typedef F<@a T>();'); |
| 220 checkMetadata('T'); |
| 221 } |
| 222 |
| 223 NodeList<Annotation> _findMetadata(CompilationUnit unit, String search) { |
| 224 AstNode node = |
| 225 EngineTestCase.findNode(unit, code, search, (AstNode _) => true); |
| 226 while (node != null) { |
| 227 if (node is AnnotatedNode && node.metadata.isNotEmpty) { |
| 228 return node.metadata; |
| 229 } |
| 230 if (node is NormalFormalParameter && node.metadata.isNotEmpty) { |
| 231 return node.metadata; |
| 232 } |
| 233 node = node.parent; |
| 234 } |
| 235 fail('Node not found'); |
| 236 return null; |
| 237 } |
| 238 } |
| 239 |
| 240 @reflectiveTest |
| 38 class DeclarationResolverTest extends ResolverTestCase { | 241 class DeclarationResolverTest extends ResolverTestCase { |
| 39 void fail_visitMethodDeclaration_setter_duplicate() { | 242 void fail_visitMethodDeclaration_setter_duplicate() { |
| 40 // https://github.com/dart-lang/sdk/issues/25601 | 243 // https://github.com/dart-lang/sdk/issues/25601 |
| 41 String code = r''' | 244 String code = r''' |
| 42 class C { | 245 class C { |
| 43 set zzz(x) {} | 246 set zzz(x) {} |
| 44 set zzz(y) {} | 247 set zzz(y) {} |
| 45 } | 248 } |
| 46 '''; | 249 '''; |
| 47 CompilationUnit unit = resolveSource(code); | 250 CompilationUnit unit = resolveSource(code); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 expect(element.type.toString(), "<T>(T, T) → T"); | 414 expect(element.type.toString(), "<T>(T, T) → T"); |
| 212 expect(t.element, same(tElement)); | 415 expect(t.element, same(tElement)); |
| 213 | 416 |
| 214 // re-resolve | 417 // re-resolve |
| 215 CompilationUnit unit2 = _cloneResolveUnit(unit); | 418 CompilationUnit unit2 = _cloneResolveUnit(unit); |
| 216 node = _findSimpleIdentifier(unit2, code, 'max').parent; | 419 node = _findSimpleIdentifier(unit2, code, 'max').parent; |
| 217 t = node.typeParameters.typeParameters[0]; | 420 t = node.typeParameters.typeParameters[0]; |
| 218 expect(t.element, same(tElement)); | 421 expect(t.element, same(tElement)); |
| 219 } | 422 } |
| 220 } | 423 } |
| OLD | NEW |