| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Collection of utilities which are useful for creating unit tests for | 5 /// Collection of utilities which are useful for creating unit tests for |
| 6 /// Barback transformers. | 6 /// Barback transformers. |
| 7 library code_transformers.tests; | 7 library code_transformers.tests; |
| 8 | 8 |
| 9 import 'dart:async' show Future; | 9 import 'dart:async' show Future; |
| 10 import 'dart:io' show Platform; | 10 import 'dart:io' show Platform; |
| 11 | 11 |
| 12 import 'package:barback/barback.dart' show Transformer; | 12 import 'package:barback/barback.dart' show Transformer; |
| 13 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
| 14 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 15 | 15 |
| 16 import 'src/test_harness.dart'; | 16 import 'src/test_harness.dart'; |
| 17 import 'src/dart_sdk.dart'; | 17 import 'src/dart_sdk.dart'; |
| 18 | 18 |
| 19 export 'src/test_harness.dart' show StringFormatter; |
| 20 |
| 19 /// Defines a test which invokes [applyTransformers]. | 21 /// Defines a test which invokes [applyTransformers]. |
| 20 testPhases(String testName, List<List<Transformer>> phases, | 22 testPhases(String testName, List<List<Transformer>> phases, |
| 21 Map<String, String> inputs, Map<String, String> results, | 23 Map<String, String> inputs, Map<String, String> results, |
| 22 [List<String> messages]) { | 24 [List<String> messages, |
| 25 StringFormatter formatter = StringFormatter.noTrailingWhitespace]) { |
| 23 test(testName, | 26 test(testName, |
| 24 () => applyTransformers(phases, inputs: inputs, results: results, | 27 () => applyTransformers(phases, inputs: inputs, results: results, |
| 25 messages: messages)); | 28 messages: messages, formatter: formatter)); |
| 26 } | 29 } |
| 27 | 30 |
| 28 /// Updates the provided transformers with [inputs] as asset inputs then | 31 /// Updates the provided transformers with [inputs] as asset inputs then |
| 29 /// validates that [results] were generated. | 32 /// validates that [results] were generated. |
| 30 /// | 33 /// |
| 31 /// The keys for inputs and results are 'package_name|lib/file.dart'. | 34 /// The keys for inputs and results are 'package_name|lib/file.dart'. |
| 32 /// Only files which are specified in results are validated. | 35 /// Only files which are specified in results are validated. |
| 33 /// | 36 /// |
| 34 /// If [messages] is non-null then this will validate that only the specified | 37 /// If [messages] is non-null then this will validate that only the specified |
| 35 /// messages were generated, ignoring info messages. | 38 /// messages were generated, ignoring info messages. |
| 36 Future applyTransformers(List<List<Transformer>> phases, | 39 Future applyTransformers(List<List<Transformer>> phases, |
| 37 {Map<String, String> inputs: const {}, | 40 {Map<String, String> inputs: const {}, |
| 38 Map<String, String> results: const {}, | 41 Map<String, String> results: const {}, |
| 39 List<String> messages: const []}) { | 42 List<String> messages: const [], |
| 43 StringFormatter formatter: StringFormatter.noTrailingWhitespace}) { |
| 40 | 44 |
| 41 var helper = new TestHelper(phases, inputs, messages)..run(); | 45 var helper = new TestHelper( |
| 46 phases, inputs, messages, formatter: formatter)..run(); |
| 42 return helper.checkAll(results).then((_) => helper.tearDown()); | 47 return helper.checkAll(results).then((_) => helper.tearDown()); |
| 43 } | 48 } |
| 44 | 49 |
| 45 /// Variant of [dartSdkDirectory] which includes additional cases only | 50 /// Variant of [dartSdkDirectory] which includes additional cases only |
| 46 /// typically encountered in Dart's testing environment. | 51 /// typically encountered in Dart's testing environment. |
| 47 String get testingDartSdkDirectory { | 52 String get testingDartSdkDirectory { |
| 48 var sdkDir = dartSdkDirectory; | 53 var sdkDir = dartSdkDirectory; |
| 49 if (sdkDir == null) { | 54 if (sdkDir == null) { |
| 50 // If we cannot find the SDK dir, then assume this is being run from Dart's | 55 // If we cannot find the SDK dir, then assume this is being run from Dart's |
| 51 // source directory and this script is the main script. | 56 // source directory and this script is the main script. |
| 52 var segments = path.split(path.fromUri(Platform.script)); | 57 var segments = path.split(path.fromUri(Platform.script)); |
| 53 var index = segments.indexOf('pkg'); | 58 var index = segments.indexOf('pkg'); |
| 54 expect(index, greaterThan(0), | 59 expect(index, greaterThan(0), |
| 55 reason: 'testingDartSdkDirectory is only supported in pkg/ tests'); | 60 reason: 'testingDartSdkDirectory is only supported in pkg/ tests'); |
| 56 sdkDir = path.joinAll(segments.sublist(0, index)..add('sdk')); | 61 sdkDir = path.joinAll(segments.sublist(0, index)..add('sdk')); |
| 57 } | 62 } |
| 58 return sdkDir; | 63 return sdkDir; |
| 59 } | 64 } |
| OLD | NEW |