Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: dart/tests/compiler/dart2js/resolver_test.dart

Issue 20742002: Clean up error handling. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Added documentation guide lines. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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:collection'; 6 import 'dart:collection';
7 7
8 import "../../../sdk/lib/_internal/compiler/implementation/resolution/resolution .dart"; 8 import "../../../sdk/lib/_internal/compiler/implementation/resolution/resolution .dart";
9 import "compiler_helper.dart"; 9 import "compiler_helper.dart";
10 import "parser_helper.dart"; 10 import "parser_helper.dart";
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 ClassElement foo = compiler.mainApp.find(buildSourceString('Foo')); 138 ClassElement foo = compiler.mainApp.find(buildSourceString('Foo'));
139 matchResolvedTypes(visitor, 'Foo<int, String> x;', 'Foo', 139 matchResolvedTypes(visitor, 'Foo<int, String> x;', 'Foo',
140 [compiler.intClass, compiler.stringClass]); 140 [compiler.intClass, compiler.stringClass]);
141 matchResolvedTypes(visitor, 'Foo<Foo, Foo> x;', 'Foo', 141 matchResolvedTypes(visitor, 'Foo<Foo, Foo> x;', 'Foo',
142 [foo, foo]); 142 [foo, foo]);
143 143
144 compiler = new MockCompiler(); 144 compiler = new MockCompiler();
145 compiler.parseScript('class Foo<T, U> {}'); 145 compiler.parseScript('class Foo<T, U> {}');
146 compiler.resolveStatement('Foo<notype, int> x;'); 146 compiler.resolveStatement('Foo<notype, int> x;');
147 Expect.equals(1, compiler.warnings.length); 147 Expect.equals(1, compiler.warnings.length);
148 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE, 148 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE.warning,
149 compiler.warnings[0].message.kind); 149 compiler.warnings[0].message.kind);
150 Expect.equals(0, compiler.errors.length); 150 Expect.equals(0, compiler.errors.length);
151 151
152 compiler = new MockCompiler(); 152 compiler = new MockCompiler();
153 compiler.parseScript('class Foo<T, U> {}'); 153 compiler.parseScript('class Foo<T, U> {}');
154 compiler.resolveStatement('var x = new Foo<notype, int>();'); 154 compiler.resolveStatement('var x = new Foo<notype, int>();');
155 Expect.equals(0, compiler.warnings.length); 155 Expect.equals(0, compiler.warnings.length);
156 Expect.equals(1, compiler.errors.length); 156 Expect.equals(1, compiler.errors.length);
157 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE, 157 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE.error,
158 compiler.errors[0].message.kind); 158 compiler.errors[0].message.kind);
159 159
160 compiler = new MockCompiler(); 160 compiler = new MockCompiler();
161 compiler.parseScript('class Foo<T> {' 161 compiler.parseScript('class Foo<T> {'
162 ' Foo<T> t;' 162 ' Foo<T> t;'
163 ' foo(Foo<T> f) {}' 163 ' foo(Foo<T> f) {}'
164 ' bar() { g(Foo<T> f) {}; g(); }' 164 ' bar() { g(Foo<T> f) {}; g(); }'
165 '}'); 165 '}');
166 foo = compiler.mainApp.find(buildSourceString('Foo')); 166 foo = compiler.mainApp.find(buildSourceString('Foo'));
167 foo.ensureResolved(compiler); 167 foo.ensureResolved(compiler);
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 427
428 // Test that we get a warning when Foo is not defined. 428 // Test that we get a warning when Foo is not defined.
429 Map mapping = compiler.resolveStatement(statement).map; 429 Map mapping = compiler.resolveStatement(statement).map;
430 430
431 Expect.equals(2, mapping.length); // Both Foo and bar have an element. 431 Expect.equals(2, mapping.length); // Both Foo and bar have an element.
432 Expect.equals(1, compiler.warnings.length); 432 Expect.equals(1, compiler.warnings.length);
433 433
434 Node warningNode = compiler.warnings[0].node; 434 Node warningNode = compiler.warnings[0].node;
435 435
436 Expect.equals( 436 Expect.equals(
437 new Message(MessageKind.CANNOT_RESOLVE_TYPE, {'typeName': 'Foo'}), 437 new Message(
438 MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'Foo'}),
438 compiler.warnings[0].message); 439 compiler.warnings[0].message);
439 VariableDefinitions definition = compiler.parsedTree; 440 VariableDefinitions definition = compiler.parsedTree;
440 Expect.equals(warningNode, definition.type); 441 Expect.equals(warningNode, definition.type);
441 compiler.clearWarnings(); 442 compiler.clearWarnings();
442 443
443 // Test that there is no warning after defining Foo. 444 // Test that there is no warning after defining Foo.
444 compiler.parseScript("class Foo {}"); 445 compiler.parseScript("class Foo {}");
445 mapping = compiler.resolveStatement(statement).map; 446 mapping = compiler.resolveStatement(statement).map;
446 Expect.equals(2, mapping.length); 447 Expect.equals(2, mapping.length);
447 Expect.equals(0, compiler.warnings.length); 448 Expect.equals(0, compiler.warnings.length);
448 449
449 // Test that 'var' does not create a warning. 450 // Test that 'var' does not create a warning.
450 mapping = compiler.resolveStatement("var foo;").map; 451 mapping = compiler.resolveStatement("var foo;").map;
451 Expect.equals(1, mapping.length); 452 Expect.equals(1, mapping.length);
452 Expect.equals(0, compiler.warnings.length); 453 Expect.equals(0, compiler.warnings.length);
453 } 454 }
454 455
455 testSuperclass() { 456 testSuperclass() {
456 MockCompiler compiler = new MockCompiler(); 457 MockCompiler compiler = new MockCompiler();
457 compiler.parseScript("class Foo extends Bar {}"); 458 compiler.parseScript("class Foo extends Bar {}");
458 compiler.resolveStatement("Foo bar;"); 459 compiler.resolveStatement("Foo bar;");
459 // TODO(ahe): We get the same error twice: once from 460 // TODO(ahe): We get the same error twice: once from
460 // ClassResolverVisitor, and once from ClassSupertypeResolver. We 461 // ClassResolverVisitor, and once from ClassSupertypeResolver. We
461 // should only the get the error once. 462 // should only the get the error once.
462 Expect.equals(2, compiler.errors.length); 463 Expect.equals(2, compiler.errors.length);
463 var cannotResolveBar = new Message(MessageKind.CANNOT_RESOLVE_TYPE, 464 var cannotResolveBar = new Message(MessageKind.CANNOT_RESOLVE_TYPE.error,
464 {'typeName': 'Bar'}); 465 {'typeName': 'Bar'});
465 Expect.equals(cannotResolveBar, compiler.errors[0].message); 466 Expect.equals(cannotResolveBar, compiler.errors[0].message);
466 Expect.equals(cannotResolveBar, compiler.errors[1].message); 467 Expect.equals(cannotResolveBar, compiler.errors[1].message);
467 compiler.clearErrors(); 468 compiler.clearErrors();
468 469
469 compiler = new MockCompiler(); 470 compiler = new MockCompiler();
470 compiler.parseScript("class Foo extends Bar {}"); 471 compiler.parseScript("class Foo extends Bar {}");
471 compiler.parseScript("class Bar {}"); 472 compiler.parseScript("class Bar {}");
472 Map mapping = compiler.resolveStatement("Foo bar;").map; 473 Map mapping = compiler.resolveStatement("Foo bar;").map;
473 Expect.equals(2, mapping.length); 474 Expect.equals(2, mapping.length);
474 475
475 ClassElement fooElement = compiler.mainApp.find(buildSourceString('Foo')); 476 ClassElement fooElement = compiler.mainApp.find(buildSourceString('Foo'));
476 ClassElement barElement = compiler.mainApp.find(buildSourceString('Bar')); 477 ClassElement barElement = compiler.mainApp.find(buildSourceString('Bar'));
477 Expect.equals(barElement.computeType(compiler), 478 Expect.equals(barElement.computeType(compiler),
478 fooElement.supertype); 479 fooElement.supertype);
479 Expect.isTrue(fooElement.interfaces.isEmpty); 480 Expect.isTrue(fooElement.interfaces.isEmpty);
480 Expect.isTrue(barElement.interfaces.isEmpty); 481 Expect.isTrue(barElement.interfaces.isEmpty);
481 } 482 }
482 483
483 testVarSuperclass() { 484 testVarSuperclass() {
484 MockCompiler compiler = new MockCompiler(); 485 MockCompiler compiler = new MockCompiler();
485 compiler.parseScript("class Foo extends var {}"); 486 compiler.parseScript("class Foo extends var {}");
486 compiler.resolveStatement("Foo bar;"); 487 compiler.resolveStatement("Foo bar;");
487 Expect.equals(1, compiler.errors.length); 488 Expect.equals(1, compiler.errors.length);
488 Expect.equals( 489 Expect.equals(
489 new Message(MessageKind.CANNOT_RESOLVE_TYPE, {'typeName': 'var'}), 490 new Message(MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'var'}),
490 compiler.errors[0].message); 491 compiler.errors[0].message);
491 compiler.clearErrors(); 492 compiler.clearErrors();
492 } 493 }
493 494
494 testOneInterface() { 495 testOneInterface() {
495 MockCompiler compiler = new MockCompiler(); 496 MockCompiler compiler = new MockCompiler();
496 compiler.parseScript("class Foo implements Bar {}"); 497 compiler.parseScript("class Foo implements Bar {}");
497 compiler.resolveStatement("Foo bar;"); 498 compiler.resolveStatement("Foo bar;");
498 Expect.equals(1, compiler.errors.length); 499 Expect.equals(1, compiler.errors.length);
499 Expect.equals( 500 Expect.equals(
500 new Message(MessageKind.CANNOT_RESOLVE_TYPE, {'typeName': 'bar'}), 501 new Message(MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'bar'}),
501 compiler.errors[0].message); 502 compiler.errors[0].message);
502 compiler.clearErrors(); 503 compiler.clearErrors();
503 504
504 // Add the abstract class to the world and make sure everything is setup 505 // Add the abstract class to the world and make sure everything is setup
505 // correctly. 506 // correctly.
506 compiler.parseScript("abstract class Bar {}"); 507 compiler.parseScript("abstract class Bar {}");
507 508
508 ResolverVisitor visitor = 509 ResolverVisitor visitor =
509 new ResolverVisitor(compiler, null, new CollectingTreeElements(null)); 510 new ResolverVisitor(compiler, null, new CollectingTreeElements(null));
510 compiler.resolveStatement("Foo bar;"); 511 compiler.resolveStatement("Foo bar;");
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 MockCompiler compiler = new MockCompiler(); 633 MockCompiler compiler = new MockCompiler();
633 compiler.parseScript("""class A extends B {} 634 compiler.parseScript("""class A extends B {}
634 class B extends A {} 635 class B extends A {}
635 main() { return new A(); }"""); 636 main() { return new A(); }""");
636 FunctionElement mainElement = compiler.mainApp.find(MAIN); 637 FunctionElement mainElement = compiler.mainApp.find(MAIN);
637 compiler.resolver.resolve(mainElement); 638 compiler.resolver.resolve(mainElement);
638 Expect.equals(0, compiler.warnings.length); 639 Expect.equals(0, compiler.warnings.length);
639 Expect.equals(2, compiler.errors.length); 640 Expect.equals(2, compiler.errors.length);
640 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, 641 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY,
641 compiler.errors[0].message.kind); 642 compiler.errors[0].message.kind);
642 Expect.equals(MessageKind.CANNOT_FIND_CONSTRUCTOR, 643 Expect.equals(MessageKind.CANNOT_FIND_CONSTRUCTOR.error,
643 compiler.errors[1].message.kind); 644 compiler.errors[1].message.kind);
644 645
645 compiler = new MockCompiler(); 646 compiler = new MockCompiler();
646 compiler.parseScript("""abstract class A extends B {} 647 compiler.parseScript("""abstract class A extends B {}
647 abstract class B extends A {} 648 abstract class B extends A {}
648 class C implements A {} 649 class C implements A {}
649 main() { return new C(); }"""); 650 main() { return new C(); }""");
650 mainElement = compiler.mainApp.find(MAIN); 651 mainElement = compiler.mainApp.find(MAIN);
651 compiler.resolver.resolve(mainElement); 652 compiler.resolver.resolve(mainElement);
652 Expect.equals(0, compiler.warnings.length); 653 Expect.equals(0, compiler.warnings.length);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 }"""; 724 }""";
724 resolveConstructor(script, "A a = new A();", "A", "", 2, 725 resolveConstructor(script, "A a = new A();", "A", "", 2,
725 expectedWarnings: [MessageKind.ALREADY_INITIALIZED], 726 expectedWarnings: [MessageKind.ALREADY_INITIALIZED],
726 expectedErrors: [MessageKind.DUPLICATE_INITIALIZER]); 727 expectedErrors: [MessageKind.DUPLICATE_INITIALIZER]);
727 728
728 script = """class A { 729 script = """class A {
729 A() : this.foo = 1; 730 A() : this.foo = 1;
730 }"""; 731 }""";
731 resolveConstructor(script, "A a = new A();", "A", "", 0, 732 resolveConstructor(script, "A a = new A();", "A", "", 0,
732 expectedWarnings: [], 733 expectedWarnings: [],
733 expectedErrors: [MessageKind.CANNOT_RESOLVE]); 734 expectedErrors: [MessageKind.CANNOT_RESOLVE.error]);
734 735
735 script = """class A { 736 script = """class A {
736 int foo; 737 int foo;
737 int bar; 738 int bar;
738 A() : this.foo = bar; 739 A() : this.foo = bar;
739 }"""; 740 }""";
740 resolveConstructor(script, "A a = new A();", "A", "", 3, 741 resolveConstructor(script, "A a = new A();", "A", "", 3,
741 expectedWarnings: [], 742 expectedWarnings: [],
742 expectedErrors: [MessageKind.NO_INSTANCE_AVAILABLE]); 743 expectedErrors: [MessageKind.NO_INSTANCE_AVAILABLE]);
743 744
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 } 898 }
898 main() { 899 main() {
899 new A() == new B(); 900 new A() == new B();
900 }"""; 901 }""";
901 final compiler = compileScript(script); 902 final compiler = compileScript(script);
902 Expect.equals(1, compiler.warnings.length); 903 Expect.equals(1, compiler.warnings.length);
903 Expect.equals(MessageKind.OVERRIDE_EQUALS_NOT_HASH_CODE, 904 Expect.equals(MessageKind.OVERRIDE_EQUALS_NOT_HASH_CODE,
904 compiler.warnings[0].message.kind); 905 compiler.warnings[0].message.kind);
905 Expect.equals(0, compiler.errors.length); 906 Expect.equals(0, compiler.errors.length);
906 } 907 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698