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

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

Issue 9959054: More error recovery back to the top level for some cases (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Previous patch had problems when JUnit tests added Created 8 years, 8 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 package com.google.dart.compiler.parser;
6
7 import com.google.common.base.Joiner;
8 import com.google.dart.compiler.ast.DartClass;
9 import com.google.dart.compiler.ast.DartFieldDefinition;
10 import com.google.dart.compiler.ast.DartIdentifier;
11 import com.google.dart.compiler.ast.DartMethodDefinition;
12 import com.google.dart.compiler.ast.DartUnit;
13
14 public class ParserRecoveryTest extends AbstractParserTest {
15
16 @Override
17 public void testStringsErrors() {
18 // Implemented elsewhere
19 }
20
21 public void testVarOnMethodDefinition() {
22 // This syntax is illegal, and should produce errors, but since it is a comm on error,
23 // we want to make sure it produce a valid AST for editor users
24 DartUnit unit = parseUnit("phony_var_on_method.dart",
25 Joiner.on("\n").join(
26 "var f1() { return 1;}", // Error, use of var on a method
27 "class A { ",
28 " var f2() { return 1;}", // Error, use of var on a method
29 " f3() { return 2; }",
30 "}"),
31 ParserErrorCode.VAR_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION, 1, 1,
32 ParserErrorCode.VAR_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION, 3, 3);
33 DartMethodDefinition f1 = (DartMethodDefinition)unit.getTopLevelNodes().get( 0);
34 assertEquals("f1", ((DartIdentifier)(f1.getName())).getName());
35 DartClass A = (DartClass)unit.getTopLevelNodes().get(1);
36 DartMethodDefinition f2 = (DartMethodDefinition)(A.getMembers().get(0));
37 assertEquals("f2", ((DartIdentifier)(f2.getName())).getName());
38 // Make sure that parsing continue
39 DartMethodDefinition f3 = (DartMethodDefinition)(A.getMembers().get(1));
40 assertEquals("f3", ((DartIdentifier)(f3.getName())).getName());
41 }
42
43 public void testFinalOnMethodDefinition() {
44 // This syntax is illegal, and should produce errors, but since it is a comm on error,
45 // we want to make sure it produce a valid AST for editor users
46 DartUnit unit = parseUnit("phony_final_on_method.dart",
47 Joiner.on("\n").join(
48 "final f1() {return 1;}", // Error, use of final on a method
49 "class A { ",
50 " final f2() {return 1;}", // Error, use of final on a method
51 " f3() { return 2; }",
52 " final String f4() { return 1; }", // Error, use of final on a me thod
53 "}"),
54 ParserErrorCode.FINAL_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION, 1, 1,
55 ParserErrorCode.FINAL_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION, 3, 3,
56 ParserErrorCode.FINAL_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION, 5, 9);
57 DartMethodDefinition f1 = (DartMethodDefinition)unit.getTopLevelNodes().get( 0);
58 assertEquals("f1", ((DartIdentifier)(f1.getName())).getName());
59 DartClass A = (DartClass)unit.getTopLevelNodes().get(1);
60 DartMethodDefinition f2 = (DartMethodDefinition)(A.getMembers().get(0));
61 assertEquals("f2", ((DartIdentifier)(f2.getName())).getName());
62 DartMethodDefinition f3 = (DartMethodDefinition)(A.getMembers().get(1));
63 assertEquals("f3", ((DartIdentifier)(f3.getName())).getName());
64 DartMethodDefinition f4 = (DartMethodDefinition)(A.getMembers().get(2));
65 assertEquals("f4", ((DartIdentifier)(f4.getName())).getName());
66 }
67
68 public void testRecoverToTopLevel1() {
69 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel1.dart" ,
70 Joiner.on("\n").join(
71 "class A {",
72 " var a;",
73 "", // error - missing right brace
74 "class B { ", // error recover should pick up class B
75 " var b;",
76 "}"));
77 // Make sure class B is still around
78 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
79 assertEquals("A", A.getName().getName());
80 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
81 assertEquals("a", A_a.getFields().get(0).getName().getName());
82 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
83 assertEquals("B", B.getName().getName());
84 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
85 assertEquals("b", B_b.getFields().get(0).getName().getName());
86 }
87
88 public void testRecoverToTopLevel2() {
89 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel2.dart" ,
90 Joiner.on("\n").join(
91 "class A {",
92 " var a;",
93 " var incomplete", // error - missing semicolon
94 "", // error - missing closing brace
95 "class B { ", // error recover should pick up class B
96 " var b;",
97 "}"));
98 // Make sure class B is still around
99 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
100 assertEquals("A", A.getName().getName());
101 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
102 assertEquals("a", A_a.getFields().get(0).getName().getName());
103 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
104 assertEquals("B", B.getName().getName());
105 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
106 assertEquals("b", B_b.getFields().get(0).getName().getName());
107 }
108
109 public void testRecoverToTopLevel3() {
110 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel3.dart" ,
111 Joiner.on("\n").join(
112 "class A {",
113 " var a;",
114 " var incomplete = ", // error - missing value
115 "", // error - missing closing brace
116 "class B { ", // error recover should pick up class B
117 " var b;",
118 "}"));
119 // Make sure class B is still around
120 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
121 assertEquals("A", A.getName().getName());
122 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
123 assertEquals("a", A_a.getFields().get(0).getName().getName());
124 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
125 assertEquals("B", B.getName().getName());
126 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
127 assertEquals("b", B_b.getFields().get(0).getName().getName());
128 }
129
130 public void testRecoverToTopLevel4() {
131 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel4.dart" ,
132 Joiner.on("\n").join(
133 "class A {",
134 " var a;",
135 " incomplete()", // error - missing body
136 "", // error - missing right brace
137 "class B { ", // error recover should pick up class B
138 " var b;",
139 "}"));
140 // Make sure class B is still around
141 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
142 assertEquals("A", A.getName().getName());
143 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
144 assertEquals("a", A_a.getFields().get(0).getName().getName());
145 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
146 assertEquals("B", B.getName().getName());
147 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
148 assertEquals("b", B_b.getFields().get(0).getName().getName());
149 }
150
151 public void testRecoverToTopLevel5() {
152 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel5.dart" ,
153 Joiner.on("\n").join(
154 "class A {",
155 " var a;",
156 " incomplete()", // error - missing body
157 "}",
158 "class B { ", // error recover should pick up class B
159 " var b;",
160 "}"));
161 // Make sure class B is still around
162 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
163 assertEquals("A", A.getName().getName());
164 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
165 assertEquals("a", A_a.getFields().get(0).getName().getName());
166 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
167 assertEquals("B", B.getName().getName());
168 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
169 assertEquals("b", B_b.getFields().get(0).getName().getName());
170 }
171
172 public void testRecoverToTopLevel6() {
173 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel6.dart" ,
174 Joiner.on("\n").join(
175 "class A {",
176 " var a;",
177 " method() { instance.field", // error - missing semicolon, missin g rbrace
178 "}",
179 "class B { ", // error recover should pick up class B
180 " var b;",
181 "}"));
182 // Make sure class B is still around
183 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
184 assertEquals("A", A.getName().getName());
185 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
186 assertEquals("a", A_a.getFields().get(0).getName().getName());
187 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
188 assertEquals("B", B.getName().getName());
189 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
190 assertEquals("b", B_b.getFields().get(0).getName().getName());
191 }
192
193 public void testRecoverToTopLevel7() {
194 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel7.dart" ,
195 Joiner.on("\n").join(
196 "class A {",
197 " var a;",
198 " method() { instance.field", // error - missing semicolon, missin g rbrace
199 "", // error missing rbrace
200 "class B { ", // error recover should pick up class B
201 " var b;",
202 "}"));
203 // Make sure class B is still around
204 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
205 assertEquals("A", A.getName().getName());
206 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
207 assertEquals("a", A_a.getFields().get(0).getName().getName());
208 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
209 assertEquals("B", B.getName().getName());
210 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
211 assertEquals("b", B_b.getFields().get(0).getName().getName());
212 }
213
214 public void testRecoverToTopLevel8() {
215 DartUnit unit = parseUnitUnspecifiedErrors("phony_recover_to_toplevel8.dart" ,
216 Joiner.on("\n").join(
217 "class A {",
218 " var a;",
219 " method() {",
220 " B instance ;",
221 " instance.", // error - missing semicolon, missing rbrace
222 "", // error missing rbrace
223 "class B { ", // error recover should pick up class B
224 " var b;",
225 "}"));
226 // Make sure class B is still around
227 DartClass A = (DartClass)unit.getTopLevelNodes().get(0);
228 assertEquals("A", A.getName().getName());
229 DartFieldDefinition A_a = (DartFieldDefinition)A.getMembers().get(0);
230 assertEquals("a", A_a.getFields().get(0).getName().getName());
231 DartClass B = (DartClass)unit.getTopLevelNodes().get(1);
232 assertEquals("B", B.getName().getName());
233 DartFieldDefinition B_b = (DartFieldDefinition)B.getMembers().get(0);
234 assertEquals("b", B_b.getFields().get(0).getName().getName());
235 }
236
237 public void testReservedWordClass() {
238 DartUnit unit = parseUnitUnspecifiedErrors("phony_reserved_word_class",
239 Joiner.on("\n").join(
240 "class foo {}",
241 "main() {",
242 " int class = 10;",
243 " print(\"class = $class\");",
244 "}",
245 "class bar {}"));
zundel 2012/04/02 20:02:59 I added another top level class definition to assu
246 DartClass foo = (DartClass)unit.getTopLevelNodes().get(0);
247 assertEquals("foo", foo.getName().getName());
248 DartMethodDefinition mainMethod = (DartMethodDefinition)unit.getTopLevelNode s().get(1);
249 assertEquals("main", ((DartIdentifier)mainMethod.getName()).getName());
250 // The recovery on 'int class' closes the main method, assuming int class = 10 is a
251 // new toplevel so 'print' ends up as a bogus top level node.
252 DartClass bar = (DartClass)unit.getTopLevelNodes().get(3);
253 assertEquals("bar", bar.getName().getName());
254 }
255 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698