OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import "package:async_helper/async_helper.dart"; | 7 import "package:async_helper/async_helper.dart"; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import "package:compiler/src/resolution/resolution.dart"; | 10 import "package:compiler/src/resolution/resolution.dart"; |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 } | 330 } |
331 | 331 |
332 Future testLocalsThree() { | 332 Future testLocalsThree() { |
333 return MockCompiler.create((MockCompiler compiler) { | 333 return MockCompiler.create((MockCompiler compiler) { |
334 ResolverVisitor visitor = compiler.resolverVisitor(); | 334 ResolverVisitor visitor = compiler.resolverVisitor(); |
335 Node tree = parseStatement("{ var a = 1; if (true) { a; } }"); | 335 Node tree = parseStatement("{ var a = 1; if (true) { a; } }"); |
336 ResolutionResult element = visitor.visit(tree); | 336 ResolutionResult element = visitor.visit(tree); |
337 Expect.equals(null, element); | 337 Expect.equals(null, element); |
338 MethodScope scope = visitor.scope; | 338 MethodScope scope = visitor.scope; |
339 Expect.equals(0, scope.elements.length); | 339 Expect.equals(0, scope.elements.length); |
340 Expect.equals(2, map(visitor).length); | 340 Expect.equals(3, map(visitor).length); |
341 List<Element> elements = map(visitor).values.toList(); | 341 List<Element> elements = map(visitor).values.toList(); |
342 Expect.equals(elements[0], elements[1]); | 342 Expect.equals(elements[0], elements[1]); |
343 }); | 343 }); |
344 } | 344 } |
345 | 345 |
346 Future testLocalsFour() { | 346 Future testLocalsFour() { |
347 return MockCompiler.create((MockCompiler compiler) { | 347 return MockCompiler.create((MockCompiler compiler) { |
348 ResolverVisitor visitor = compiler.resolverVisitor(); | 348 ResolverVisitor visitor = compiler.resolverVisitor(); |
349 Node tree = parseStatement("{ var a = 1; if (true) { var a = 1; } }"); | 349 Node tree = parseStatement("{ var a = 1; if (true) { var a = 1; } }"); |
350 ResolutionResult element = visitor.visit(tree); | 350 ResolutionResult element = visitor.visit(tree); |
351 Expect.equals(null, element); | 351 Expect.equals(null, element); |
352 MethodScope scope = visitor.scope; | 352 MethodScope scope = visitor.scope; |
353 Expect.equals(0, scope.elements.length); | 353 Expect.equals(0, scope.elements.length); |
354 Expect.equals(2, map(visitor).length); | 354 Expect.equals(2, map(visitor).length); |
355 List<Element> elements = map(visitor).values.toList(); | 355 List<Element> elements = map(visitor).values.toList(); |
356 Expect.notEquals(elements[0], elements[1]); | 356 Expect.notEquals(elements[0], elements[1]); |
357 }); | 357 }); |
358 } | 358 } |
359 | 359 |
360 Future testLocalsFive() { | 360 Future testLocalsFive() { |
361 return MockCompiler.create((MockCompiler compiler) { | 361 return MockCompiler.create((MockCompiler compiler) { |
362 ResolverVisitor visitor = compiler.resolverVisitor(); | 362 ResolverVisitor visitor = compiler.resolverVisitor(); |
363 If tree = | 363 If tree = |
364 parseStatement("if (true) { var a = 1; a; } else { var a = 2; a;}"); | 364 parseStatement("if (true) { var a = 1; a; } else { var a = 2; a;}"); |
365 ResolutionResult element = visitor.visit(tree); | 365 ResolutionResult element = visitor.visit(tree); |
366 Expect.equals(null, element); | 366 Expect.equals(null, element); |
367 MethodScope scope = visitor.scope; | 367 MethodScope scope = visitor.scope; |
368 Expect.equals(0, scope.elements.length); | 368 Expect.equals(0, scope.elements.length); |
369 Expect.equals(4, map(visitor).length); | 369 Expect.equals(6, map(visitor).length); |
370 | 370 |
371 Block thenPart = tree.thenPart; | 371 Block thenPart = tree.thenPart; |
372 List statements1 = thenPart.statements.nodes.toList(); | 372 List statements1 = thenPart.statements.nodes.toList(); |
373 Node def1 = statements1[0].definitions.nodes.head; | 373 Node def1 = statements1[0].definitions.nodes.head; |
374 Node id1 = statements1[1].expression; | 374 Node id1 = statements1[1].expression; |
375 Expect.equals(visitor.registry.mapping[def1], | 375 Expect.equals(visitor.registry.mapping[def1], |
376 visitor.registry.mapping[id1]); | 376 visitor.registry.mapping[id1]); |
377 | 377 |
378 Block elsePart = tree.elsePart; | 378 Block elsePart = tree.elsePart; |
379 List statements2 = elsePart.statements.nodes.toList(); | 379 List statements2 = elsePart.statements.nodes.toList(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 } | 412 } |
413 | 413 |
414 Future testFor() { | 414 Future testFor() { |
415 return MockCompiler.create((MockCompiler compiler) { | 415 return MockCompiler.create((MockCompiler compiler) { |
416 ResolverVisitor visitor = compiler.resolverVisitor(); | 416 ResolverVisitor visitor = compiler.resolverVisitor(); |
417 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }"); | 417 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }"); |
418 visitor.visit(tree); | 418 visitor.visit(tree); |
419 | 419 |
420 MethodScope scope = visitor.scope; | 420 MethodScope scope = visitor.scope; |
421 Expect.equals(0, scope.elements.length); | 421 Expect.equals(0, scope.elements.length); |
422 Expect.equals(7, map(visitor).length); | 422 Expect.equals(9, map(visitor).length); |
423 | 423 |
424 VariableDefinitions initializer = tree.initializer; | 424 VariableDefinitions initializer = tree.initializer; |
425 Node iNode = initializer.definitions.nodes.head; | 425 Node iNode = initializer.definitions.nodes.head; |
426 Element iElement = visitor.registry.mapping[iNode]; | 426 Element iElement = visitor.registry.mapping[iNode]; |
427 | 427 |
428 // Check that we have the expected nodes. This test relies on the mapping | 428 // Check that we have the expected nodes. This test relies on the mapping |
429 // field to be a linked hash map (preserving insertion order). | 429 // field to be a linked hash map (preserving insertion order). |
430 Expect.isTrue(map(visitor) is LinkedHashMap); | 430 Expect.isTrue(map(visitor) is LinkedHashMap); |
431 List<Node> nodes = map(visitor).keys.toList(); | 431 List<Node> nodes = map(visitor).keys.toList(); |
432 List<Element> elements = map(visitor).values.toList(); | 432 List<Element> elements = map(visitor).values.toList(); |
433 | 433 |
434 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 434 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
435 // ^^^^^ | 435 // ^^^^^ |
436 checkSendSet(iElement, nodes[0], elements[0]); | 436 checkSendSet(iElement, nodes[0], elements[0]); |
437 | 437 |
438 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 438 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
439 // ^ | 439 // ^ |
440 checkSend(iElement, nodes[1], elements[1]); | 440 checkIdentifier(iElement, nodes[1], elements[1]); |
| 441 |
| 442 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 443 // ^ |
| 444 checkSend(iElement, nodes[2], elements[2]); |
441 | 445 |
442 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 446 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
443 // ^ | 447 // ^ |
444 checkIdentifier(iElement, nodes[2], elements[2]); | 448 checkIdentifier(iElement, nodes[3], elements[3]); |
445 | 449 |
446 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 450 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
447 // ^ | 451 // ^ |
448 checkSend(iElement, nodes[3], elements[3]); | 452 checkIdentifier(iElement, nodes[4], elements[4]); |
| 453 |
| 454 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 455 // ^ |
| 456 checkSend(iElement, nodes[5], elements[5]); |
449 | 457 |
450 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 458 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
451 // ^^^^^^^^^ | 459 // ^^^^^^^^^ |
452 checkSendSet(iElement, nodes[4], elements[4]); | 460 checkSendSet(iElement, nodes[6], elements[6]); |
453 | 461 |
454 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 462 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
455 // ^ | 463 // ^ |
456 checkIdentifier(iElement, nodes[5], elements[5]); | 464 checkIdentifier(iElement, nodes[7], elements[7]); |
457 | 465 |
458 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 466 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
459 // ^^^^^ | 467 // ^^^^^ |
460 checkSendSet(iElement, nodes[6], elements[6]); | 468 checkSendSet(iElement, nodes[8], elements[8]); |
461 }); | 469 }); |
462 } | 470 } |
463 | 471 |
464 checkIdentifier(Element expected, Node node, Element actual) { | 472 checkIdentifier(Element expected, Node node, Element actual) { |
465 Expect.isTrue(node is Identifier, node.toDebugString()); | 473 Expect.isTrue(node is Identifier, node.toDebugString()); |
466 Expect.equals(expected, actual); | 474 Expect.equals(expected, actual); |
467 } | 475 } |
468 | 476 |
469 checkSend(Element expected, Node node, Element actual) { | 477 checkSend(Element expected, Node node, Element actual) { |
470 Expect.isTrue(node is Send, node.toDebugString()); | 478 Expect.isTrue(node is Send, node.toDebugString()); |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 expectedWarnings: [], | 920 expectedWarnings: [], |
913 expectedErrors: [MessageKind.CANNOT_RESOLVE]); | 921 expectedErrors: [MessageKind.CANNOT_RESOLVE]); |
914 }, | 922 }, |
915 () { | 923 () { |
916 String script = | 924 String script = |
917 """class A { | 925 """class A { |
918 int foo; | 926 int foo; |
919 int bar; | 927 int bar; |
920 A() : this.foo = bar; | 928 A() : this.foo = bar; |
921 }"""; | 929 }"""; |
922 return resolveConstructor(script, "A a = new A();", "A", "", 2, | 930 return resolveConstructor(script, "A a = new A();", "A", "", 3, |
923 expectedWarnings: [], | 931 expectedWarnings: [], |
924 expectedErrors: [MessageKind.NO_INSTANCE_AVAILABLE]); | 932 expectedErrors: [MessageKind.NO_INSTANCE_AVAILABLE]); |
925 }, | 933 }, |
926 () { | 934 () { |
927 String script = | 935 String script = |
928 """class A { | 936 """class A { |
929 int foo() => 42; | 937 int foo() => 42; |
930 A() : foo(); | 938 A() : foo(); |
931 }"""; | 939 }"""; |
932 return resolveConstructor(script, "A a = new A();", "A", "", 0, | 940 return resolveConstructor(script, "A a = new A();", "A", "", 0, |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1324 } | 1332 } |
1325 main() => A.m(); | 1333 main() => A.m(); |
1326 ''', functionName: 'm'); | 1334 ''', functionName: 'm'); |
1327 check(''' | 1335 check(''' |
1328 class A { | 1336 class A { |
1329 m() => () => await - 3; | 1337 m() => () => await - 3; |
1330 } | 1338 } |
1331 main() => new A().m(); | 1339 main() => new A().m(); |
1332 ''', className: 'A'); | 1340 ''', className: 'A'); |
1333 } | 1341 } |
OLD | NEW |