| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library ddc.src.testing; | 5 library ddc.src.testing; |
| 6 | 6 |
| 7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | 9 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:logging/logging.dart'; | 11 import 'package:logging/logging.dart'; |
| 12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
| 13 import 'package:source_span/source_span.dart'; | 13 import 'package:source_span/source_span.dart'; |
| 14 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 15 | 15 |
| 16 import 'package:dev_compiler/src/checker/dart_sdk.dart' | 16 import 'package:dev_compiler/src/checker/dart_sdk.dart' |
| 17 show mockSdkSources, dartSdkDirectory; | 17 show mockSdkSources, dartSdkDirectory; |
| 18 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; | 18 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; |
| 19 import 'package:dev_compiler/src/utils.dart'; | 19 import 'package:dev_compiler/src/utils.dart'; |
| 20 import 'package:dev_compiler/src/info.dart'; | 20 import 'package:dev_compiler/src/info.dart'; |
| 21 import 'package:dev_compiler/src/options.dart'; | 21 import 'package:dev_compiler/src/options.dart'; |
| 22 import 'package:dev_compiler/src/report.dart'; | 22 import 'package:dev_compiler/src/report.dart'; |
| 23 import 'package:dev_compiler/config.dart'; |
| 23 import 'package:dev_compiler/devc.dart' show compile; | 24 import 'package:dev_compiler/devc.dart' show compile; |
| 24 | 25 |
| 25 /// Run the checker on a program with files contents as indicated in | 26 /// Run the checker on a program with files contents as indicated in |
| 26 /// [testFiles]. | 27 /// [testFiles]. |
| 27 /// | 28 /// |
| 28 /// This function makes several assumptions to make it easier to describe error | 29 /// This function makes several assumptions to make it easier to describe error |
| 29 /// expectations: | 30 /// expectations: |
| 30 /// | 31 /// |
| 31 /// * a file named `/main.dart` exists in [testFiles]. | 32 /// * a file named `/main.dart` exists in [testFiles]. |
| 32 /// * all expected failures are listed in the source code using comments | 33 /// * all expected failures are listed in the source code using comments |
| 33 /// immediately in front of the AST node that should contain the error. | 34 /// immediately in front of the AST node that should contain the error. |
| 34 /// * errors are formatted as a token `level:Type`, where `level` is the | 35 /// * errors are formatted as a token `level:Type`, where `level` is the |
| 35 /// logging level were the error would be reported at, and `Type` is the | 36 /// logging level were the error would be reported at, and `Type` is the |
| 36 /// concrete subclass of [StaticInfo] that denotes the error. | 37 /// concrete subclass of [StaticInfo] that denotes the error. |
| 37 /// | 38 /// |
| 38 /// For example, to check that an assignment produces a warning about a boxing | 39 /// For example, to check that an assignment produces a warning about a boxing |
| 39 /// conversion, you can describe the test as follows: | 40 /// conversion, you can describe the test as follows: |
| 40 /// | 41 /// |
| 41 /// testChecker({ | 42 /// testChecker({ |
| 42 /// '/main.dart': ''' | 43 /// '/main.dart': ''' |
| 43 /// testMethod() { | 44 /// testMethod() { |
| 44 /// dynamic x = /*warning:Box*/3; | 45 /// dynamic x = /*warning:Box*/3; |
| 45 /// } | 46 /// } |
| 46 /// ''' | 47 /// ''' |
| 47 /// }); | 48 /// }); |
| 48 /// | 49 /// |
| 49 CheckerResults testChecker(Map<String, String> testFiles, | 50 CheckerResults testChecker(Map<String, String> testFiles, |
| 50 {bool allowConstCasts: true, String sdkDir, CheckerReporter reporter, | 51 {bool allowConstCasts: true, String sdkDir, CheckerReporter reporter, |
| 51 covariantGenerics: true, relaxedCasts: true, inferFromOverrides: true, | 52 covariantGenerics: true, relaxedCasts: true, inferFromOverrides: true, |
| 52 inferStaticsFromIdentifiers: false, inferInNonStableOrder: false}) { | 53 inferStaticsFromIdentifiers: false, inferInNonStableOrder: false, |
| 54 nonnullableTypes: TypeOptions.NONNULLABLE_TYPES}) { |
| 53 expect(testFiles.containsKey('/main.dart'), isTrue, | 55 expect(testFiles.containsKey('/main.dart'), isTrue, |
| 54 reason: '`/main.dart` is missing in testFiles'); | 56 reason: '`/main.dart` is missing in testFiles'); |
| 55 | 57 |
| 56 // Create a resolver that can load test files from memory. | 58 // Create a resolver that can load test files from memory. |
| 57 var testUriResolver = new _TestUriResolver(testFiles); | 59 var testUriResolver = new _TestUriResolver(testFiles); |
| 58 var options = new CompilerOptions( | 60 var options = new CompilerOptions( |
| 59 allowConstCasts: allowConstCasts, | 61 allowConstCasts: allowConstCasts, |
| 60 covariantGenerics: covariantGenerics, | 62 covariantGenerics: covariantGenerics, |
| 61 relaxedCasts: relaxedCasts, | 63 relaxedCasts: relaxedCasts, |
| 62 inferFromOverrides: inferFromOverrides, | 64 inferFromOverrides: inferFromOverrides, |
| 63 inferStaticsFromIdentifiers: inferStaticsFromIdentifiers, | 65 inferStaticsFromIdentifiers: inferStaticsFromIdentifiers, |
| 64 inferInNonStableOrder: inferInNonStableOrder); | 66 inferInNonStableOrder: inferInNonStableOrder, |
| 67 nonnullableTypes: nonnullableTypes); |
| 65 var resolver = sdkDir == null | 68 var resolver = sdkDir == null |
| 66 ? new TypeResolver.fromMock(mockSdkSources, options, | 69 ? new TypeResolver.fromMock(mockSdkSources, options, |
| 67 otherResolvers: [testUriResolver]) | 70 otherResolvers: [testUriResolver]) |
| 68 : new TypeResolver.fromDir(sdkDir, options, | 71 : new TypeResolver.fromDir(sdkDir, options, |
| 69 otherResolvers: [testUriResolver]); | 72 otherResolvers: [testUriResolver]); |
| 70 | 73 |
| 71 // Run the checker on /main.dart. | 74 // Run the checker on /main.dart. |
| 72 var mainFile = new Uri.file('/main.dart'); | 75 var mainFile = new Uri.file('/main.dart'); |
| 73 var checkExpectations = reporter == null; | 76 var checkExpectations = reporter == null; |
| 74 if (reporter == null) reporter = new TestReporter(); | 77 if (reporter == null) reporter = new TestReporter(); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 | 308 |
| 306 SourceSpan spanFor(AstNode node) { | 309 SourceSpan spanFor(AstNode node) { |
| 307 final begin = node is AnnotatedNode | 310 final begin = node is AnnotatedNode |
| 308 ? node.firstTokenAfterCommentAndMetadata.offset | 311 ? node.firstTokenAfterCommentAndMetadata.offset |
| 309 : node.offset; | 312 : node.offset; |
| 310 return _file.span(begin, node.end); | 313 return _file.span(begin, node.end); |
| 311 } | 314 } |
| 312 | 315 |
| 313 String toString() => '[$runtimeType: $uri]'; | 316 String toString() => '[$runtimeType: $uri]'; |
| 314 } | 317 } |
| OLD | NEW |