OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
Brian Wilkerson
2012/06/25 14:24:38
nit: copyright year
scheglov
2012/06/26 19:46:34
Done.
| |
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.parser; | 5 package com.google.dart.compiler.parser; |
6 | 6 |
7 import com.google.dart.compiler.CompilerTestCase; | 7 import com.google.dart.compiler.CompilerTestCase; |
8 import com.google.dart.compiler.DartCompilerListener; | 8 import com.google.dart.compiler.DartCompilerListener; |
9 import com.google.dart.compiler.Source; | 9 import com.google.dart.compiler.Source; |
10 import com.google.dart.compiler.ast.DartComment; | 10 import com.google.dart.compiler.ast.DartComment; |
11 import com.google.dart.compiler.ast.DartUnit; | 11 import com.google.dart.compiler.ast.DartUnit; |
12 import com.google.dart.compiler.common.SourceInfo; | 12 import com.google.dart.compiler.common.SourceInfo; |
13 | 13 |
14 import java.util.ArrayList; | 14 import java.util.ArrayList; |
15 import java.util.List; | 15 import java.util.List; |
16 | 16 |
17 /** | 17 /** |
18 * Tests for the parser, which simply assert that valid source units parse | 18 * Tests for the parser, which simply assert that valid source units parse |
19 * correctly. All tests invoking {@code parseUnit} are designed such that | 19 * correctly. All tests invoking {@code parseUnit} are designed such that |
20 * they will throw an exception if anything goes wrong in the parser. | 20 * they will throw an exception if anything goes wrong in the parser. |
21 */ | 21 */ |
22 public class CPParserTest extends CompilerTestCase { | 22 public class CPParserTest extends CompilerTestCase { |
23 | 23 |
24 private static String[] EXPECTED001 = {"/*\n * Beginning comment\n */", | 24 private static String[] EXPECTED001 = {"/*\n * Beginning comment\n */", |
25 "// line comment", "// another", "/**/", "//", "/*/*nested*/*/", | 25 "// line comment", "// another", "/**/", "//", "/*/*nested*/*/", |
26 }; | 26 }; |
27 private static String[] EXPECTED002 = {"/*\n*\n //comment\nX Y"}; | 27 private static String[] EXPECTED002 = {"/*\n*\n //comment\nX Y"}; |
28 | 28 |
29 private String source; | 29 private String source; |
30 private CommentPreservingParser parser; | 30 |
31 | |
32 @Override | |
33 protected DartParser makeParser(Source src, String sourceCode, DartCompilerLis tener listener) { | |
34 source = sourceCode; | |
35 return super.makeParser(src, sourceCode, listener); | |
36 } | |
31 | 37 |
32 public void test001() { | 38 public void test001() { |
33 DartUnit unit = parseUnit("Comments.dart"); | 39 DartUnit unit = parseUnit("Comments.dart"); |
34 compareComments(unit.getComments(), EXPECTED001); | 40 compareComments(unit.getComments(), EXPECTED001); |
35 } | 41 } |
36 | 42 |
37 public void test002() { | 43 public void test002() { |
38 DartUnit unit = parseUnitErrors("BadCommentNegativeTest.dart", | 44 DartUnit unit = parseUnitErrors("BadCommentNegativeTest.dart", |
39 "Unexpected token 'ILLEGAL' (expected end of file)", 1, 1); | 45 "Unexpected token 'ILLEGAL' (expected end of file)", 1, 1); |
40 compareComments(unit.getComments(), EXPECTED002); | 46 compareComments(unit.getComments(), EXPECTED002); |
41 } | 47 } |
42 | 48 |
43 @Override | |
44 protected DartParser makeParser(ParserContext context) { | |
45 parser = new CommentPreservingParser(context, false); | |
46 return parser; | |
47 } | |
48 | |
49 @Override | |
50 protected ParserContext makeParserContext(Source src, String sourceCode, | |
51 DartCompilerListener listener) { | |
52 this.source = sourceCode; | |
53 return CommentPreservingParser.createContext(src, sourceCode, listener); | |
54 } | |
55 | |
56 private List<String> extractComments(List<DartComment> cms) { | 49 private List<String> extractComments(List<DartComment> cms) { |
57 List<String> comments = new ArrayList<String>(); | 50 List<String> comments = new ArrayList<String>(); |
58 for (DartComment cm : cms) { | 51 for (DartComment cm : cms) { |
59 SourceInfo sourceInfo = cm.getSourceInfo(); | 52 SourceInfo sourceInfo = cm.getSourceInfo(); |
60 comments.add(source.substring(sourceInfo.getOffset(), sourceInfo.getOffset () | 53 comments.add(source.substring(sourceInfo.getOffset(), sourceInfo.getEnd()) ); |
61 + sourceInfo.getLength())); | |
62 } | 54 } |
63 return comments; | 55 return comments; |
64 } | 56 } |
65 | 57 |
66 private void compareComments(List<DartComment> cms, String[] expected) { | 58 private void compareComments(List<DartComment> cms, String[] expected) { |
67 List<String> comments = extractComments(cms); | 59 List<String> comments = extractComments(cms); |
68 assertEquals(expected.length, comments.size()); | 60 assertEquals(expected.length, comments.size()); |
69 for (int i = 0; i < expected.length; i++) { | 61 for (int i = 0; i < expected.length; i++) { |
70 assertEquals(expected[i], comments.get(i)); | 62 assertEquals(expected[i], comments.get(i)); |
71 } | 63 } |
72 } | 64 } |
73 | 65 |
74 } | 66 } |
OLD | NEW |