OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library smoke.test.codegen.recorder_test; |
| 6 |
| 7 import 'package:analyzer/src/generated/element.dart'; |
| 8 import 'package:smoke/codegen/generator.dart'; |
| 9 import 'package:smoke/codegen/recorder.dart'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 |
| 12 import 'common.dart' show checkResults; |
| 13 import 'testing_resolver_utils.dart' show initAnalyzer; |
| 14 |
| 15 main() { |
| 16 var provider = initAnalyzer(_SOURCES); |
| 17 var generator; |
| 18 var recorder; |
| 19 setUp(() { |
| 20 generator = new SmokeCodeGenerator(); |
| 21 recorder = new Recorder(generator, resolveImportUrl); |
| 22 }); |
| 23 |
| 24 group('parents', () { |
| 25 test('simple subclassing', () { |
| 26 var lib = provider.libraryFor('/a.dart'); |
| 27 recorder.lookupParent(lib.getType('A')); |
| 28 recorder.lookupParent(lib.getType('C')); |
| 29 |
| 30 checkResults(generator, |
| 31 imports: [ |
| 32 "import '/a.dart' as smoke_0;", |
| 33 "import '/b.dart' as smoke_1;", |
| 34 ], |
| 35 initCall: |
| 36 'useGeneratedCode(new StaticConfiguration(\n' |
| 37 ' checkedMode: false,\n' |
| 38 ' parents: {\n' |
| 39 ' smoke_0.A: smoke_1.B,\n' |
| 40 ' smoke_0.C: smoke_0.A,\n' |
| 41 ' }));\n'); |
| 42 }); |
| 43 |
| 44 test('single mixin', () { |
| 45 var lib = provider.libraryFor('/a.dart'); |
| 46 recorder.lookupParent(lib.getType('E')); |
| 47 |
| 48 checkResults(generator, |
| 49 imports: [ |
| 50 "import '/a.dart' as smoke_0;", |
| 51 ], |
| 52 topLevel: 'abstract class _M0 {} // A & D1\n', |
| 53 initCall: |
| 54 'useGeneratedCode(new StaticConfiguration(\n' |
| 55 ' checkedMode: false,\n' |
| 56 ' parents: {\n' |
| 57 ' smoke_0.E: _M0,\n' |
| 58 ' _M0: smoke_0.A,\n' |
| 59 ' }));\n'); |
| 60 }); |
| 61 |
| 62 test('multiple mixins', () { |
| 63 var lib = provider.libraryFor('/a.dart'); |
| 64 recorder.lookupParent(lib.getType('F')); |
| 65 |
| 66 checkResults(generator, |
| 67 imports: [ |
| 68 "import '/a.dart' as smoke_0;", |
| 69 ], |
| 70 topLevel: |
| 71 'abstract class _M0 {} // A & D1\n' |
| 72 'abstract class _M1 {} // _M0 & D2\n' |
| 73 'abstract class _M2 {} // _M1 & D3\n', |
| 74 initCall: |
| 75 'useGeneratedCode(new StaticConfiguration(\n' |
| 76 ' checkedMode: false,\n' |
| 77 ' parents: {\n' |
| 78 ' smoke_0.F: _M2,\n' |
| 79 ' _M0: smoke_0.A,\n' |
| 80 ' _M1: _M0,\n' |
| 81 ' _M2: _M1,\n' |
| 82 ' }));\n'); |
| 83 }); |
| 84 |
| 85 test('same as common_test', () { |
| 86 var lib = provider.libraryFor('/common.dart'); |
| 87 recorder.lookupParent(lib.getType('Annot')); |
| 88 recorder.lookupParent(lib.getType('AnnotB')); |
| 89 recorder.lookupParent(lib.getType('A')); |
| 90 recorder.lookupParent(lib.getType('B')); |
| 91 recorder.lookupParent(lib.getType('C')); |
| 92 recorder.lookupParent(lib.getType('D')); |
| 93 recorder.lookupParent(lib.getType('E')); |
| 94 recorder.lookupParent(lib.getType('E2')); |
| 95 recorder.lookupParent(lib.getType('F')); |
| 96 recorder.lookupParent(lib.getType('F2')); |
| 97 recorder.lookupParent(lib.getType('G')); |
| 98 recorder.lookupParent(lib.getType('H')); |
| 99 var coreLib = lib.visibleLibraries.firstWhere( |
| 100 (l) => l.displayName == 'dart.core'); |
| 101 recorder.lookupParent(coreLib.getType('int')); |
| 102 recorder.lookupParent(coreLib.getType('num')); |
| 103 |
| 104 checkResults(generator, |
| 105 imports: [ |
| 106 "import '/common.dart' as smoke_0;", |
| 107 ], |
| 108 topLevel: |
| 109 'abstract class _M0 {} // C & A\n', |
| 110 initCall: |
| 111 'useGeneratedCode(new StaticConfiguration(\n' |
| 112 ' checkedMode: false,\n' |
| 113 ' parents: {\n' |
| 114 ' smoke_0.AnnotB: smoke_0.Annot,\n' |
| 115 ' smoke_0.D: _M0,\n' |
| 116 ' smoke_0.E2: smoke_0.E,\n' |
| 117 ' smoke_0.F2: smoke_0.F,\n' |
| 118 ' smoke_0.H: smoke_0.G,\n' |
| 119 ' int: num,\n' |
| 120 ' _M0: smoke_0.C,\n' |
| 121 ' }));\n'); |
| 122 }); |
| 123 }); |
| 124 |
| 125 group('lookup member', () { |
| 126 var lib; |
| 127 setUp(() { |
| 128 lib = provider.libraryFor('/common.dart'); |
| 129 }); |
| 130 |
| 131 test('missing declaration', () { |
| 132 recorder.lookupMember(lib.getType('A'), 'q'); |
| 133 checkResults(generator, |
| 134 imports: [ |
| 135 "import '/common.dart' as smoke_0;", |
| 136 ], |
| 137 initCall: |
| 138 'useGeneratedCode(new StaticConfiguration(\n' |
| 139 ' checkedMode: false,\n' |
| 140 ' declarations: {\n' |
| 141 ' smoke_0.A: const {},\n' |
| 142 ' }));\n'); |
| 143 }); |
| 144 |
| 145 test('field declaration', () { |
| 146 recorder.lookupMember(lib.getType('A'), 'i'); |
| 147 checkResults(generator, |
| 148 imports: [ |
| 149 "import '/common.dart' as smoke_0;", |
| 150 ], |
| 151 initCall: |
| 152 'useGeneratedCode(new StaticConfiguration(\n' |
| 153 ' checkedMode: false,\n' |
| 154 ' declarations: {\n' |
| 155 ' smoke_0.A: {\n' |
| 156 ' #i: const Declaration(#i, int),\n' |
| 157 ' },\n' |
| 158 ' }));\n'); |
| 159 }); |
| 160 |
| 161 test('property declaration', () { |
| 162 recorder.lookupMember(lib.getType('A'), 'j2'); |
| 163 checkResults(generator, |
| 164 imports: [ |
| 165 "import '/common.dart' as smoke_0;", |
| 166 ], |
| 167 initCall: |
| 168 'useGeneratedCode(new StaticConfiguration(\n' |
| 169 ' checkedMode: false,\n' |
| 170 ' declarations: {\n' |
| 171 ' smoke_0.A: {\n' |
| 172 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n' |
| 173 ' },\n' |
| 174 ' }));\n'); |
| 175 }); |
| 176 |
| 177 test('method declaration', () { |
| 178 recorder.lookupMember(lib.getType('A'), 'inc0'); |
| 179 checkResults(generator, |
| 180 imports: [ |
| 181 "import '/common.dart' as smoke_0;", |
| 182 ], |
| 183 initCall: |
| 184 'useGeneratedCode(new StaticConfiguration(\n' |
| 185 ' checkedMode: false,\n' |
| 186 ' declarations: {\n' |
| 187 ' smoke_0.A: {\n' |
| 188 ' #inc0: const Declaration(#inc0, Function, kind: METHOD),\n' |
| 189 ' },\n' |
| 190 ' }));\n'); |
| 191 }); |
| 192 |
| 193 test('inherited field - not recursive', () { |
| 194 recorder.lookupMember(lib.getType('D'), 'i'); |
| 195 checkResults(generator, |
| 196 imports: [ |
| 197 "import '/common.dart' as smoke_0;", |
| 198 ], |
| 199 initCall: |
| 200 'useGeneratedCode(new StaticConfiguration(\n' |
| 201 ' checkedMode: false,\n' |
| 202 ' declarations: {\n' |
| 203 ' smoke_0.D: const {},\n' |
| 204 ' }));\n'); |
| 205 }); |
| 206 |
| 207 test('inherited field - recursive', () { |
| 208 recorder.lookupMember(lib.getType('D'), 'i', recursive: true); |
| 209 checkResults(generator, |
| 210 imports: [ |
| 211 "import '/common.dart' as smoke_0;", |
| 212 ], |
| 213 topLevel: 'abstract class _M0 {} // C & A\n', |
| 214 initCall: |
| 215 'useGeneratedCode(new StaticConfiguration(\n' |
| 216 ' checkedMode: false,\n' |
| 217 ' parents: {\n' |
| 218 ' smoke_0.D: _M0,\n' |
| 219 ' _M0: smoke_0.C,\n' |
| 220 ' },\n' |
| 221 ' declarations: {\n' |
| 222 ' smoke_0.D: const {},\n' |
| 223 ' _M0: {\n' |
| 224 ' #i: const Declaration(#i, int),\n' |
| 225 ' },\n' |
| 226 ' }));\n'); |
| 227 }); |
| 228 }); |
| 229 |
| 230 group('query', () { |
| 231 test('default query', () { |
| 232 var options = new QueryOptions(); |
| 233 var lib = provider.libraryFor('/common.dart'); |
| 234 recorder.runQuery(lib.getType('A'), options); |
| 235 checkResults(generator, |
| 236 imports: [ |
| 237 "import '/common.dart' as smoke_0;", |
| 238 ], |
| 239 initCall: |
| 240 'useGeneratedCode(new StaticConfiguration(\n' |
| 241 ' checkedMode: false,\n' |
| 242 ' declarations: {\n' |
| 243 ' smoke_0.A: {\n' |
| 244 ' #i: const Declaration(#i, int),\n' |
| 245 ' #j: const Declaration(#j, int),\n' |
| 246 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n' |
| 247 ' },\n' |
| 248 ' }));\n'); |
| 249 |
| 250 }); |
| 251 |
| 252 test('only fields', () { |
| 253 var options = new QueryOptions(includeProperties: false); |
| 254 var lib = provider.libraryFor('/common.dart'); |
| 255 recorder.runQuery(lib.getType('A'), options); |
| 256 checkResults(generator, |
| 257 imports: [ |
| 258 "import '/common.dart' as smoke_0;", |
| 259 ], |
| 260 initCall: |
| 261 'useGeneratedCode(new StaticConfiguration(\n' |
| 262 ' checkedMode: false,\n' |
| 263 ' declarations: {\n' |
| 264 ' smoke_0.A: {\n' |
| 265 ' #i: const Declaration(#i, int),\n' |
| 266 ' #j: const Declaration(#j, int),\n' |
| 267 ' },\n' |
| 268 ' }));\n'); |
| 269 |
| 270 }); |
| 271 |
| 272 test('only properties', () { |
| 273 var options = new QueryOptions(includeFields: false); |
| 274 var lib = provider.libraryFor('/common.dart'); |
| 275 recorder.runQuery(lib.getType('A'), options); |
| 276 checkResults(generator, |
| 277 imports: [ |
| 278 "import '/common.dart' as smoke_0;", |
| 279 ], |
| 280 initCall: |
| 281 'useGeneratedCode(new StaticConfiguration(\n' |
| 282 ' checkedMode: false,\n' |
| 283 ' declarations: {\n' |
| 284 ' smoke_0.A: {\n' |
| 285 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n' |
| 286 ' },\n' |
| 287 ' }));\n'); |
| 288 |
| 289 }); |
| 290 |
| 291 test('fields, properties, and and methods', () { |
| 292 var options = new QueryOptions(includeMethods: true); |
| 293 var lib = provider.libraryFor('/common.dart'); |
| 294 recorder.runQuery(lib.getType('A'), options); |
| 295 checkResults(generator, |
| 296 imports: [ |
| 297 "import '/common.dart' as smoke_0;", |
| 298 ], |
| 299 initCall: |
| 300 'useGeneratedCode(new StaticConfiguration(\n' |
| 301 ' checkedMode: false,\n' |
| 302 ' declarations: {\n' |
| 303 ' smoke_0.A: {\n' |
| 304 ' #i: const Declaration(#i, int),\n' |
| 305 ' #inc0: const Declaration(#inc0, Function, kind: METHOD),\n' |
| 306 ' #inc1: const Declaration(#inc1, Function, kind: METHOD),\n' |
| 307 ' #inc2: const Declaration(#inc2, Function, kind: METHOD),\n' |
| 308 ' #j: const Declaration(#j, int),\n' |
| 309 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n' |
| 310 ' },\n' |
| 311 ' }));\n'); |
| 312 }); |
| 313 |
| 314 test('exclude inherited', () { |
| 315 var options = new QueryOptions(includeInherited: false); |
| 316 var lib = provider.libraryFor('/common.dart'); |
| 317 recorder.runQuery(lib.getType('D'), options); |
| 318 checkResults(generator, |
| 319 imports: [ |
| 320 "import '/common.dart' as smoke_0;", |
| 321 ], |
| 322 initCall: |
| 323 'useGeneratedCode(new StaticConfiguration(\n' |
| 324 ' checkedMode: false,\n' |
| 325 ' declarations: {\n' |
| 326 ' smoke_0.D: {\n' |
| 327 ' #i2: const Declaration(#i2, int, kind: PROPERTY, ' |
| 328 'isFinal: true),\n' |
| 329 ' #x2: const Declaration(#x2, int, kind: PROPERTY, ' |
| 330 'isFinal: true),\n' |
| 331 ' },\n' |
| 332 ' }));\n'); |
| 333 }); |
| 334 |
| 335 test('include inherited', () { |
| 336 var options = new QueryOptions(includeInherited: true); |
| 337 var lib = provider.libraryFor('/common.dart'); |
| 338 recorder.runQuery(lib.getType('D'), options); |
| 339 checkResults(generator, |
| 340 imports: [ |
| 341 "import '/common.dart' as smoke_0;", |
| 342 ], |
| 343 topLevel: 'abstract class _M0 {} // C & A\n', |
| 344 initCall: |
| 345 'useGeneratedCode(new StaticConfiguration(\n' |
| 346 ' checkedMode: false,\n' |
| 347 ' parents: {\n' |
| 348 ' smoke_0.D: _M0,\n' |
| 349 ' _M0: smoke_0.C,\n' |
| 350 ' },\n' |
| 351 ' declarations: {\n' |
| 352 ' smoke_0.C: {\n' |
| 353 ' #b: const Declaration(#b, smoke_0.B),\n' |
| 354 ' #x: const Declaration(#x, int),\n' |
| 355 ' #y: const Declaration(#y, String),\n' |
| 356 ' },\n' |
| 357 ' smoke_0.D: {\n' |
| 358 ' #i2: const Declaration(#i2, int, kind: PROPERTY, ' |
| 359 'isFinal: true),\n' |
| 360 ' #x2: const Declaration(#x2, int, kind: PROPERTY, ' |
| 361 'isFinal: true),\n' |
| 362 ' },\n' |
| 363 ' _M0: {\n' |
| 364 ' #i: const Declaration(#i, int),\n' |
| 365 ' #j: const Declaration(#j, int),\n' |
| 366 ' #j2: const Declaration(#j2, int, kind: PROPERTY),\n' |
| 367 ' },\n' |
| 368 ' }));\n'); |
| 369 }); |
| 370 |
| 371 test('exact annotation', () { |
| 372 var lib = provider.libraryFor('/common.dart'); |
| 373 var vars = lib.definingCompilationUnit.topLevelVariables; |
| 374 expect(vars[0].name, 'a1'); |
| 375 var options = new QueryOptions(includeInherited: true, |
| 376 withAnnotations: [vars[0]]); |
| 377 recorder.runQuery(lib.getType('H'), options); |
| 378 final annot = 'annotations: const [smoke_0.a1]'; |
| 379 checkResults(generator, |
| 380 imports: [ |
| 381 "import '/common.dart' as smoke_0;", |
| 382 ], |
| 383 initCall: |
| 384 'useGeneratedCode(new StaticConfiguration(\n' |
| 385 ' checkedMode: false,\n' |
| 386 ' parents: {\n' |
| 387 ' smoke_0.H: smoke_0.G,\n' |
| 388 ' },\n' |
| 389 ' declarations: {\n' |
| 390 ' smoke_0.G: {\n' |
| 391 ' #b: const Declaration(#b, int, $annot),\n' |
| 392 ' },\n' |
| 393 ' smoke_0.H: {\n' |
| 394 ' #f: const Declaration(#f, int, $annot),\n' |
| 395 ' #g: const Declaration(#g, int, $annot),\n' |
| 396 ' },\n' |
| 397 ' }));\n'); |
| 398 }); |
| 399 |
| 400 test('type annotation', () { |
| 401 var lib = provider.libraryFor('/common.dart'); |
| 402 var options = new QueryOptions(includeInherited: true, |
| 403 withAnnotations: [lib.getType('Annot')]); |
| 404 recorder.runQuery(lib.getType('H'), options); |
| 405 final a1Annot = 'annotations: const [smoke_0.a1]'; |
| 406 final a3Annot = 'annotations: const [smoke_0.a3]'; |
| 407 final exprAnnot = 'annotations: const [const smoke_0.Annot(1)]'; |
| 408 checkResults(generator, |
| 409 imports: [ |
| 410 "import '/common.dart' as smoke_0;", |
| 411 ], |
| 412 initCall: |
| 413 'useGeneratedCode(new StaticConfiguration(\n' |
| 414 ' checkedMode: false,\n' |
| 415 ' parents: {\n' |
| 416 ' smoke_0.H: smoke_0.G,\n' |
| 417 ' },\n' |
| 418 ' declarations: {\n' |
| 419 ' smoke_0.G: {\n' |
| 420 ' #b: const Declaration(#b, int, $a1Annot),\n' |
| 421 ' },\n' |
| 422 ' smoke_0.H: {\n' |
| 423 ' #f: const Declaration(#f, int, $a1Annot),\n' |
| 424 ' #g: const Declaration(#g, int, $a1Annot),\n' |
| 425 ' #i: const Declaration(#i, int, $a3Annot),\n' |
| 426 ' #j: const Declaration(#j, int, $exprAnnot),\n' |
| 427 ' },\n' |
| 428 ' }));\n'); |
| 429 }); |
| 430 }); |
| 431 } |
| 432 |
| 433 const _SOURCES = const { |
| 434 '/a.dart': ''' |
| 435 library a; |
| 436 import '/b.dart'; |
| 437 |
| 438 class Annot { const Annot(); } |
| 439 const annot = const Annot(); |
| 440 |
| 441 class A extends B {} |
| 442 class C extends A {} |
| 443 class D1 { |
| 444 int d1; |
| 445 } |
| 446 class D2 { |
| 447 int d2; |
| 448 } |
| 449 class D3 { |
| 450 int d3; |
| 451 } |
| 452 class E extends A with D1 { |
| 453 int e1; |
| 454 } |
| 455 class F extends A with D1, D2, D3 { |
| 456 int f1; |
| 457 } |
| 458 ''', |
| 459 |
| 460 '/b.dart': ''' |
| 461 library b; |
| 462 |
| 463 class B {} |
| 464 ''', |
| 465 '/common.dart': ''' |
| 466 library common; |
| 467 |
| 468 class A { |
| 469 int i = 42; |
| 470 int j = 44; |
| 471 int get j2 => j; |
| 472 void set j2(int v) { j = v; } |
| 473 void inc0() { i++; } |
| 474 void inc1(int v) { i = i + (v == null ? -10 : v); } |
| 475 void inc2([int v]) { i = i + (v == null ? -10 : v); } |
| 476 } |
| 477 |
| 478 class B { |
| 479 final int f = 3; |
| 480 int _w; |
| 481 int get w => _w; |
| 482 set w(int v) { _w = v; } |
| 483 |
| 484 String z; |
| 485 A a; |
| 486 |
| 487 B(this._w, this.z, this.a); |
| 488 } |
| 489 |
| 490 class C { |
| 491 int x; |
| 492 String y; |
| 493 B b; |
| 494 |
| 495 inc(int n) { |
| 496 x = x + n; |
| 497 } |
| 498 dec(int n) { |
| 499 x = x - n; |
| 500 } |
| 501 |
| 502 C(this.x, this.y, this.b); |
| 503 } |
| 504 |
| 505 |
| 506 class D extends C with A { |
| 507 int get x2 => x; |
| 508 int get i2 => i; |
| 509 |
| 510 D(x, y, b) : super(x, y, b); |
| 511 } |
| 512 |
| 513 class E { |
| 514 set x(int v) { } |
| 515 int get y => 1; |
| 516 |
| 517 noSuchMethod(i) => y; |
| 518 } |
| 519 |
| 520 class E2 extends E {} |
| 521 |
| 522 class F { |
| 523 static int staticMethod(A a) => a.i; |
| 524 } |
| 525 |
| 526 class F2 extends F {} |
| 527 |
| 528 class Annot { const Annot(int ignore); } |
| 529 class AnnotB extends Annot { const AnnotB(); } |
| 530 const a1 = const Annot(0); |
| 531 const a2 = 32; |
| 532 const a3 = const AnnotB(); |
| 533 |
| 534 |
| 535 class G { |
| 536 int a; |
| 537 @a1 int b; |
| 538 int c; |
| 539 @a2 int d; |
| 540 } |
| 541 |
| 542 class H extends G { |
| 543 int e; |
| 544 @a1 int f; |
| 545 @a1 int g; |
| 546 @a2 int h; |
| 547 @a3 int i; |
| 548 @Annot(1) int j; |
| 549 } |
| 550 ''' |
| 551 }; |
| 552 |
| 553 resolveImportUrl(LibraryElement lib) => |
| 554 lib.isDartCore ? 'dart:core' : '/${lib.displayName}.dart'; |
OLD | NEW |