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

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

Issue 10661022: Issue 3752. Support for @override annotations (as structured doc comments) (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 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) 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.common.collect.Lists;
7 import com.google.dart.compiler.CompilerTestCase; 8 import com.google.dart.compiler.CompilerTestCase;
8 import com.google.dart.compiler.DartCompilerListener; 9 import com.google.dart.compiler.DartCompilerListener;
9 import com.google.dart.compiler.Source; 10 import com.google.dart.compiler.Source;
11 import com.google.dart.compiler.ast.DartComment;
10 import com.google.dart.compiler.ast.DartDeclaration; 12 import com.google.dart.compiler.ast.DartDeclaration;
11 import com.google.dart.compiler.ast.DartNode; 13 import com.google.dart.compiler.ast.DartNode;
12 import com.google.dart.compiler.ast.DartUnit; 14 import com.google.dart.compiler.ast.DartUnit;
15 import com.google.dart.compiler.common.SourceInfo;
13 16
14 import java.util.ArrayList;
15 import java.util.List; 17 import java.util.List;
16 18
17 /** 19 /**
18 * Tests to ensure the scanner is correctly recording comments, as defined 20 * Tests to ensure the scanner is correctly recording comments, as defined
19 * in the javadoc for <code>DartScanner.recordCommentLocation().</code> 21 * in the javadoc for <code>DartScanner.recordCommentLocation().</code>
20 */ 22 */
21 public class CommentTest extends CompilerTestCase { 23 public class CommentTest extends CompilerTestCase {
22
23 /**
24 * A parser context that uses its own scanner.
25 */
26 class CommentParserContext extends DartScannerParserContext {
27
28 CommentParserContext(Source source, String sourceCode,
29 DartCompilerListener listener) {
30 super(source, sourceCode, listener);
31 }
32
33 @Override
34 protected DartScanner createScanner(String sourceCode) {
35 return new CommentScanner(sourceCode);
36 }
37 }
38
39 /**
40 * A specialized scanner that records comment locations. It would have been
41 * more natural to use the parser context to record comments, but the
42 * scanner doesn't know about the context.
43 */
44 class CommentScanner extends DartScanner {
45
46 CommentScanner(String sourceCode) {
47 super(sourceCode);
48 }
49
50 @Override
51 protected void recordCommentLocation(int start, int stop, int line, int col) {
52 int size = commentLocs.size();
53 if (size > 0) {
54 // check for duplicates
55 int[] loc = commentLocs.get(size - 1);
56 // use <= to allow parser to back up more than one token
57 if (start <= loc[0] && stop <= loc[1]) {
58 return;
59 }
60 }
61 commentLocs.add(new int[]{start, stop});
62 }
63 }
64
65 private List<int[]> commentLocs = new ArrayList<int[]>();
66 private String source; 24 private String source;
67 25
68 private static String[] EXPECTED001 = {"/*\n * Beginning comment\n */", 26 private static String[] EXPECTED001 = {"/*\n * Beginning comment\n */",
69 "// line comment", "// another", "/**/", "//", "/*/*nested*/*/", 27 "// line comment", "// another", "/**/", "//", "/*/*nested*/*/",
70 }; 28 };
71 private static String[] EXPECTED002 = {"/*\n*\n //comment\nX Y"}; 29 private static String[] EXPECTED002 = {"/*\n*\n //comment\nX Y"};
72 30
73 public void test001() { 31 public void test001() {
74 parseUnit("Comments.dart"); 32 DartUnit unit = parseUnit("Comments.dart");
75 compareComments(EXPECTED001); 33 compareComments(unit, EXPECTED001);
76 } 34 }
77 35
78 public void test002() { 36 public void test002() {
79 parseUnitErrors("BadCommentNegativeTest.dart", 37 DartUnit unit = parseUnitErrors("BadCommentNegativeTest.dart",
80 "Unexpected token 'ILLEGAL' (expected end of file)", 1, 1); 38 "Unexpected token 'ILLEGAL' (expected end of file)", 1, 1);
81 compareComments(EXPECTED002); 39 compareComments(unit, EXPECTED002);
82 } 40 }
83 41
84 public void test003() { 42 public void test003() {
85 DartUnit unit = parseUnit("Comments2.dart"); 43 DartUnit unit = parseUnit("Comments2.dart");
86 assertDeclComments(unit, "firstMethod", "/** Comments are good. */"); 44 assertDeclComments(unit, "firstMethod", "/** Comments are good. */");
87 assertDeclComments(unit, "secondMethod", null); 45 assertDeclComments(unit, "secondMethod", null);
88 } 46 }
89 47
90 @Override 48 @Override
91 protected ParserContext makeParserContext(Source src, String sourceCode, 49 protected DartParser makeParser(Source src, String sourceCode, DartCompilerLis tener listener) {
92 DartCompilerListener listener) {
93 // initializing source and commentLocs here is a bit of a hack but it
94 // means parseUnit() and parseUnitErrors() do not have to be overridden
95 source = sourceCode; 50 source = sourceCode;
96 commentLocs.clear(); 51 return super.makeParser(src, sourceCode, listener);
97 return new CommentParserContext(src, sourceCode, listener);
98 } 52 }
99 53
100 private List<String> extractComments() { 54 private List<String> extractComments(DartUnit unit) {
101 List<String> comments = new ArrayList<String>(); 55 List<String> comments = Lists.newArrayList();
102 for (int[] loc : commentLocs) { 56 List<DartComment> commentNodes = unit.getComments();
103 comments.add(source.substring(loc[0], loc[1])); 57 for (DartComment commentNode : commentNodes) {
58 SourceInfo sourceInfo = commentNode.getSourceInfo();
59 String comment = source.substring(sourceInfo.getOffset(), sourceInfo.getEn d());
60 comments.add(comment);
104 } 61 }
105 return comments; 62 return comments;
106 } 63 }
107 64
108 private void compareComments(String[] expected) { 65 private void compareComments(DartUnit unit, String[] expected) {
109 List<String> comments = extractComments(); 66 List<String> comments = extractComments(unit);
110 assertEquals(expected.length, comments.size()); 67 assertEquals(expected.length, comments.size());
111 for (int i = 0; i < expected.length; i++) { 68 for (int i = 0; i < expected.length; i++) {
112 assertEquals(expected[i], comments.get(i)); 69 assertEquals(expected[i], comments.get(i));
113 } 70 }
114 } 71 }
115 72
116 private void assertDeclComments(DartUnit unit, String name, String comments) { 73 private void assertDeclComments(DartUnit unit, String name, String comments) {
117 for (DartNode node : unit.getTopLevelNodes()) { 74 for (DartNode node : unit.getTopLevelNodes()) {
118 if (node instanceof DartDeclaration && node.getElement() != null 75 if (node instanceof DartDeclaration && node.getElement() != null
119 && name.equals(node.getElement().getOriginalName())) { 76 && name.equals(node.getElement().getOriginalName())) {
120 DartDeclaration<?> decl = (DartDeclaration<?>)node; 77 DartDeclaration<?> decl = (DartDeclaration<?>)node;
121 String nodeComments = null; 78 String nodeComments = null;
122 79
123 if (decl.getDartDoc() != null) { 80 if (decl.getDartDoc() != null) {
124 nodeComments = decl.getDartDoc().toSource(); 81 nodeComments = decl.getDartDoc().toSource();
125 } 82 }
126 83
127 assertEquals(comments, nodeComments); 84 assertEquals(comments, nodeComments);
128 } 85 }
129 } 86 }
130 } 87 }
131 } 88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698