| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library analyzer.test.src.task.options_test; |
| 6 |
| 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/source/analysis_options_provider.dart'; |
| 9 import 'package:analyzer/source/error_processor.dart'; |
| 10 import 'package:analyzer/src/context/context.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/task/options.dart' |
| 14 show CONFIGURED_ERROR_PROCESSORS; |
| 15 import 'package:analyzer/src/task/options.dart'; |
| 16 import 'package:analyzer/task/general.dart'; |
| 17 import 'package:analyzer/task/model.dart'; |
| 18 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 19 import 'package:unittest/unittest.dart'; |
| 20 import 'package:yaml/yaml.dart'; |
| 21 |
| 22 import '../../generated/test_support.dart'; |
| 23 import '../../utils.dart'; |
| 24 import '../context/abstract_context.dart'; |
| 25 |
| 26 main() { |
| 27 initializeTestEnvironment(); |
| 28 defineReflectiveTests(ContextConfigurationTest); |
| 29 defineReflectiveTests(GenerateNewOptionsErrorsTaskTest); |
| 30 defineReflectiveTests(GenerateOldOptionsErrorsTaskTest); |
| 31 defineReflectiveTests(OptionsFileValidatorTest); |
| 32 } |
| 33 |
| 34 isInstanceOf isGenerateOptionsErrorsTask = |
| 35 new isInstanceOf<GenerateOptionsErrorsTask>(); |
| 36 |
| 37 @reflectiveTest |
| 38 class ContextConfigurationTest extends AbstractContextTest { |
| 39 final AnalysisOptionsProvider optionsProvider = new AnalysisOptionsProvider(); |
| 40 |
| 41 AnalysisOptions get analysisOptions => context.analysisOptions; |
| 42 |
| 43 configureContext(String optionsSource) => |
| 44 configureContextOptions(context, parseOptions(optionsSource)); |
| 45 |
| 46 Map<String, YamlNode> parseOptions(String source) => |
| 47 optionsProvider.getOptionsFromString(source); |
| 48 |
| 49 test_configure_bad_options_contents() { |
| 50 configureContext(''' |
| 51 analyzer: |
| 52 strong-mode:true # misformatted |
| 53 '''); |
| 54 expect(analysisOptions.strongMode, false); |
| 55 } |
| 56 |
| 57 test_configure_enableGenericMethods() { |
| 58 expect(analysisOptions.enableGenericMethods, false); |
| 59 configureContext(''' |
| 60 analyzer: |
| 61 language: |
| 62 enableGenericMethods: true |
| 63 '''); |
| 64 expect(analysisOptions.enableGenericMethods, true); |
| 65 } |
| 66 |
| 67 test_configure_enableStrictCallChecks() { |
| 68 configureContext(''' |
| 69 analyzer: |
| 70 language: |
| 71 enableStrictCallChecks: true |
| 72 '''); |
| 73 expect(analysisOptions.enableStrictCallChecks, true); |
| 74 } |
| 75 |
| 76 test_configure_enableSuperMixins() { |
| 77 configureContext(''' |
| 78 analyzer: |
| 79 language: |
| 80 enableSuperMixins: true |
| 81 '''); |
| 82 expect(analysisOptions.enableSuperMixins, true); |
| 83 } |
| 84 |
| 85 test_configure_error_processors() { |
| 86 configureContext(''' |
| 87 analyzer: |
| 88 errors: |
| 89 invalid_assignment: ignore |
| 90 unused_local_variable: error |
| 91 '''); |
| 92 |
| 93 List<ErrorProcessor> processors = |
| 94 context.getConfigurationData(CONFIGURED_ERROR_PROCESSORS); |
| 95 expect(processors, hasLength(2)); |
| 96 |
| 97 var unused_local = new AnalysisError( |
| 98 new TestSource(), 0, 1, HintCode.UNUSED_LOCAL_VARIABLE, [ |
| 99 ['x'] |
| 100 ]); |
| 101 var invalid_assignment = |
| 102 new AnalysisError(new TestSource(), 0, 1, HintCode.INVALID_ASSIGNMENT, [ |
| 103 ['x'], |
| 104 ['y'] |
| 105 ]); |
| 106 |
| 107 // ignore |
| 108 var invalidAssignment = |
| 109 processors.firstWhere((p) => p.appliesTo(invalid_assignment)); |
| 110 expect(invalidAssignment.severity, isNull); |
| 111 |
| 112 // error |
| 113 var unusedLocal = processors.firstWhere((p) => p.appliesTo(unused_local)); |
| 114 expect(unusedLocal.severity, ErrorSeverity.ERROR); |
| 115 } |
| 116 |
| 117 test_configure_excludes() { |
| 118 configureContext(''' |
| 119 analyzer: |
| 120 exclude: |
| 121 - foo/bar.dart |
| 122 - 'test/**' |
| 123 '''); |
| 124 |
| 125 List<String> excludes = context.getConfigurationData(CONTEXT_EXCLUDES); |
| 126 expect(excludes, unorderedEquals(['foo/bar.dart', 'test/**'])); |
| 127 } |
| 128 |
| 129 test_configure_strong_mode() { |
| 130 configureContext(''' |
| 131 analyzer: |
| 132 strong-mode: true |
| 133 '''); |
| 134 expect(analysisOptions.strongMode, true); |
| 135 } |
| 136 |
| 137 test_configure_strong_mode_bad_value() { |
| 138 configureContext(''' |
| 139 analyzer: |
| 140 strong-mode: foo |
| 141 '''); |
| 142 expect(analysisOptions.strongMode, false); |
| 143 } |
| 144 } |
| 145 |
| 146 @reflectiveTest |
| 147 class GenerateNewOptionsErrorsTaskTest extends GenerateOptionsErrorsTaskTest { |
| 148 String get optionsFilePath => '/${AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE}'; |
| 149 } |
| 150 |
| 151 @reflectiveTest |
| 152 class GenerateOldOptionsErrorsTaskTest extends GenerateOptionsErrorsTaskTest { |
| 153 String get optionsFilePath => '/${AnalysisEngine.ANALYSIS_OPTIONS_FILE}'; |
| 154 } |
| 155 |
| 156 abstract class GenerateOptionsErrorsTaskTest extends AbstractContextTest { |
| 157 Source source; |
| 158 |
| 159 String get optionsFilePath; |
| 160 LineInfo lineInfo(String source) => |
| 161 GenerateOptionsErrorsTask.computeLineInfo(source); |
| 162 |
| 163 @override |
| 164 setUp() { |
| 165 super.setUp(); |
| 166 source = newSource(optionsFilePath); |
| 167 } |
| 168 |
| 169 test_buildInputs() { |
| 170 Map<String, TaskInput> inputs = |
| 171 GenerateOptionsErrorsTask.buildInputs(source); |
| 172 expect(inputs, isNotNull); |
| 173 expect(inputs.keys, |
| 174 unorderedEquals([GenerateOptionsErrorsTask.CONTENT_INPUT_NAME])); |
| 175 } |
| 176 |
| 177 test_compute_lineInfo() { |
| 178 expect(lineInfo('foo\nbar').getLocation(4).lineNumber, 2); |
| 179 expect(lineInfo('foo\nbar').getLocation(4).columnNumber, 1); |
| 180 expect(lineInfo('foo\r\nbar').getLocation(5).lineNumber, 2); |
| 181 expect(lineInfo('foo\r\nbar').getLocation(5).columnNumber, 1); |
| 182 expect(lineInfo('foo\rbar').getLocation(4).lineNumber, 2); |
| 183 expect(lineInfo('foo\rbar').getLocation(4).columnNumber, 1); |
| 184 expect(lineInfo('foo').getLocation(0).lineNumber, 1); |
| 185 expect(lineInfo('foo').getLocation(0).columnNumber, 1); |
| 186 expect(lineInfo('').getLocation(1).lineNumber, 1); |
| 187 } |
| 188 |
| 189 test_constructor() { |
| 190 GenerateOptionsErrorsTask task = |
| 191 new GenerateOptionsErrorsTask(context, source); |
| 192 expect(task, isNotNull); |
| 193 expect(task.context, context); |
| 194 expect(task.target, source); |
| 195 } |
| 196 |
| 197 test_createTask() { |
| 198 GenerateOptionsErrorsTask task = |
| 199 GenerateOptionsErrorsTask.createTask(context, source); |
| 200 expect(task, isNotNull); |
| 201 expect(task.context, context); |
| 202 expect(task.target, source); |
| 203 } |
| 204 |
| 205 test_description() { |
| 206 GenerateOptionsErrorsTask task = |
| 207 new GenerateOptionsErrorsTask(null, source); |
| 208 expect(task.description, isNotNull); |
| 209 } |
| 210 |
| 211 test_descriptor() { |
| 212 TaskDescriptor descriptor = GenerateOptionsErrorsTask.DESCRIPTOR; |
| 213 expect(descriptor, isNotNull); |
| 214 } |
| 215 |
| 216 test_perform_bad_yaml() { |
| 217 String code = r''' |
| 218 : |
| 219 '''; |
| 220 AnalysisTarget target = newSource(optionsFilePath, code); |
| 221 computeResult(target, ANALYSIS_OPTIONS_ERRORS); |
| 222 expect(task, isGenerateOptionsErrorsTask); |
| 223 List<AnalysisError> errors = |
| 224 outputs[ANALYSIS_OPTIONS_ERRORS] as List<AnalysisError>; |
| 225 expect(errors, hasLength(1)); |
| 226 expect(errors[0].errorCode, AnalysisOptionsErrorCode.PARSE_ERROR); |
| 227 } |
| 228 |
| 229 test_perform_OK() { |
| 230 String code = r''' |
| 231 analyzer: |
| 232 strong-mode: true |
| 233 '''; |
| 234 AnalysisTarget target = newSource(optionsFilePath, code); |
| 235 computeResult(target, ANALYSIS_OPTIONS_ERRORS); |
| 236 expect(task, isGenerateOptionsErrorsTask); |
| 237 expect(outputs[ANALYSIS_OPTIONS_ERRORS], isEmpty); |
| 238 LineInfo lineInfo = outputs[LINE_INFO]; |
| 239 expect(lineInfo, isNotNull); |
| 240 expect(lineInfo.getLocation(1).lineNumber, 1); |
| 241 expect(lineInfo.getLocation(10).lineNumber, 2); |
| 242 } |
| 243 |
| 244 test_perform_unsupported_analyzer_option() { |
| 245 String code = r''' |
| 246 analyzer: |
| 247 not_supported: true |
| 248 '''; |
| 249 AnalysisTarget target = newSource(optionsFilePath, code); |
| 250 computeResult(target, ANALYSIS_OPTIONS_ERRORS); |
| 251 expect(task, isGenerateOptionsErrorsTask); |
| 252 List<AnalysisError> errors = |
| 253 outputs[ANALYSIS_OPTIONS_ERRORS] as List<AnalysisError>; |
| 254 expect(errors, hasLength(1)); |
| 255 expect(errors[0].errorCode, |
| 256 AnalysisOptionsWarningCode.UNSUPPORTED_OPTION_WITH_LEGAL_VALUES); |
| 257 expect( |
| 258 errors[0].message, |
| 259 "The option 'not_supported' is not supported by analyzer, supported " |
| 260 "values are 'errors', 'exclude', 'language', 'plugins' and 'strong-mode'
"); |
| 261 } |
| 262 } |
| 263 |
| 264 @reflectiveTest |
| 265 class OptionsFileValidatorTest { |
| 266 final OptionsFileValidator validator = |
| 267 new OptionsFileValidator(new TestSource()); |
| 268 final AnalysisOptionsProvider optionsProvider = new AnalysisOptionsProvider(); |
| 269 |
| 270 test_analyzer_error_code_supported() { |
| 271 validate( |
| 272 ''' |
| 273 analyzer: |
| 274 errors: |
| 275 unused_local_variable: ignore |
| 276 invalid_assignment: warning |
| 277 missing_return: error |
| 278 dead_code: info |
| 279 ''', |
| 280 []); |
| 281 } |
| 282 |
| 283 test_analyzer_error_code_supported_bad_value() { |
| 284 validate( |
| 285 ''' |
| 286 analyzer: |
| 287 errors: |
| 288 unused_local_variable: ftw |
| 289 ''', |
| 290 [AnalysisOptionsWarningCode.UNSUPPORTED_OPTION_WITH_LEGAL_VALUES]); |
| 291 } |
| 292 |
| 293 test_analyzer_error_code_unsupported() { |
| 294 validate( |
| 295 ''' |
| 296 analyzer: |
| 297 errors: |
| 298 not_supported: ignore |
| 299 ''', |
| 300 [AnalysisOptionsWarningCode.UNRECOGNIZED_ERROR_CODE]); |
| 301 } |
| 302 |
| 303 test_analyzer_language_supported() { |
| 304 validate( |
| 305 ''' |
| 306 analyzer: |
| 307 language: |
| 308 enableSuperMixins: true |
| 309 ''', |
| 310 []); |
| 311 } |
| 312 |
| 313 test_analyzer_language_unsupported_key() { |
| 314 validate( |
| 315 ''' |
| 316 analyzer: |
| 317 language: |
| 318 unsupported: true |
| 319 ''', |
| 320 [AnalysisOptionsWarningCode.UNSUPPORTED_OPTION_WITH_LEGAL_VALUES]); |
| 321 } |
| 322 |
| 323 test_analyzer_language_unsupported_value() { |
| 324 validate( |
| 325 ''' |
| 326 analyzer: |
| 327 language: |
| 328 enableSuperMixins: foo |
| 329 ''', |
| 330 [AnalysisOptionsWarningCode.UNSUPPORTED_VALUE]); |
| 331 } |
| 332 |
| 333 test_analyzer_strong_mode_error_code_supported() { |
| 334 validate( |
| 335 ''' |
| 336 analyzer: |
| 337 errors: |
| 338 strong_mode_assignment_cast: ignore |
| 339 ''', |
| 340 []); |
| 341 } |
| 342 |
| 343 test_analyzer_supported_exclude() { |
| 344 validate( |
| 345 ''' |
| 346 analyzer: |
| 347 exclude: |
| 348 - test/_data/p4/lib/lib1.dart |
| 349 ''', |
| 350 []); |
| 351 } |
| 352 |
| 353 test_analyzer_supported_strong_mode() { |
| 354 validate( |
| 355 ''' |
| 356 analyzer: |
| 357 strong-mode: true |
| 358 ''', |
| 359 []); |
| 360 } |
| 361 |
| 362 test_analyzer_supported_strong_mode_supported_bad_value() { |
| 363 validate( |
| 364 ''' |
| 365 analyzer: |
| 366 strong-mode: w00t |
| 367 ''', |
| 368 [AnalysisOptionsWarningCode.UNSUPPORTED_VALUE]); |
| 369 } |
| 370 |
| 371 test_analyzer_unsupported_option() { |
| 372 validate( |
| 373 ''' |
| 374 analyzer: |
| 375 not_supported: true |
| 376 ''', |
| 377 [AnalysisOptionsWarningCode.UNSUPPORTED_OPTION_WITH_LEGAL_VALUES]); |
| 378 } |
| 379 |
| 380 test_linter_supported_rules() { |
| 381 validate( |
| 382 ''' |
| 383 linter: |
| 384 rules: |
| 385 - camel_case_types |
| 386 ''', |
| 387 []); |
| 388 } |
| 389 |
| 390 test_linter_unsupported_option() { |
| 391 validate( |
| 392 ''' |
| 393 linter: |
| 394 unsupported: true |
| 395 ''', |
| 396 [AnalysisOptionsWarningCode.UNSUPPORTED_OPTION_WITH_LEGAL_VALUE]); |
| 397 } |
| 398 |
| 399 void validate(String source, List<ErrorCode> expected) { |
| 400 var options = optionsProvider.getOptionsFromString(source); |
| 401 var errors = validator.validate(options); |
| 402 expect(errors.map((AnalysisError e) => e.errorCode), |
| 403 unorderedEquals(expected)); |
| 404 } |
| 405 } |
| OLD | NEW |