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 |
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 package com.google.dart.compiler.parser; | 4 package com.google.dart.compiler.parser; |
5 | 5 |
6 import com.google.common.base.Joiner; | 6 import com.google.common.base.Joiner; |
7 import com.google.dart.compiler.CompilerTestCase; | 7 import com.google.dart.compiler.CompilerTestCase; |
8 import com.google.dart.compiler.ast.DartIdentifier; | 8 import com.google.dart.compiler.ast.DartIdentifier; |
9 import com.google.dart.compiler.ast.DartMethodDefinition; | 9 import com.google.dart.compiler.ast.DartMethodDefinition; |
10 import com.google.dart.compiler.ast.DartUnit; | 10 import com.google.dart.compiler.ast.DartUnit; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 ParserErrorCode.DISALLOWED_FACTORY_KEYWORD.getMessage(), | 134 ParserErrorCode.DISALLOWED_FACTORY_KEYWORD.getMessage(), |
135 1, | 135 1, |
136 1); | 136 1); |
137 DartMethodDefinition factory = (DartMethodDefinition) unit.getTopLevelNodes(
).get(0); | 137 DartMethodDefinition factory = (DartMethodDefinition) unit.getTopLevelNodes(
).get(0); |
138 assertNotNull(factory); | 138 assertNotNull(factory); |
139 // this factory has name, which is allowed for normal method | 139 // this factory has name, which is allowed for normal method |
140 assertEquals(true, factory.getName() instanceof DartIdentifier); | 140 assertEquals(true, factory.getName() instanceof DartIdentifier); |
141 assertEquals("foo", ((DartIdentifier) factory.getName()).getTargetName()); | 141 assertEquals("foo", ((DartIdentifier) factory.getName()).getTargetName()); |
142 } | 142 } |
143 | 143 |
144 /** | |
145 * Language specification requires that factory should be declared in class. H
owever declaring | |
146 * factory on top level should not cause exceptions in compiler. To ensure thi
s we parse top level | |
147 * factory into normal {@link DartMethodDefinition}. | |
148 * <p> | |
149 * http://code.google.com/p/dart/issues/detail?id=345 | |
150 */ | |
151 public void test_badTopLevelFactory_withTypeParameters() { | |
152 DartUnit unit = | |
153 parseSourceUnitErrors( | |
154 "factory foo<T>() {}", | |
155 ParserErrorCode.DISALLOWED_FACTORY_KEYWORD.getMessage(), | |
156 1, | |
157 1); | |
158 DartMethodDefinition factory = (DartMethodDefinition) unit.getTopLevelNodes(
).get(0); | |
159 assertNotNull(factory); | |
160 // normal method requires name, so we provide some name | |
161 assertEquals(true, factory.getName() instanceof DartIdentifier); | |
162 assertEquals("foo<T>", ((DartIdentifier) factory.getName()).getTargetName())
; | |
163 } | |
164 | |
165 public void test_defaultParameterValue_inInterfaceMethod() { | 144 public void test_defaultParameterValue_inInterfaceMethod() { |
166 parseExpectErrors( | 145 parseExpectErrors( |
167 "interface A { f(int a, [int b = 12345]); }", | 146 "interface A { f(int a, [int b = 12345]); }", |
168 errEx(ParserErrorCode.DEFAULT_VALUE_CAN_NOT_BE_SPECIFIED_IN_INTERFACE, 1
, 33, 5)); | 147 errEx(ParserErrorCode.DEFAULT_VALUE_CAN_NOT_BE_SPECIFIED_IN_INTERFACE, 1
, 33, 5)); |
169 } | 148 } |
170 | 149 |
171 public void test_defaultParameterValue_inAbstractMethod() { | 150 public void test_defaultParameterValue_inAbstractMethod() { |
172 parseExpectErrors( | 151 parseExpectErrors( |
173 "class A { abstract f(int a, [int b = 12345, int c]); }", | 152 "class A { abstract f(int a, [int b = 12345, int c]); }", |
174 errEx(ParserErrorCode.DEFAULT_VALUE_CAN_NOT_BE_SPECIFIED_IN_ABSTRACT, 1,
38, 5)); | 153 errEx(ParserErrorCode.DEFAULT_VALUE_CAN_NOT_BE_SPECIFIED_IN_ABSTRACT, 1,
38, 5)); |
(...skipping 27 matching lines...) Expand all Loading... |
202 * If keyword "extends" is mistyped in type parameters declaration, we should
report about this | 181 * If keyword "extends" is mistyped in type parameters declaration, we should
report about this |
203 * and then recover correctly. | 182 * and then recover correctly. |
204 * <p> | 183 * <p> |
205 * http://code.google.com/p/dart/issues/detail?id=341 | 184 * http://code.google.com/p/dart/issues/detail?id=341 |
206 */ | 185 */ |
207 public void test_parseTypeParameter_expectedExtends_mistypedExtends() throws E
xception { | 186 public void test_parseTypeParameter_expectedExtends_mistypedExtends() throws E
xception { |
208 DartParserRunner parserRunner = | 187 DartParserRunner parserRunner = |
209 parseSource(Joiner.on("\n").join( | 188 parseSource(Joiner.on("\n").join( |
210 "class A {", | 189 "class A {", |
211 "}", | 190 "}", |
212 "class B {", | 191 "class B<X ex> {", |
213 " factory B<X ex>(){}", | 192 "}", |
214 " factory B<X extneds A>(){}", | 193 "class C<X extneds A> {", |
215 " factory B<X extneds A, Y extends A>(){}", | 194 "}", |
| 195 "class D<X extneds A, Y extends A> {", |
216 "}")); | 196 "}")); |
217 // check expected errors | 197 // check expected errors |
218 assertErrors( | 198 assertErrors( |
219 parserRunner.getErrors(), | 199 parserRunner.getErrors(), |
220 errEx(ParserErrorCode.EXPECTED_EXTENDS, 4, 15, 2), | 200 errEx(ParserErrorCode.EXPECTED_EXTENDS, 3, 11, 2), |
221 errEx(ParserErrorCode.EXPECTED_EXTENDS, 5, 15, 7), | 201 errEx(ParserErrorCode.EXPECTED_EXTENDS, 5, 11, 7), |
222 errEx(ParserErrorCode.EXPECTED_EXTENDS, 6, 15, 7)); | 202 errEx(ParserErrorCode.EXPECTED_EXTENDS, 7, 11, 7)); |
| 203 |
223 // check structure of AST | 204 // check structure of AST |
224 DartUnit dartUnit = parserRunner.getDartUnit(); | 205 DartUnit dartUnit = parserRunner.getDartUnit(); |
225 assertEquals( | 206 String expected = |
226 Joiner.on("\n").join( | 207 Joiner.on("\n").join( |
227 "// unit " + getName(), | 208 "// unit " + getName(), |
228 "class A {", | 209 "class A {", |
229 "}", | 210 "}", |
230 "", | 211 "", |
231 "class B {", | 212 "class B<X> {", |
| 213 "}", |
232 "", | 214 "", |
233 " factory B<X>() { }", | 215 "class C<X extends A> {", |
| 216 "}", |
234 "", | 217 "", |
235 " factory B<X extends A>() { }", | 218 "class D<X extends A, Y extends A> {", |
236 "", | 219 "}"); |
237 " factory B<X extends A, Y extends A>() { }", | 220 String actual = dartUnit.toDietSource().trim(); |
238 "}"), | 221 if (!expected.equals(actual)) { |
239 dartUnit.toDietSource().trim()); | 222 System.err.println("Expected:\n" + expected); |
| 223 System.err.println("\nActual:\n" + actual); |
| 224 } |
| 225 assertEquals(expected, actual); |
240 } | 226 } |
241 | 227 |
242 /** | 228 /** |
243 * Type parameters declaration is not finished, stop parsing and restart from
next top level | 229 * Type parameters declaration is not finished, stop parsing and restart from
next top level |
244 * element. | 230 * element. |
245 * <p> | 231 * <p> |
246 * http://code.google.com/p/dart/issues/detail?id=341 | 232 * http://code.google.com/p/dart/issues/detail?id=341 |
247 */ | 233 */ |
248 public void test_parseTypeParameter_unfinishedTypeParameters() throws Exceptio
n { | 234 public void test_parseTypeParameter_unfinishedTypeParameters() throws Exceptio
n { |
249 DartParserRunner parserRunner = | 235 DartParserRunner parserRunner = |
250 parseSource(Joiner.on("\n").join( | 236 parseSource(Joiner.on("\n").join( |
251 "class ClassWithLongEnoughName {", | 237 "class ClassWithLongEnoughName {", |
252 "}", | 238 "}", |
253 "class B {", | 239 "class B<X {", |
254 " factory B<X(){}", | |
255 "}", | 240 "}", |
256 "class C {", | 241 "class C {", |
257 "}")); | 242 "}")); |
258 // check expected errors | 243 // check expected errors |
259 assertErrors( | 244 assertErrors( |
260 parserRunner.getErrors(), | 245 parserRunner.getErrors(), |
261 errEx(ParserErrorCode.EXPECTED_EXTENDS, 4, 14, 1), | 246 errEx(ParserErrorCode.EXPECTED_EXTENDS, 3, 11, 1), |
262 errEx(ParserErrorCode.SKIPPED_SOURCE, 4, 14, 6)); | 247 errEx(ParserErrorCode.SKIPPED_SOURCE, 3, 11, 3)); |
263 // check structure of AST | 248 // check structure of AST |
264 DartUnit dartUnit = parserRunner.getDartUnit(); | 249 DartUnit dartUnit = parserRunner.getDartUnit(); |
265 assertEquals( | 250 assertEquals( |
266 Joiner.on("\n").join( | 251 Joiner.on("\n").join( |
267 "// unit " + getName(), | 252 "// unit " + getName(), |
268 "class ClassWithLongEnoughName {", | 253 "class ClassWithLongEnoughName {", |
269 "}", | 254 "}", |
270 "", | 255 "", |
271 "class C {", | 256 "class C {", |
272 "}"), | 257 "}"), |
273 dartUnit.toDietSource().trim()); | 258 dartUnit.toDietSource().trim()); |
274 } | 259 } |
275 | 260 |
276 /** | 261 /** |
277 * Type parameters declaration is not finished, next top level element beginni
ng encountered. May | 262 * Type parameters declaration is not finished, next top level element beginni
ng encountered. May |
278 * be use just types new class declaration before existing one. | 263 * be use just types new class declaration before existing one. |
279 * <p> | 264 * <p> |
280 * http://code.google.com/p/dart/issues/detail?id=341 | 265 * http://code.google.com/p/dart/issues/detail?id=341 |
281 */ | 266 */ |
282 public void test_parseTypeParameter_nextTopLevelInTheMiddle() throws Exception
{ | 267 public void test_parseTypeParameter_nextTopLevelInTheMiddle() throws Exception
{ |
283 DartParserRunner parserRunner = | 268 DartParserRunner parserRunner = |
284 parseSource(Joiner.on("\n").join( | 269 parseSource(Joiner.on("\n").join( |
285 "class ClassWithLongEnoughName {", | 270 "class ClassWithLongEnoughName {", |
286 "}", | 271 "}", |
287 "class B {", | 272 "class B<X", |
288 " factory B<X", | |
289 "class C {", | 273 "class C {", |
290 "}")); | 274 "}")); |
291 // check expected errors | 275 // check expected errors |
292 assertErrors(parserRunner.getErrors(), errEx(ParserErrorCode.SKIPPED_SOURCE,
4, 13, 1)); | 276 assertErrors(parserRunner.getErrors(), errEx(ParserErrorCode.SKIPPED_SOURCE,
3, 9, 1)); |
293 // check structure of AST | 277 // check structure of AST |
294 DartUnit dartUnit = parserRunner.getDartUnit(); | 278 DartUnit dartUnit = parserRunner.getDartUnit(); |
295 assertEquals( | 279 assertEquals( |
296 Joiner.on("\n").join( | 280 Joiner.on("\n").join( |
297 "// unit " + getName(), | 281 "// unit " + getName(), |
298 "class ClassWithLongEnoughName {", | 282 "class ClassWithLongEnoughName {", |
299 "}", | 283 "}", |
300 "", | 284 "", |
301 "class C {", | 285 "class C {", |
302 "}"), | 286 "}"), |
303 dartUnit.toDietSource().trim()); | 287 dartUnit.toDietSource().trim()); |
304 } | 288 } |
305 } | 289 } |
OLD | NEW |