OLD | NEW |
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; |
| 8 |
7 import com.google.common.collect.Lists; | 9 import com.google.common.collect.Lists; |
8 import com.google.common.collect.Maps; | 10 import com.google.common.collect.Maps; |
| 11 import com.google.common.collect.Sets; |
9 import com.google.dart.compiler.CommandLineOptions.CompilerOptions; | 12 import com.google.dart.compiler.CommandLineOptions.CompilerOptions; |
10 import com.google.dart.compiler.ast.ASTVisitor; | 13 import com.google.dart.compiler.ast.ASTVisitor; |
11 import com.google.dart.compiler.ast.DartExpression; | 14 import com.google.dart.compiler.ast.DartExpression; |
12 import com.google.dart.compiler.ast.DartFunctionTypeAlias; | 15 import com.google.dart.compiler.ast.DartFunctionTypeAlias; |
13 import com.google.dart.compiler.ast.DartNode; | 16 import com.google.dart.compiler.ast.DartNode; |
14 import com.google.dart.compiler.ast.DartUnit; | 17 import com.google.dart.compiler.ast.DartUnit; |
15 import com.google.dart.compiler.ast.LibraryUnit; | 18 import com.google.dart.compiler.ast.LibraryUnit; |
16 import com.google.dart.compiler.common.ErrorExpectation; | 19 import com.google.dart.compiler.common.ErrorExpectation; |
| 20 import com.google.dart.compiler.common.SourceInfo; |
17 import com.google.dart.compiler.parser.DartParser; | 21 import com.google.dart.compiler.parser.DartParser; |
18 import com.google.dart.compiler.parser.DartParserRunner; | 22 import com.google.dart.compiler.parser.DartParserRunner; |
19 import com.google.dart.compiler.parser.DartScannerParserContext; | |
20 import com.google.dart.compiler.parser.ParserContext; | |
21 | |
22 import static com.google.dart.compiler.common.ErrorExpectation.assertErrors; | |
23 | 23 |
24 import junit.framework.TestCase; | 24 import junit.framework.TestCase; |
25 | 25 |
26 import java.io.IOException; | 26 import java.io.IOException; |
27 import java.io.InputStreamReader; | 27 import java.io.InputStreamReader; |
28 import java.io.Reader; | 28 import java.io.Reader; |
29 import java.net.URI; | 29 import java.net.URI; |
30 import java.net.URL; | 30 import java.net.URL; |
31 import java.util.List; | 31 import java.util.List; |
32 import java.util.Map; | 32 import java.util.Map; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 */ | 189 */ |
190 protected AnalyzeLibraryResult analyzeLibrary(String name, String code) | 190 protected AnalyzeLibraryResult analyzeLibrary(String name, String code) |
191 throws Exception { | 191 throws Exception { |
192 AnalyzeLibraryResult result = new AnalyzeLibraryResult(); | 192 AnalyzeLibraryResult result = new AnalyzeLibraryResult(); |
193 result.source = code; | 193 result.source = code; |
194 // Prepare library. | 194 // Prepare library. |
195 MockLibrarySource lib = new MockLibrarySource(); | 195 MockLibrarySource lib = new MockLibrarySource(); |
196 // Prepare unit. | 196 // Prepare unit. |
197 Map<URI, DartUnit> testUnits = Maps.newHashMap(); | 197 Map<URI, DartUnit> testUnits = Maps.newHashMap(); |
198 { | 198 { |
199 DartSourceTest src = new DartSourceTest(name, code, lib); | 199 DartSource src = new DartSourceTest(name, code, lib); |
200 ParserContext context = makeParserContext(src, code, result); | 200 DartUnit unit = makeParser(src, code, result).parseUnit(); |
201 DartUnit unit = makeParser(context).parseUnit(src); | |
202 // Remember unit. | 201 // Remember unit. |
203 lib.addSource(src); | 202 lib.addSource(src); |
204 testUnits.put(src.getUri(), unit); | 203 testUnits.put(src.getUri(), unit); |
205 } | 204 } |
206 DartArtifactProvider provider = new MockArtifactProvider(); | 205 DartArtifactProvider provider = new MockArtifactProvider(); |
207 result.setLibraryUnitResult(DartCompiler.analyzeLibrary( | 206 result.setLibraryUnitResult(DartCompiler.analyzeLibrary( |
208 lib, | 207 lib, |
209 testUnits, | 208 testUnits, |
210 compilerConfiguration, | 209 compilerConfiguration, |
211 provider, | 210 provider, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 return parseUnit(path, source); | 248 return parseUnit(path, source); |
250 } | 249 } |
251 | 250 |
252 /** | 251 /** |
253 * Parse a single compilation unit for the name and source. | 252 * Parse a single compilation unit for the name and source. |
254 */ | 253 */ |
255 protected DartUnit parseUnit(String srcName, String sourceCode, Object... erro
rs) { | 254 protected DartUnit parseUnit(String srcName, String sourceCode, Object... erro
rs) { |
256 // TODO(jgw): We'll need to fill in the library parameter when testing multi
ple units. | 255 // TODO(jgw): We'll need to fill in the library parameter when testing multi
ple units. |
257 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); | 256 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); |
258 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er
rors); | 257 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er
rors); |
259 ParserContext context = makeParserContext(src, sourceCode, listener); | 258 DartUnit unit = makeParser(src, sourceCode, listener).parseUnit(); |
260 DartUnit unit = makeParser(context).parseUnit(src); | |
261 listener.checkAllErrorsReported(); | 259 listener.checkAllErrorsReported(); |
262 return unit; | 260 return unit; |
263 } | 261 } |
264 | 262 |
265 /** | 263 /** |
266 * Parse a single compilation unit for the name and source. The parse expects
some kind of error, | 264 * Parse a single compilation unit for the name and source. The parse expects
some kind of error, |
267 * but isn't picky about the actual contents. This is useful for testing parse
r recovery where we | 265 * but isn't picky about the actual contents. This is useful for testing parse
r recovery where we |
268 * don't want to make the test too brittle. | 266 * don't want to make the test too brittle. |
269 */ | 267 */ |
270 protected DartUnit parseUnitUnspecifiedErrors(String srcName, String sourceCod
e) { | 268 protected DartUnit parseUnitUnspecifiedErrors(String srcName, String sourceCod
e) { |
271 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); | 269 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); |
272 final List<DartCompilationError> errorsEncountered = Lists.newArrayList(); | 270 final List<DartCompilationError> errorsEncountered = Lists.newArrayList(); |
273 DartCompilerListener listener = new DartCompilerListener.Empty() { | 271 DartCompilerListener listener = new DartCompilerListener.Empty() { |
274 @Override | 272 @Override |
275 public void onError(DartCompilationError event) { | 273 public void onError(DartCompilationError event) { |
276 errorsEncountered.add(event); | 274 errorsEncountered.add(event); |
277 } | 275 } |
278 }; | 276 }; |
279 ParserContext context = makeParserContext(src, sourceCode, listener); | 277 DartUnit unit = makeParser(src, sourceCode, listener).parseUnit(); |
280 DartUnit unit = makeParser(context).parseUnit(src); | |
281 assertTrue("Expected some compilation errors, got none.", errorsEncountered.
size() > 0); | 278 assertTrue("Expected some compilation errors, got none.", errorsEncountered.
size() > 0); |
282 return unit; | 279 return unit; |
283 } | 280 } |
284 | 281 |
285 protected DartUnit parseUnitAsSystemLibrary(final String srcName, String sou
rceCode, | 282 protected DartUnit parseUnitAsSystemLibrary(final String srcName, String sou
rceCode, |
286 Object... errors) { | 283 Object... errors) { |
287 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null) { | 284 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null) { |
288 @Override | 285 @Override |
289 public URI getUri() { | 286 public URI getUri() { |
290 return URI.create("dart:core/" + srcName); | 287 return URI.create("dart:core/" + srcName); |
291 } | 288 } |
292 }; | 289 }; |
293 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er
rors); | 290 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er
rors); |
294 ParserContext context = makeParserContext(src, sourceCode, listener); | 291 DartUnit unit = makeParser(src, sourceCode, listener).parseUnit(); |
295 DartUnit unit = makeParser(context).parseUnit(src); | |
296 listener.checkAllErrorsReported(); | 292 listener.checkAllErrorsReported(); |
297 return unit; | 293 return unit; |
298 } | 294 } |
299 | 295 |
300 | 296 |
301 /** | 297 /** |
302 * Parse a single compilation unit with given name and source, and check for a
set of expected errors. | 298 * Parse a single compilation unit with given name and source, and check for a
set of expected errors. |
303 * | 299 * |
304 * @param errors a sequence of errors represented as triples of the form | 300 * @param errors a sequence of errors represented as triples of the form |
305 * (String msg, int line, int column) or | 301 * (String msg, int line, int column) or |
306 * (ErrorCode code, int line, int column) | 302 * (ErrorCode code, int line, int column) |
307 */ | 303 */ |
308 protected DartUnit parseSourceUnitErrors(String sourceCode, Object... errors)
{ | 304 protected DartUnit parseSourceUnitErrors(String sourceCode, Object... errors)
{ |
309 String srcName = "Test.dart"; | 305 String srcName = "Test.dart"; |
310 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); | 306 DartSourceTest src = new DartSourceTest(srcName, sourceCode, null); |
311 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er
rors); | 307 DartCompilerListenerTest listener = new DartCompilerListenerTest(srcName, er
rors); |
312 ParserContext context = makeParserContext(src, sourceCode, listener); | 308 DartUnit unit = makeParser(src, sourceCode, listener).parseUnit(); |
313 DartUnit unit = makeParser(context).parseUnit(src); | |
314 listener.checkAllErrorsReported(); | 309 listener.checkAllErrorsReported(); |
315 return unit; | 310 return unit; |
316 } | 311 } |
317 | 312 |
318 /** | 313 /** |
319 * Parse a single compilation unit for the given input file, and check for a | 314 * Parse a single compilation unit for the given input file, and check for a |
320 * set of expected errors. | 315 * set of expected errors. |
321 * | 316 * |
322 * @param errors a sequence of errors represented as triples of the form | 317 * @param errors a sequence of errors represented as triples of the form |
323 * (String msg, int line, int column) or | 318 * (String msg, int line, int column) or |
324 * (ErrorCode code, int line, int column) | 319 * (ErrorCode code, int line, int column) |
325 */ | 320 */ |
326 protected DartUnit parseUnitErrors(final String path, final Object... errors)
{ | 321 protected DartUnit parseUnitErrors(final String path, final Object... errors)
{ |
327 URL url = inputUrlFor(getClass(), path); | 322 URL url = inputUrlFor(getClass(), path); |
328 String sourceCode = readUrl(url); | 323 String sourceCode = readUrl(url); |
329 // TODO(jgw): We'll need to fill in the library parameter when testing multi
ple units. | 324 // TODO(jgw): We'll need to fill in the library parameter when testing multi
ple units. |
330 DartSourceTest src = new DartSourceTest(path, sourceCode, null); | 325 DartSourceTest src = new DartSourceTest(path, sourceCode, null); |
331 DartCompilerListenerTest listener = new DartCompilerListenerTest(path, error
s); | 326 DartCompilerListenerTest listener = new DartCompilerListenerTest(path, error
s); |
332 ParserContext context = makeParserContext(src, sourceCode, listener); | 327 DartUnit unit = makeParser(src, sourceCode, listener).parseUnit(); |
333 DartUnit unit = makeParser(context).parseUnit(src); | |
334 listener.checkAllErrorsReported(); | 328 listener.checkAllErrorsReported(); |
335 return unit; | 329 return unit; |
336 } | 330 } |
337 | 331 |
338 /** | 332 /** |
339 * Override this method to provide an alternate {@link DartParser}. | 333 * Override this method to provide an alternate {@link DartParser}. |
340 */ | 334 */ |
341 protected DartParser makeParser(ParserContext context) { | 335 protected DartParser makeParser(Source src, String sourceCode, |
342 return new DartParser(context); | 336 DartCompilerListener listener) { |
| 337 return new DartParser(src, sourceCode, false, Sets.<String>newHashSet(), lis
tener, null); |
343 } | 338 } |
344 | 339 |
345 /** | 340 /** |
346 * Override this method to provide an alternate {@link ParserContext}. | |
347 */ | |
348 protected ParserContext makeParserContext(Source src, String sourceCode, | |
349 DartCompilerListener listener) { | |
350 return new DartScannerParserContext(src, sourceCode, listener); | |
351 } | |
352 | |
353 /** | |
354 * @return the {@link DartParserRunner} with parsed source. It can be used to
request | 341 * @return the {@link DartParserRunner} with parsed source. It can be used to
request |
355 * {@link DartUnit} or compilation problems. | 342 * {@link DartUnit} or compilation problems. |
356 */ | 343 */ |
357 protected final DartParserRunner parseSource(String code) { | 344 protected final DartParserRunner parseSource(String code) { |
358 return DartParserRunner.parse(getName(), code, Integer.MAX_VALUE, false); | 345 return DartParserRunner.parse(getName(), code, Integer.MAX_VALUE, false); |
359 } | 346 } |
360 | 347 |
361 /** | 348 /** |
362 * Parses given source and checks parsing problems. | 349 * Parses given source and checks parsing problems. |
363 */ | 350 */ |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 @Override | 389 @Override |
403 public Void visitFunctionTypeAlias(DartFunctionTypeAlias node) { | 390 public Void visitFunctionTypeAlias(DartFunctionTypeAlias node) { |
404 if (node.getName().getName().equals(name)) { | 391 if (node.getName().getName().equals(name)) { |
405 result[0] = node; | 392 result[0] = node; |
406 } | 393 } |
407 return null; | 394 return null; |
408 } | 395 } |
409 }); | 396 }); |
410 return result[0]; | 397 return result[0]; |
411 } | 398 } |
| 399 |
| 400 public static String getNodeSource(String code, DartNode node) { |
| 401 SourceInfo sourceInfo = node.getSourceInfo(); |
| 402 return code.substring(sourceInfo.getOffset(), sourceInfo.getEnd()); |
| 403 } |
412 } | 404 } |
OLD | NEW |