| 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 linter.test.rule; | 5 library linter.test.rule; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart'; | 10 import 'package:analyzer/src/generated/error.dart'; |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 AnnotationMatcher matchesAnnotation( | 334 AnnotationMatcher matchesAnnotation( |
| 335 String message, ErrorType type, int lineNumber) => | 335 String message, ErrorType type, int lineNumber) => |
| 336 new AnnotationMatcher(new Annotation(message, type, lineNumber)); | 336 new AnnotationMatcher(new Annotation(message, type, lineNumber)); |
| 337 | 337 |
| 338 testEach(Iterable<String> values, dynamic f(String s), Matcher m) { | 338 testEach(Iterable<String> values, dynamic f(String s), Matcher m) { |
| 339 values.forEach((s) => test('"$s"', () => expect(f(s), m))); | 339 values.forEach((s) => test('"$s"', () => expect(f(s), m))); |
| 340 } | 340 } |
| 341 | 341 |
| 342 testRule(String ruleName, File file, {bool debug: false}) { | 342 testRule(String ruleName, File file, {bool debug: false}) { |
| 343 test('$ruleName', () { | 343 test('$ruleName', () { |
| 344 if (!file.existsSync()) { |
| 345 throw new Exception('No rule found defined at: ${file.path}'); |
| 346 } |
| 347 |
| 344 var expected = <AnnotationMatcher>[]; | 348 var expected = <AnnotationMatcher>[]; |
| 345 | 349 |
| 346 int lineNumber = 1; | 350 int lineNumber = 1; |
| 347 for (var line in file.readAsLinesSync()) { | 351 for (var line in file.readAsLinesSync()) { |
| 348 var annotation = extractAnnotation(line); | 352 var annotation = extractAnnotation(line); |
| 349 if (annotation != null) { | 353 if (annotation != null) { |
| 350 annotation.lineNumber = lineNumber; | 354 annotation.lineNumber = lineNumber; |
| 351 expected.add(new AnnotationMatcher(annotation)); | 355 expected.add(new AnnotationMatcher(annotation)); |
| 352 } | 356 } |
| 353 ++lineNumber; | 357 ++lineNumber; |
| 354 } | 358 } |
| 355 | 359 |
| 356 LinterOptions options = new LinterOptions( | 360 LintRule rule = ruleRegistry[ruleName]; |
| 357 [ruleRegistry[ruleName]].where((rule) => rule != null)) | 361 if (rule == null) { |
| 362 print('WARNING: Test skipped -- rule `$ruleName` is not registered.'); |
| 363 return; |
| 364 } |
| 365 |
| 366 LinterOptions options = new LinterOptions([rule]) |
| 358 ..useMockSdk = true | 367 ..useMockSdk = true |
| 359 ..packageRootPath = '.'; | 368 ..packageRootPath = '.'; |
| 360 | 369 |
| 361 DartLinter driver = new DartLinter(options); | 370 DartLinter driver = new DartLinter(options); |
| 362 | 371 |
| 363 Iterable<AnalysisErrorInfo> lints = driver.lintFiles([file]); | 372 Iterable<AnalysisErrorInfo> lints = driver.lintFiles([file]); |
| 364 | 373 |
| 365 List<Annotation> actual = []; | 374 List<Annotation> actual = []; |
| 366 lints.forEach((AnalysisErrorInfo info) { | 375 lints.forEach((AnalysisErrorInfo info) { |
| 367 info.errors.forEach((AnalysisError error) { | 376 info.errors.forEach((AnalysisError error) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 | 460 |
| 452 class NoFilter implements LintFilter { | 461 class NoFilter implements LintFilter { |
| 453 @override | 462 @override |
| 454 bool filter(AnalysisError lint) => false; | 463 bool filter(AnalysisError lint) => false; |
| 455 } | 464 } |
| 456 | 465 |
| 457 class ResultReporter extends DetailedReporter { | 466 class ResultReporter extends DetailedReporter { |
| 458 ResultReporter(Iterable<AnalysisErrorInfo> errors) | 467 ResultReporter(Iterable<AnalysisErrorInfo> errors) |
| 459 : super(errors, new NoFilter(), stdout); | 468 : super(errors, new NoFilter(), stdout); |
| 460 } | 469 } |
| OLD | NEW |