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

Side by Side Diff: compiler/javatests/com/google/dart/compiler/CompilerTestCase.java

Issue 11029037: Fix for cascade sections SourceInfo (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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 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;
(...skipping 16 matching lines...) Expand all
27 27
28 import junit.framework.TestCase; 28 import junit.framework.TestCase;
29 29
30 import java.io.IOException; 30 import java.io.IOException;
31 import java.io.InputStreamReader; 31 import java.io.InputStreamReader;
32 import java.io.Reader; 32 import java.io.Reader;
33 import java.net.URI; 33 import java.net.URI;
34 import java.net.URL; 34 import java.net.URL;
35 import java.util.List; 35 import java.util.List;
36 import java.util.Map; 36 import java.util.Map;
37 import java.util.concurrent.atomic.AtomicReference;
37 38
38 /** 39 /**
39 * Base class for compiler tests, with helpful utility methods. 40 * Base class for compiler tests, with helpful utility methods.
40 */ 41 */
41 public abstract class CompilerTestCase extends TestCase { 42 public abstract class CompilerTestCase extends TestCase {
42 43
43 private static final String UTF8 = "UTF-8"; 44 private static final String UTF8 = "UTF-8";
44 protected CompilerConfiguration compilerConfiguration; 45 protected CompilerConfiguration compilerConfiguration;
45 protected String testSource; 46 protected String testSource;
46 protected DartUnit testUnit; 47 protected DartUnit testUnit;
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 // be overriden to do extra checks. 264 // be overriden to do extra checks.
264 URL url = inputUrlFor(getClass(), path); 265 URL url = inputUrlFor(getClass(), path);
265 String source = readUrl(url); 266 String source = readUrl(url);
266 return parseUnit(path, source); 267 return parseUnit(path, source);
267 } 268 }
268 269
269 /** 270 /**
270 * Parse a single compilation unit for the name and source. 271 * Parse a single compilation unit for the name and source.
271 */ 272 */
272 protected DartUnit parseUnit(String srcName, String sourceCode, Object... erro rs) { 273 protected DartUnit parseUnit(String srcName, String sourceCode, Object... erro rs) {
274 testSource = sourceCode;
273 // TODO(jgw): We'll need to fill in the library parameter when testing multi ple units. 275 // TODO(jgw): We'll need to fill in the library parameter when testing multi ple units.
274 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); 276 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null);
275 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er rors); 277 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er rors);
276 DartUnit unit = makeParser(src, sourceCode, listener).parseUnit(); 278 testUnit = makeParser(src, sourceCode, listener).parseUnit();
277 listener.checkAllErrorsReported(); 279 listener.checkAllErrorsReported();
278 return unit; 280 return testUnit;
279 } 281 }
280 282
281 /** 283 /**
282 * Parse a single compilation unit for the name and source. The parse expects some kind of error, 284 * Parse a single compilation unit for the name and source. The parse expects some kind of error,
283 * but isn't picky about the actual contents. This is useful for testing parse r recovery where we 285 * but isn't picky about the actual contents. This is useful for testing parse r recovery where we
284 * don't want to make the test too brittle. 286 * don't want to make the test too brittle.
285 */ 287 */
286 protected DartUnit parseUnitUnspecifiedErrors(String srcName, String sourceCod e) { 288 protected DartUnit parseUnitUnspecifiedErrors(String srcName, String sourceCod e) {
287 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); 289 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null);
288 final List<DartCompilationError> errorsEncountered = Lists.newArrayList(); 290 final List<DartCompilationError> errorsEncountered = Lists.newArrayList();
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 377 }
376 378
377 /** 379 /**
378 * Parses given source and checks parsing problems. 380 * Parses given source and checks parsing problems.
379 */ 381 */
380 protected final void parseExpectWarnings(String code, ErrorExpectation... expe ctedWarnings) { 382 protected final void parseExpectWarnings(String code, ErrorExpectation... expe ctedWarnings) {
381 DartParserRunner runner = DartParserRunner.parse(getName(), code, Integer.M AX_VALUE, true); 383 DartParserRunner runner = DartParserRunner.parse(getName(), code, Integer.M AX_VALUE, true);
382 List<DartCompilationError> errors = runner.getErrors(); 384 List<DartCompilationError> errors = runner.getErrors();
383 assertErrors(errors, expectedWarnings); 385 assertErrors(errors, expectedWarnings);
384 } 386 }
387
385 /** 388 /**
386 * @return the {@link DartExpression} with given source. This is inaccurate ap proach, but good 389 * Find node in {@link #testUnit} parsed form {@link #testSource}.
390 */
391 public <T extends DartNode> T findNode(final Class<T> clazz, String pattern) {
392 final int index = testSource.indexOf(pattern);
393 assertTrue(index != -1);
394 final AtomicReference<T> result = new AtomicReference<T>();
395 testUnit.accept(new ASTVisitor<Void>() {
396 @Override
397 @SuppressWarnings("unchecked")
398 public Void visitNode(DartNode node) {
399 SourceInfo sourceInfo = node.getSourceInfo();
400 if (sourceInfo.getOffset() <= index
401 && index < sourceInfo.getEnd()
402 && clazz.isInstance(node)) {
403 result.set((T) node);
404 }
405 return super.visitNode(node);
406 }
407 });
408 return result.get();
409 }
410
411 /**
412 * @return the {@link DartExpression} with given {@link DartNode#toSource()}. This is inaccurate approach, but good
387 * enough for specific tests. 413 * enough for specific tests.
388 */ 414 */
389 @SuppressWarnings("unchecked") 415 @SuppressWarnings("unchecked")
390 protected static <T extends DartExpression> T findExpression(DartNode rootNode , final String sampleSource) { 416 protected static <T extends DartNode> T findNodeBySource(DartNode rootNode, fi nal String sampleSource) {
391 final DartExpression result[] = new DartExpression[1]; 417 final DartNode result[] = new DartNode[1];
392 rootNode.accept(new ASTVisitor<Void>() { 418 rootNode.accept(new ASTVisitor<Void>() {
393 @Override 419 @Override
394 public Void visitExpression(DartExpression node) { 420 public Void visitNode(DartNode node) {
395 if (node.toSource().equals(sampleSource)) { 421 if (node.toSource().equals(sampleSource)) {
396 result[0] = node; 422 result[0] = node;
397 } 423 }
398 return super.visitExpression(node); 424 return super.visitNode(node);
399 } 425 }
400 }); 426 });
401 return (T) result[0]; 427 return (T) result[0];
402 } 428 }
403 429
404 protected static DartFunctionTypeAlias findTypedef(DartNode rootNode, final St ring name) { 430 protected static DartFunctionTypeAlias findTypedef(DartNode rootNode, final St ring name) {
405 final DartFunctionTypeAlias result[] = new DartFunctionTypeAlias[1]; 431 final DartFunctionTypeAlias result[] = new DartFunctionTypeAlias[1];
406 rootNode.accept(new ASTVisitor<Void>() { 432 rootNode.accept(new ASTVisitor<Void>() {
407 @Override 433 @Override
408 public Void visitFunctionTypeAlias(DartFunctionTypeAlias node) { 434 public Void visitFunctionTypeAlias(DartFunctionTypeAlias node) {
409 if (node.getName().getName().equals(name)) { 435 if (node.getName().getName().equals(name)) {
410 result[0] = node; 436 result[0] = node;
411 } 437 }
412 return null; 438 return null;
413 } 439 }
414 }); 440 });
415 return result[0]; 441 return result[0];
416 } 442 }
417 443
418 public static String getNodeSource(String code, DartNode node) { 444 public static String getNodeSource(String code, DartNode node) {
419 SourceInfo sourceInfo = node.getSourceInfo(); 445 SourceInfo sourceInfo = node.getSourceInfo();
420 return code.substring(sourceInfo.getOffset(), sourceInfo.getEnd()); 446 return code.substring(sourceInfo.getOffset(), sourceInfo.getEnd());
421 } 447 }
422 448
449 /**
450 * Asserts that parsed {@link DartUnit} has {@link DartExpression} with given source and its
451 * {@link SourceInfo} points to the given source.
452 */
453 protected void assertNodeSourceInfo(String nodeSource) {
454 DartNode node = findNode(DartNode.class, nodeSource);
455 assertNotNull(node);
456 SourceInfo sourceInfo = node.getSourceInfo();
457 String actualSource = testSource.substring(sourceInfo.getOffset(), sourceInf o.getEnd());
458 assertEquals(nodeSource, actualSource);
459 }
460
423 461
424 /** 462 /**
425 * Asserts that {@link Element} with given name has expected type. 463 * Asserts that {@link Element} with given name has expected type.
426 */ 464 */
427 protected static void assertInferredElementTypeString( 465 protected static void assertInferredElementTypeString(
428 DartUnit unit, 466 DartUnit unit,
429 String variableName, 467 String variableName,
430 String expectedType) { 468 String expectedType) {
431 // find element 469 // find element
432 Element element = getNamedElement(unit, variableName); 470 Element element = getNamedElement(unit, variableName);
(...skipping 25 matching lines...) Expand all
458 Element element = node.getElement(); 496 Element element = node.getElement();
459 if (element != null && element.getName().equals(name)) { 497 if (element != null && element.getName().equals(name)) {
460 result[0] = element; 498 result[0] = element;
461 } 499 }
462 return super.visitIdentifier(node); 500 return super.visitIdentifier(node);
463 } 501 }
464 }); 502 });
465 return result[0]; 503 return result[0];
466 } 504 }
467 } 505 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698