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