move test to a dev-dependency and move test apis to transformer_test package
diff --git a/CHANGELOG.md b/CHANGELOG.md index 43e5553..b7294ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md
@@ -1,3 +1,8 @@ +## 0.4.0 + +* Remove dependency on `test`, and move all test related apis to a new + `transformer_test` package which is now a dev dependency. + ## 0.3.1 * Update to analyzer `>=0.27.0 <0.28.0`.
diff --git a/lib/src/test_harness.dart b/lib/src/test_harness.dart deleted file mode 100644 index 2de0c96..0000000 --- a/lib/src/test_harness.dart +++ /dev/null
@@ -1,166 +0,0 @@ -// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// Utilities for creating unit tests of Barback transformers. -library code_transformers.src.test_harness; - -import 'dart:async'; - -import 'package:barback/barback.dart'; -import 'package:stack_trace/stack_trace.dart'; -import 'package:test/test.dart'; - -String idToString(AssetId id) => '${id.package}|${id.path}'; -AssetId idFromString(String s) { - int index = s.indexOf('|'); - return new AssetId(s.substring(0, index), s.substring(index + 1)); -} - -/// A helper package provider that has files stored in memory, also wraps -/// [Barback] to simply our tests. -class TestHelper implements PackageProvider { - - /// Maps from an asset string identifier of the form 'package|path' to the - /// file contents. - final Map<String, String> files; - final Iterable<String> packages; - final List<String> messages; - int messagesSeen = 0; - bool errorSeen = false; - - Barback barback; - var errorSubscription; - var resultSubscription; - var logSubscription; - - final StringFormatter formatter; - - Future<Asset> getAsset(AssetId id) => - new Future.value(new Asset.fromString(id, files[idToString(id)])); - - TestHelper(List<List<Transformer>> transformers, Map<String, String> files, - this.messages, {this.formatter: StringFormatter.noTrailingWhitespace}) - : files = files, - packages = files.keys.map((s) => idFromString(s).package) { - barback = new Barback(this); - for (var p in packages) { - barback.updateTransformers(p, transformers); - } - - errorSubscription = barback.errors.listen((e) { - var trace = null; - if (e is Error) trace = e.stackTrace; - if (trace != null) { - print(Trace.format(trace)); - } - fail('error running barback: $e'); - }); - - resultSubscription = barback.results.listen((result) { - expect(result.succeeded, !errorSeen, reason: "${result.errors}"); - }); - - logSubscription = barback.log.listen((entry) { - // Ignore info and fine messages. - if (entry.level == LogLevel.INFO || entry.level == LogLevel.FINE) return; - if (entry.level == LogLevel.ERROR) errorSeen = true; - // We only check messages when an expectation is provided. - if (messages == null) return; - - var msg = '${entry.level.name.toLowerCase()}: ${entry.message}'; - var span = entry.span; - var spanInfo = span == null - ? '' - : ' (${span.sourceUrl} ${span.start.line} ${span.start.column})'; - expect(messagesSeen, lessThan(messages.length), - reason: 'more messages than expected.\nMessage seen: $msg$spanInfo'); - expect('$msg$spanInfo', messages[messagesSeen++]); - }); - } - - void tearDown() { - errorSubscription.cancel(); - resultSubscription.cancel(); - logSubscription.cancel(); - } - - /// Tells barback which files have changed, and thus anything that depends on - /// it on should be computed. By default mark all the input files. - void run([Iterable<String> paths]) { - if (paths == null) paths = files.keys; - barback.updateSources(paths.map(idFromString)); - } - - Future<String> operator [](String assetString) { - return barback - .getAssetById(idFromString(assetString)) - .then((asset) => asset.readAsString()); - } - - Future check(String assetIdString, String content) { - return this[assetIdString].then((value) { - value = formatter.formatString(value); - content = formatter.formatString(content); - expect(value, content, reason: 'Final output of $assetIdString differs.'); - }); - } - - Future checkAll(Map<String, String> files) { - return barback.results.first.then((_) { - if (files == null) return null; - var futures = []; - files.forEach((k, v) { - futures.add(check(k, v)); - }); - return Future.wait(futures); - }).then((_) { - // We only check messages when an expectation is provided. - if (messages == null) return; - expect( - messagesSeen, messages.length, reason: 'less messages than expected'); - }); - } -} - -class StringFormatter { - // Formatting options - final bool stripLeadingWhitespace; - final bool stripTrailingWhitespace; - final bool stripNewlines; - - // Static variations for convenience - static const noLeadingWhitespace = - const StringFormatter(stripLeadingWhitespace: true); - - static const noTrailingWhitespace = - const StringFormatter(stripTrailingWhitespace: true); - - static const noSurroundingWhitespace = const StringFormatter( - stripLeadingWhitespace: true, stripTrailingWhitespace: true); - - static const noNewlines = const StringFormatter(stripNewlines: true); - - static const noNewlinesOrSurroundingWhitespace = const StringFormatter( - stripLeadingWhitespace: true, - stripTrailingWhitespace: true, - stripNewlines: true); - - const StringFormatter({this.stripLeadingWhitespace: false, - this.stripTrailingWhitespace: false, this.stripNewlines: false}); - - String formatString(String str) { - if (stripLeadingWhitespace) str = _removeLeadingWhitespace(str); - if (stripTrailingWhitespace) str = _removeTrailingWhitespace(str); - if (stripNewlines) str = _removeNewlines(str); - return str; - } -} - -String _removeTrailingWhitespace(String str) => str.splitMapJoin('\n', - onNonMatch: (s) => s.replaceAll(new RegExp(r'\s+$'), '')); - -String _removeLeadingWhitespace(String str) => str.splitMapJoin('\n', - onNonMatch: (s) => s.replaceAll(new RegExp(r'^\s+'), '')); - -String _removeNewlines(String str) => str.replaceAll('\n', '');
diff --git a/lib/tests.dart b/lib/tests.dart deleted file mode 100644 index cda576b..0000000 --- a/lib/tests.dart +++ /dev/null
@@ -1,64 +0,0 @@ -// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// Collection of utilities which are useful for creating unit tests for -/// Barback transformers. -library code_transformers.tests; - -import 'dart:async' show Future; -import 'dart:io' show Platform; - -import 'package:barback/barback.dart' show Transformer; -import 'package:path/path.dart' as path; -import 'package:test/test.dart'; - -import 'src/test_harness.dart'; -import 'src/dart_sdk.dart'; - -export 'src/test_harness.dart' show StringFormatter; - -/// Defines a test which invokes [applyTransformers]. -testPhases(String testName, List<List<Transformer>> phases, - Map<String, String> inputs, Map<String, String> results, - [List<String> messages, - StringFormatter formatter = StringFormatter.noTrailingWhitespace]) { - test(testName, () => applyTransformers(phases, - inputs: inputs, - results: results, - messages: messages, - formatter: formatter)); -} - -/// Updates the provided transformers with [inputs] as asset inputs then -/// validates that [results] were generated. -/// -/// The keys for inputs and results are 'package_name|lib/file.dart'. -/// Only files which are specified in results are validated. -/// -/// If [messages] is non-null then this will validate that only the specified -/// messages were generated, ignoring info messages. -Future applyTransformers(List<List<Transformer>> phases, - {Map<String, String> inputs: const {}, Map<String, String> results: const { -}, List<String> messages: const [], - StringFormatter formatter: StringFormatter.noTrailingWhitespace}) { - var helper = new TestHelper(phases, inputs, messages, formatter: formatter) - ..run(); - return helper.checkAll(results).then((_) => helper.tearDown()); -} - -/// Variant of [dartSdkDirectory] which includes additional cases only -/// typically encountered in Dart's testing environment. -String get testingDartSdkDirectory { - var sdkDir = dartSdkDirectory; - if (sdkDir == null) { - // If we cannot find the SDK dir, then assume this is being run from Dart's - // source directory and this script is the main script. - var segments = path.split(path.fromUri(Platform.script)); - var index = segments.indexOf('pkg'); - expect(index, greaterThan(0), - reason: 'testingDartSdkDirectory is only supported in pkg/ tests'); - sdkDir = path.joinAll(segments.sublist(0, index)..add('sdk')); - } - return sdkDir; -}
diff --git a/pubspec.yaml b/pubspec.yaml index f41da73..58180ea 100644 --- a/pubspec.yaml +++ b/pubspec.yaml
@@ -1,5 +1,5 @@ name: code_transformers -version: 0.3.1 +version: 0.4.0 author: Dart Team <misc@dartlang.org> description: Collection of utilities related to creating barback transformers. homepage: https://github.com/dart-lang/code-transformers @@ -12,4 +12,6 @@ path: '>=0.9.0 <2.0.0' source_maps: '>=0.9.4 <0.11.0' source_span: '>=1.0.0 <2.0.0' +dev_dependencies: test: '>=0.12.1 <0.13.0' + transformer_test: '>=0.1.0 <0.2.0'
diff --git a/test/assets_test.dart b/test/assets_test.dart index 7c2fc59..afdee51 100644 --- a/test/assets_test.dart +++ b/test/assets_test.dart
@@ -8,7 +8,7 @@ import 'package:barback/barback.dart'; import 'package:code_transformers/assets.dart'; -import 'package:code_transformers/tests.dart'; +import 'package:transformer_test/transformer_test.dart'; import 'package:test/test.dart'; main() {
diff --git a/test/entry_point_test.dart b/test/entry_point_test.dart index a08d6d0..7905c4c 100644 --- a/test/entry_point_test.dart +++ b/test/entry_point_test.dart
@@ -8,7 +8,7 @@ import 'package:barback/barback.dart'; import 'package:code_transformers/resolver.dart'; -import 'package:code_transformers/tests.dart'; +import 'package:transformer_test/transformer_test.dart'; import 'package:test/test.dart'; main() {
diff --git a/test/remove_sourcemap_comment_test.dart b/test/remove_sourcemap_comment_test.dart index b162321..1836b62 100644 --- a/test/remove_sourcemap_comment_test.dart +++ b/test/remove_sourcemap_comment_test.dart
@@ -6,7 +6,7 @@ import 'package:barback/barback.dart'; import 'package:code_transformers/src/remove_sourcemap_comment.dart'; -import 'package:code_transformers/tests.dart'; +import 'package:transformer_test/transformer_test.dart'; final phases = [ [
diff --git a/test/resolver_test.dart b/test/resolver_test.dart index 8dff240..136b908 100644 --- a/test/resolver_test.dart +++ b/test/resolver_test.dart
@@ -8,7 +8,7 @@ import 'package:barback/barback.dart'; import 'package:code_transformers/resolver.dart'; -import 'package:code_transformers/tests.dart'; +import 'package:transformer_test/transformer_test.dart'; import 'package:test/test.dart'; import 'package:code_transformers/src/dart_sdk.dart' show mockSdkSources;