OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'package:analyzer/dart/ast/ast.dart'; |
| 6 import 'package:analyzer/src/dart/analysis/referenced_names.dart'; |
| 7 import 'package:test/test.dart'; |
| 8 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 9 |
| 10 import '../../../generated/parser_test.dart'; |
| 11 |
| 12 main() { |
| 13 defineReflectiveSuite(() { |
| 14 defineReflectiveTests(ReferencedNamesBuilderTest); |
| 15 }); |
| 16 } |
| 17 |
| 18 @reflectiveTest |
| 19 class ReferencedNamesBuilderTest { |
| 20 test_class_constructor() { |
| 21 Set<String> names = _computeReferencedNames(''' |
| 22 class U { |
| 23 U.named(A a, B b) { |
| 24 C c = null; |
| 25 } |
| 26 } |
| 27 '''); |
| 28 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 29 } |
| 30 |
| 31 test_class_field() { |
| 32 Set<String> names = _computeReferencedNames(''' |
| 33 class U { |
| 34 A f = new B(); |
| 35 } |
| 36 '''); |
| 37 expect(names, unorderedEquals(['A', 'B'])); |
| 38 } |
| 39 |
| 40 test_class_getter() { |
| 41 Set<String> names = _computeReferencedNames(''' |
| 42 class U { |
| 43 A get a => new B(); |
| 44 } |
| 45 '''); |
| 46 expect(names, unorderedEquals(['A', 'B'])); |
| 47 } |
| 48 |
| 49 test_class_members() { |
| 50 Set<String> names = _computeReferencedNames(''' |
| 51 class U { |
| 52 int a; |
| 53 int get b; |
| 54 set c(_) {} |
| 55 m(D d) { |
| 56 a; |
| 57 b; |
| 58 c = 1; |
| 59 m(); |
| 60 } |
| 61 } |
| 62 '''); |
| 63 expect(names, unorderedEquals(['int', 'D'])); |
| 64 } |
| 65 |
| 66 test_class_members_dontHideQualified() { |
| 67 Set<String> names = _computeReferencedNames(''' |
| 68 class U { |
| 69 int a; |
| 70 int get b; |
| 71 set c(_) {} |
| 72 m(D d) { |
| 73 d.a; |
| 74 d.b; |
| 75 d.c; |
| 76 } |
| 77 } |
| 78 '''); |
| 79 expect(names, unorderedEquals(['int', 'D', 'a', 'b', 'c'])); |
| 80 } |
| 81 |
| 82 test_class_method() { |
| 83 Set<String> names = _computeReferencedNames(''' |
| 84 class U { |
| 85 A m(B p) { |
| 86 C v = 0; |
| 87 } |
| 88 } |
| 89 '''); |
| 90 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 91 } |
| 92 |
| 93 test_class_method_localVariables() { |
| 94 Set<String> names = _computeReferencedNames(''' |
| 95 class U { |
| 96 A m() { |
| 97 B b = null; |
| 98 b; |
| 99 { |
| 100 C c = null; |
| 101 b; |
| 102 c; |
| 103 } |
| 104 d; |
| 105 } |
| 106 } |
| 107 '''); |
| 108 expect(names, unorderedEquals(['A', 'B', 'C', 'd'])); |
| 109 } |
| 110 |
| 111 test_class_method_parameters() { |
| 112 Set<String> names = _computeReferencedNames(''' |
| 113 class U { |
| 114 m(A a) { |
| 115 a; |
| 116 b; |
| 117 } |
| 118 } |
| 119 '''); |
| 120 expect(names, unorderedEquals(['A', 'b'])); |
| 121 } |
| 122 |
| 123 test_class_method_typeParameters() { |
| 124 Set<String> names = _computeReferencedNames(''' |
| 125 class U { |
| 126 A m<T>(B b, T t) { |
| 127 C c = 0; |
| 128 } |
| 129 } |
| 130 '''); |
| 131 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 132 } |
| 133 |
| 134 test_class_setter() { |
| 135 Set<String> names = _computeReferencedNames(''' |
| 136 class U { |
| 137 set a(A a) { |
| 138 B b = null; |
| 139 } |
| 140 } |
| 141 '''); |
| 142 expect(names, unorderedEquals(['A', 'B'])); |
| 143 } |
| 144 |
| 145 test_class_typeParameters() { |
| 146 Set<String> names = _computeReferencedNames(''' |
| 147 class U<T> { |
| 148 T f = new A<T>(); |
| 149 } |
| 150 '''); |
| 151 expect(names, unorderedEquals(['A'])); |
| 152 } |
| 153 |
| 154 test_instantiatedNames_importPrefix() { |
| 155 Set<String> names = _computeReferencedNames(''' |
| 156 import 'a.dart' as p1; |
| 157 import 'b.dart' as p2; |
| 158 main() { |
| 159 new p1.A(); |
| 160 new p1.A.c1(); |
| 161 new p1.B(); |
| 162 new p2.C(); |
| 163 new D(); |
| 164 new D.c2(); |
| 165 } |
| 166 '''); |
| 167 expect(names, unorderedEquals(['A', 'B', 'C', 'D', 'c1', 'c2'])); |
| 168 } |
| 169 |
| 170 test_localFunction() { |
| 171 Set<String> names = _computeReferencedNames(''' |
| 172 f(A a) { |
| 173 g(B b) {} |
| 174 } |
| 175 '''); |
| 176 expect(names, unorderedEquals(['A', 'B'])); |
| 177 } |
| 178 |
| 179 test_superToSubs_importPrefix() { |
| 180 Set<String> names = _computeReferencedNames(''' |
| 181 import 'a.dart' as p1; |
| 182 import 'b.dart' as p2; |
| 183 class U extends p1.A with p2.B implements p2.C {} |
| 184 '''); |
| 185 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 186 } |
| 187 |
| 188 test_topLevelVariable() { |
| 189 Set<String> names = _computeReferencedNames(''' |
| 190 A v = new B(c); |
| 191 '''); |
| 192 expect(names, unorderedEquals(['A', 'B', 'c'])); |
| 193 } |
| 194 |
| 195 test_topLevelVariable_multiple() { |
| 196 Set<String> names = _computeReferencedNames(''' |
| 197 A v1 = new B(c), v2 = new D<E>(f); |
| 198 '''); |
| 199 expect(names, unorderedEquals(['A', 'B', 'c', 'D', 'E', 'f'])); |
| 200 } |
| 201 |
| 202 test_unit_classTypeAlias() { |
| 203 Set<String> names = _computeReferencedNames(''' |
| 204 class U = A with B implements C; |
| 205 '''); |
| 206 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 207 } |
| 208 |
| 209 test_unit_classTypeAlias_typeParameters() { |
| 210 Set<String> names = _computeReferencedNames(''' |
| 211 class U<T1, T2 extends D> = A<T1> with B<T2> implements C<T1, T2>; |
| 212 '''); |
| 213 expect(names, unorderedEquals(['A', 'B', 'C', 'D'])); |
| 214 } |
| 215 |
| 216 test_unit_function() { |
| 217 Set<String> names = _computeReferencedNames(''' |
| 218 A f(B b) { |
| 219 C c = 0; |
| 220 } |
| 221 '''); |
| 222 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 223 } |
| 224 |
| 225 test_unit_function_doc() { |
| 226 Set<String> names = _computeReferencedNames(''' |
| 227 /** |
| 228 * Documentation [C.d] reference. |
| 229 */ |
| 230 A f(B b) {} |
| 231 '''); |
| 232 expect(names, unorderedEquals(['A', 'B', 'C', 'd'])); |
| 233 } |
| 234 |
| 235 test_unit_function_dontHideQualified() { |
| 236 Set<String> names = _computeReferencedNames(''' |
| 237 class U { |
| 238 int a; |
| 239 int get b; |
| 240 set c(_) {} |
| 241 m(D d) { |
| 242 d.a; |
| 243 d.b; |
| 244 d.c; |
| 245 } |
| 246 } |
| 247 '''); |
| 248 expect(names, unorderedEquals(['int', 'D', 'a', 'b', 'c'])); |
| 249 } |
| 250 |
| 251 test_unit_function_localFunction_parameter() { |
| 252 Set<String> names = _computeReferencedNames(''' |
| 253 A f() { |
| 254 B g(x) { |
| 255 x; |
| 256 return null; |
| 257 } |
| 258 return null; |
| 259 } |
| 260 '''); |
| 261 expect(names, unorderedEquals(['A', 'B'])); |
| 262 } |
| 263 |
| 264 test_unit_function_localFunctions() { |
| 265 Set<String> names = _computeReferencedNames(''' |
| 266 A f() { |
| 267 B b = null; |
| 268 C g() {} |
| 269 g(); |
| 270 } |
| 271 '''); |
| 272 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 273 } |
| 274 |
| 275 test_unit_function_localsDontHideQualified() { |
| 276 Set<String> names = _computeReferencedNames(''' |
| 277 f(A a, B b) { |
| 278 var v = 0; |
| 279 a.v; |
| 280 a.b; |
| 281 } |
| 282 '''); |
| 283 expect(names, unorderedEquals(['A', 'B', 'v', 'b'])); |
| 284 } |
| 285 |
| 286 test_unit_function_localVariables() { |
| 287 Set<String> names = _computeReferencedNames(''' |
| 288 A f() { |
| 289 B b = null; |
| 290 b; |
| 291 { |
| 292 C c = null; |
| 293 b; |
| 294 c; |
| 295 } |
| 296 d; |
| 297 } |
| 298 '''); |
| 299 expect(names, unorderedEquals(['A', 'B', 'C', 'd'])); |
| 300 } |
| 301 |
| 302 test_unit_function_parameters() { |
| 303 Set<String> names = _computeReferencedNames(''' |
| 304 A f(B b) { |
| 305 C c = 0; |
| 306 b; |
| 307 } |
| 308 '''); |
| 309 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 310 } |
| 311 |
| 312 test_unit_function_parameters_dontHideQualified() { |
| 313 Set<String> names = _computeReferencedNames(''' |
| 314 f(x, C g()) { |
| 315 g().x; |
| 316 } |
| 317 '''); |
| 318 expect(names, unorderedEquals(['C', 'x'])); |
| 319 } |
| 320 |
| 321 test_unit_function_typeParameters() { |
| 322 Set<String> names = _computeReferencedNames(''' |
| 323 A f<T>(B b, T t) { |
| 324 C c = 0; |
| 325 } |
| 326 '''); |
| 327 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 328 } |
| 329 |
| 330 test_unit_functionTypeAlias() { |
| 331 Set<String> names = _computeReferencedNames(''' |
| 332 typedef A F(B B, C c(D d)); |
| 333 '''); |
| 334 expect(names, unorderedEquals(['A', 'B', 'C', 'D'])); |
| 335 } |
| 336 |
| 337 test_unit_functionTypeAlias_typeParameters() { |
| 338 Set<String> names = _computeReferencedNames(''' |
| 339 typedef A F<T>(B b, T t); |
| 340 '''); |
| 341 expect(names, unorderedEquals(['A', 'B'])); |
| 342 } |
| 343 |
| 344 test_unit_getter() { |
| 345 Set<String> names = _computeReferencedNames(''' |
| 346 A get aaa { |
| 347 return new B(); |
| 348 } |
| 349 '''); |
| 350 expect(names, unorderedEquals(['A', 'B'])); |
| 351 } |
| 352 |
| 353 test_unit_setter() { |
| 354 Set<String> names = _computeReferencedNames(''' |
| 355 set aaa(A a) { |
| 356 B b = null; |
| 357 } |
| 358 '''); |
| 359 expect(names, unorderedEquals(['A', 'B'])); |
| 360 } |
| 361 |
| 362 test_unit_topLevelDeclarations() { |
| 363 Set<String> names = _computeReferencedNames(''' |
| 364 class L1 {} |
| 365 class L2 = A with B implements C; |
| 366 A L3() => null; |
| 367 typedef A L4(B b); |
| 368 A get L5 => null; |
| 369 set L6(_) {} |
| 370 A L7, L8; |
| 371 main() { |
| 372 L1; |
| 373 L2; |
| 374 L3; |
| 375 L4; |
| 376 L5; |
| 377 L6; |
| 378 L7; |
| 379 L8; |
| 380 } |
| 381 '''); |
| 382 expect(names, unorderedEquals(['A', 'B', 'C'])); |
| 383 } |
| 384 |
| 385 Set<String> _computeReferencedNames(String code) { |
| 386 CompilationUnit unit = |
| 387 ParserTestCase.parseCompilationUnit2(code, parseGenericMethods: true); |
| 388 return computeReferencedNames(unit); |
| 389 } |
| 390 } |
OLD | NEW |