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 package com.google.dart.compiler; | 5 package com.google.dart.compiler; |
6 | 6 |
7 import static com.google.dart.compiler.common.ErrorExpectation.assertErrors; | 7 import static com.google.dart.compiler.common.ErrorExpectation.assertErrors; |
8 | 8 |
9 import com.google.common.collect.Lists; | 9 import com.google.common.collect.Lists; |
10 import com.google.common.collect.Maps; | 10 import com.google.common.collect.Maps; |
11 import com.google.common.collect.Sets; | 11 import com.google.common.collect.Sets; |
12 import com.google.dart.compiler.CommandLineOptions.CompilerOptions; | 12 import com.google.dart.compiler.CommandLineOptions.CompilerOptions; |
13 import com.google.dart.compiler.ast.ASTVisitor; | 13 import com.google.dart.compiler.ast.ASTVisitor; |
14 import com.google.dart.compiler.ast.DartExpression; | 14 import com.google.dart.compiler.ast.DartExpression; |
15 import com.google.dart.compiler.ast.DartFunctionTypeAlias; | 15 import com.google.dart.compiler.ast.DartFunctionTypeAlias; |
| 16 import com.google.dart.compiler.ast.DartIdentifier; |
16 import com.google.dart.compiler.ast.DartNode; | 17 import com.google.dart.compiler.ast.DartNode; |
17 import com.google.dart.compiler.ast.DartUnit; | 18 import com.google.dart.compiler.ast.DartUnit; |
18 import com.google.dart.compiler.ast.LibraryUnit; | 19 import com.google.dart.compiler.ast.LibraryUnit; |
19 import com.google.dart.compiler.common.ErrorExpectation; | 20 import com.google.dart.compiler.common.ErrorExpectation; |
20 import com.google.dart.compiler.common.SourceInfo; | 21 import com.google.dart.compiler.common.SourceInfo; |
21 import com.google.dart.compiler.parser.DartParser; | 22 import com.google.dart.compiler.parser.DartParser; |
22 import com.google.dart.compiler.parser.DartParserRunner; | 23 import com.google.dart.compiler.parser.DartParserRunner; |
| 24 import com.google.dart.compiler.resolver.Element; |
| 25 import com.google.dart.compiler.type.Type; |
| 26 import com.google.dart.compiler.type.TypeKind; |
23 | 27 |
24 import junit.framework.TestCase; | 28 import junit.framework.TestCase; |
25 | 29 |
26 import java.io.IOException; | 30 import java.io.IOException; |
27 import java.io.InputStreamReader; | 31 import java.io.InputStreamReader; |
28 import java.io.Reader; | 32 import java.io.Reader; |
29 import java.net.URI; | 33 import java.net.URI; |
30 import java.net.URL; | 34 import java.net.URL; |
31 import java.util.List; | 35 import java.util.List; |
32 import java.util.Map; | 36 import java.util.Map; |
33 | 37 |
34 /** | 38 /** |
35 * Base class for compiler tests, with helpful utility methods. | 39 * Base class for compiler tests, with helpful utility methods. |
36 */ | 40 */ |
37 public abstract class CompilerTestCase extends TestCase { | 41 public abstract class CompilerTestCase extends TestCase { |
38 | 42 |
39 private static final String UTF8 = "UTF-8"; | 43 private static final String UTF8 = "UTF-8"; |
40 protected CompilerConfiguration compilerConfiguration; | 44 protected CompilerConfiguration compilerConfiguration; |
| 45 protected DartUnit testUnit; |
41 | 46 |
42 /** | 47 /** |
43 * Instance of {@link CompilerConfiguration} for incremental check-only compil
ation. | 48 * Instance of {@link CompilerConfiguration} for incremental check-only compil
ation. |
44 */ | 49 */ |
45 protected static final CompilerConfiguration CHECK_ONLY_CONFIGURATION = | 50 protected static final CompilerConfiguration CHECK_ONLY_CONFIGURATION = |
46 new DefaultCompilerConfiguration(new CompilerOptions()) { | 51 new DefaultCompilerConfiguration(new CompilerOptions()) { |
47 @Override | 52 @Override |
48 public boolean incremental() { | 53 public boolean incremental() { |
49 return true; | 54 return true; |
50 } | 55 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 169 } |
165 return buf.toString(); | 170 return buf.toString(); |
166 } | 171 } |
167 | 172 |
168 @Override | 173 @Override |
169 protected void setUp() throws Exception { | 174 protected void setUp() throws Exception { |
170 super.setUp(); | 175 super.setUp(); |
171 compilerConfiguration = CHECK_ONLY_CONFIGURATION; | 176 compilerConfiguration = CHECK_ONLY_CONFIGURATION; |
172 } | 177 } |
173 | 178 |
| 179 @Override |
| 180 protected void tearDown() throws Exception { |
| 181 compilerConfiguration = null; |
| 182 testUnit = null; |
| 183 super.tearDown(); |
| 184 } |
| 185 |
174 protected AnalyzeLibraryResult analyzeLibrary(String... lines) throws Exceptio
n { | 186 protected AnalyzeLibraryResult analyzeLibrary(String... lines) throws Exceptio
n { |
175 return analyzeLibrary(getName(), makeCode(lines)); | 187 String name = getName(); |
| 188 AnalyzeLibraryResult libraryResult = analyzeLibrary(name, makeCode(lines)); |
| 189 testUnit = libraryResult.getLibraryUnitResult().getUnit(name); |
| 190 return libraryResult; |
176 } | 191 } |
177 | 192 |
178 /** | 193 /** |
179 * Simulate running {@code analyzeLibrary} the way the IDE will. | 194 * Simulate running {@code analyzeLibrary} the way the IDE will. |
180 * <p> | 195 * <p> |
181 * <b>Note:</b> if the IDE changes how it calls analyzeLibrary, this should | 196 * <b>Note:</b> if the IDE changes how it calls analyzeLibrary, this should |
182 * be changed to match. | 197 * be changed to match. |
183 * | 198 * |
184 * @param name the name to use for the source file | 199 * @param name the name to use for the source file |
185 * @param code the Dart code to parse/analyze | 200 * @param code the Dart code to parse/analyze |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 return null; | 409 return null; |
395 } | 410 } |
396 }); | 411 }); |
397 return result[0]; | 412 return result[0]; |
398 } | 413 } |
399 | 414 |
400 public static String getNodeSource(String code, DartNode node) { | 415 public static String getNodeSource(String code, DartNode node) { |
401 SourceInfo sourceInfo = node.getSourceInfo(); | 416 SourceInfo sourceInfo = node.getSourceInfo(); |
402 return code.substring(sourceInfo.getOffset(), sourceInfo.getEnd()); | 417 return code.substring(sourceInfo.getOffset(), sourceInfo.getEnd()); |
403 } | 418 } |
| 419 |
| 420 |
| 421 /** |
| 422 * Asserts that {@link Element} with given name has expected type. |
| 423 */ |
| 424 protected static void assertInferredElementTypeString( |
| 425 DartUnit unit, |
| 426 String variableName, |
| 427 String expectedType) { |
| 428 // find element |
| 429 Element element = getNamedElement(unit, variableName); |
| 430 assertNotNull(element); |
| 431 // check type |
| 432 Type actualType = element.getType(); |
| 433 assertEquals(element.getName(), expectedType, actualType.toString()); |
| 434 // should be inferred |
| 435 if (TypeKind.of(actualType) != TypeKind.DYNAMIC) { |
| 436 assertTrue("Should be marked as inferred", actualType.isInferred()); |
| 437 } |
| 438 } |
| 439 |
| 440 /** |
| 441 * @return the {@link Element} with given name, may be <code>null</code>. |
| 442 */ |
| 443 private static Element getNamedElement(DartUnit unit, final String name) { |
| 444 final Element[] result = {null}; |
| 445 unit.accept(new ASTVisitor<Void>() { |
| 446 @Override |
| 447 public Void visitIdentifier(DartIdentifier node) { |
| 448 Element element = node.getElement(); |
| 449 if (element != null && element.getName().equals(name)) { |
| 450 result[0] = element; |
| 451 } |
| 452 return super.visitIdentifier(node); |
| 453 } |
| 454 }); |
| 455 return result[0]; |
| 456 } |
404 } | 457 } |
OLD | NEW |