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 package com.google.dart.compiler.type; | 4 package com.google.dart.compiler.type; |
5 | 5 |
6 import static com.google.dart.compiler.common.ErrorExpectation.assertErrors; | 6 import static com.google.dart.compiler.common.ErrorExpectation.assertErrors; |
7 import static com.google.dart.compiler.common.ErrorExpectation.errEx; | 7 import static com.google.dart.compiler.common.ErrorExpectation.errEx; |
8 | 8 |
9 import com.google.common.base.Joiner; | 9 import com.google.common.base.Joiner; |
10 import com.google.common.collect.Iterables; | 10 import com.google.common.collect.Iterables; |
(...skipping 18 matching lines...) Expand all Loading... |
29 import com.google.dart.compiler.ast.DartForInStatement; | 29 import com.google.dart.compiler.ast.DartForInStatement; |
30 import com.google.dart.compiler.ast.DartFunctionExpression; | 30 import com.google.dart.compiler.ast.DartFunctionExpression; |
31 import com.google.dart.compiler.ast.DartIdentifier; | 31 import com.google.dart.compiler.ast.DartIdentifier; |
32 import com.google.dart.compiler.ast.DartInvocation; | 32 import com.google.dart.compiler.ast.DartInvocation; |
33 import com.google.dart.compiler.ast.DartMethodDefinition; | 33 import com.google.dart.compiler.ast.DartMethodDefinition; |
34 import com.google.dart.compiler.ast.DartNewExpression; | 34 import com.google.dart.compiler.ast.DartNewExpression; |
35 import com.google.dart.compiler.ast.DartNode; | 35 import com.google.dart.compiler.ast.DartNode; |
36 import com.google.dart.compiler.ast.DartParameter; | 36 import com.google.dart.compiler.ast.DartParameter; |
37 import com.google.dart.compiler.ast.DartUnit; | 37 import com.google.dart.compiler.ast.DartUnit; |
38 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; | 38 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; |
| 39 import com.google.dart.compiler.common.SourceInfo; |
39 import com.google.dart.compiler.parser.ParserErrorCode; | 40 import com.google.dart.compiler.parser.ParserErrorCode; |
40 import com.google.dart.compiler.resolver.ClassElement; | 41 import com.google.dart.compiler.resolver.ClassElement; |
41 import com.google.dart.compiler.resolver.Element; | 42 import com.google.dart.compiler.resolver.Element; |
42 import com.google.dart.compiler.resolver.ElementKind; | 43 import com.google.dart.compiler.resolver.ElementKind; |
43 import com.google.dart.compiler.resolver.MethodElement; | 44 import com.google.dart.compiler.resolver.MethodElement; |
44 import com.google.dart.compiler.resolver.NodeElement; | 45 import com.google.dart.compiler.resolver.NodeElement; |
45 import com.google.dart.compiler.resolver.ResolverErrorCode; | 46 import com.google.dart.compiler.resolver.ResolverErrorCode; |
46 import com.google.dart.compiler.resolver.TypeErrorCode; | 47 import com.google.dart.compiler.resolver.TypeErrorCode; |
47 | 48 |
48 import java.io.Reader; | 49 import java.io.Reader; |
49 import java.io.StringReader; | 50 import java.io.StringReader; |
50 import java.net.URI; | 51 import java.net.URI; |
| 52 import java.util.Iterator; |
51 import java.util.List; | 53 import java.util.List; |
| 54 import java.util.concurrent.atomic.AtomicReference; |
52 | 55 |
53 /** | 56 /** |
54 * Variant of {@link TypeAnalyzerTest}, which is based on {@link CompilerTestCas
e}. It is probably | 57 * Variant of {@link TypeAnalyzerTest}, which is based on {@link CompilerTestCas
e}. It is probably |
55 * slower, not actually unit test, but easier to use if you need access to DartN
ode's. | 58 * slower, not actually unit test, but easier to use if you need access to DartN
ode's. |
56 */ | 59 */ |
57 public class TypeAnalyzerCompilerTest extends CompilerTestCase { | 60 public class TypeAnalyzerCompilerTest extends CompilerTestCase { |
58 | 61 |
59 /** | 62 /** |
60 * Top-level "main" function should not have parameters. | 63 * Top-level "main" function should not have parameters. |
61 * <p> | 64 * <p> |
62 * http://code.google.com/p/dart/issues/detail?id=3271 | 65 * http://code.google.com/p/dart/issues/detail?id=3271 |
63 */ | 66 */ |
64 public void test_topLevelMainFunction() throws Exception { | 67 public void test_topLevelMainFunction() throws Exception { |
65 AnalyzeLibraryResult libraryResult = analyzeLibrary( | 68 AnalyzeLibraryResult libraryResult = analyzeLibrary( |
66 "// filler filler filler filler filler filler filler filler filler fille
r", | 69 "// filler filler filler filler filler filler filler filler filler fille
r", |
67 "main(var p) {}", | 70 "main(var p) {}", |
68 "class A {", | 71 "class A {", |
69 " main(var p) {}", | 72 " main(var p) {}", |
70 "}", | 73 "}", |
71 ""); | 74 ""); |
72 assertErrors( | 75 assertErrors( |
73 libraryResult.getErrors(), | 76 libraryResult.getErrors(), |
74 errEx(ResolverErrorCode.MAIN_FUNCTION_PARAMETERS, 2, 1, 4)); | 77 errEx(ResolverErrorCode.MAIN_FUNCTION_PARAMETERS, 2, 1, 4)); |
75 } | 78 } |
76 | 79 |
77 /** | 80 /** |
| 81 * We should support resolving to the method "call". |
| 82 * <p> |
| 83 * http://code.google.com/p/dart/issues/detail?id=1355 |
| 84 */ |
| 85 public void test_resolveCallMethod() throws Exception { |
| 86 AnalyzeLibraryResult libraryResult = analyzeLibrary( |
| 87 "// filler filler filler filler filler filler filler filler filler fille
r", |
| 88 "class A {", |
| 89 " call() => 42;", |
| 90 "}", |
| 91 "main() {", |
| 92 " A a = new A();", |
| 93 " a();", |
| 94 "}", |
| 95 ""); |
| 96 assertErrors(libraryResult.getErrors()); |
| 97 // find a() |
| 98 DartIdentifier aVar = findNode(libraryResult, DartIdentifier.class, "a()"); |
| 99 assertNotNull(aVar); |
| 100 DartUnqualifiedInvocation invocation = (DartUnqualifiedInvocation) aVar.getP
arent(); |
| 101 // analyze a() element |
| 102 MethodElement element = (MethodElement) invocation.getElement(); |
| 103 assertNotNull(element); |
| 104 assertEquals("call", element.getName()); |
| 105 assertEquals( |
| 106 libraryResult.source.indexOf("call() => 42"), |
| 107 element.getNameLocation().getOffset()); |
| 108 } |
| 109 |
| 110 /** |
78 * It is a compile-time error if a typedef refers to itself via a chain of ref
erences that does | 111 * It is a compile-time error if a typedef refers to itself via a chain of ref
erences that does |
79 * not include a class or interface type. | 112 * not include a class or interface type. |
80 * <p> | 113 * <p> |
81 * http://code.google.com/p/dart/issues/detail?id=3534 | 114 * http://code.google.com/p/dart/issues/detail?id=3534 |
82 */ | 115 */ |
83 public void test_functionTypeAlias_selfRerences_direct() throws Exception { | 116 public void test_functionTypeAlias_selfRerences_direct() throws Exception { |
84 AnalyzeLibraryResult libraryResult = analyzeLibrary( | 117 AnalyzeLibraryResult libraryResult = analyzeLibrary( |
85 "// filler filler filler filler filler filler filler filler filler fille
r", | 118 "// filler filler filler filler filler filler filler filler filler fille
r", |
86 "typedef A A();", | 119 "typedef A A();", |
87 "typedef B(B b);", | 120 "typedef B(B b);", |
(...skipping 2518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2606 assertErrors( | 2639 assertErrors( |
2607 libraryResult.getErrors(), | 2640 libraryResult.getErrors(), |
2608 errEx(TypeErrorCode.INTERFACE_HAS_NO_METHOD_NAMED, 6, 5, 1), | 2641 errEx(TypeErrorCode.INTERFACE_HAS_NO_METHOD_NAMED, 6, 5, 1), |
2609 errEx(TypeErrorCode.CANNOT_BE_RESOLVED, 7, 3, 1), | 2642 errEx(TypeErrorCode.CANNOT_BE_RESOLVED, 7, 3, 1), |
2610 errEx(TypeErrorCode.CANNOT_BE_RESOLVED, 8, 4, 2)); | 2643 errEx(TypeErrorCode.CANNOT_BE_RESOLVED, 8, 4, 2)); |
2611 } | 2644 } |
2612 | 2645 |
2613 private AnalyzeLibraryResult analyzeLibrary(String... lines) throws Exception
{ | 2646 private AnalyzeLibraryResult analyzeLibrary(String... lines) throws Exception
{ |
2614 return analyzeLibrary(getName(), makeCode(lines)); | 2647 return analyzeLibrary(getName(), makeCode(lines)); |
2615 } | 2648 } |
| 2649 |
| 2650 private static <T extends DartNode> T findNode( |
| 2651 AnalyzeLibraryResult libraryResult, |
| 2652 final Class<T> clazz, |
| 2653 String pattern) { |
| 2654 final int index = libraryResult.source.indexOf(pattern); |
| 2655 assertTrue(index != -1); |
| 2656 final AtomicReference<T> result = new AtomicReference<T>(); |
| 2657 Iterator<DartUnit> unitsIterator = libraryResult.getLibraryUnitResult().getU
nits().iterator(); |
| 2658 unitsIterator.next(); |
| 2659 DartUnit unit = unitsIterator.next(); |
| 2660 unit.accept(new ASTVisitor<Void>() { |
| 2661 @Override |
| 2662 @SuppressWarnings("unchecked") |
| 2663 public Void visitNode(DartNode node) { |
| 2664 SourceInfo sourceInfo = node.getSourceInfo(); |
| 2665 if (sourceInfo.getOffset() <= index |
| 2666 && index < sourceInfo.getEnd() |
| 2667 && clazz.isInstance(node)) { |
| 2668 result.set((T) node); |
| 2669 } |
| 2670 return super.visitNode(node); |
| 2671 } |
| 2672 }); |
| 2673 return result.get(); |
| 2674 } |
2616 } | 2675 } |
OLD | NEW |