diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cb3b88..2562c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.2.4 + +* Added some basic string formatting options to `testPhases` to make it a bit + less strict if desired. + ## 0.2.3+2 * Added logic to discover the location of the dart SDK when the dart binary is a diff --git a/lib/src/test_harness.dart b/lib/src/test_harness.dart index 3340204..a25cf52 100644 --- a/lib/src/test_harness.dart +++ b/lib/src/test_harness.dart @@ -17,10 +17,6 @@ AssetId idFromString(String s) { return new AssetId(s.substring(0, index), s.substring(index + 1)); } -String _removeTrailingWhitespace(String str) => - str.splitMapJoin('\n', - onNonMatch: (s) => s.replaceAll(new RegExp(r'\s+$'), '')); - /// A helper package provider that has files stored in memory, also wraps /// [Barback] to simply our tests. class TestHelper implements PackageProvider { @@ -38,11 +34,13 @@ class TestHelper implements PackageProvider { var resultSubscription; var logSubscription; + final StringFormatter formatter; + Future getAsset(AssetId id) => new Future.value(new Asset.fromString(id, files[idToString(id)])); TestHelper(List> transformers, Map files, - this.messages) + this.messages, {this.formatter: StringFormatter.noTrailingWhitespace}) : files = files, packages = files.keys.map((s) => idFromString(s).package) { barback = new Barback(this); @@ -100,12 +98,14 @@ class TestHelper implements PackageProvider { Future check(String assetIdString, String content) { return this[assetIdString].then((value) { - value = _removeTrailingWhitespace(value); - content = _removeTrailingWhitespace(content); + value = formatter.formatString(value); + content = formatter.formatString(content); expect(value, content, reason: 'Final output of $assetIdString differs.'); }); } + + Future checkAll(Map files) { return barback.results.first.then((_) { if (files == null) return null; @@ -122,3 +122,49 @@ class TestHelper implements PackageProvider { }); } } + +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 index 9ed8e0b..a394613 100644 --- a/lib/tests.dart +++ b/lib/tests.dart @@ -16,13 +16,16 @@ import 'package:unittest/unittest.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> phases, Map inputs, Map results, - [List messages]) { + [List messages, + StringFormatter formatter = StringFormatter.noTrailingWhitespace]) { test(testName, () => applyTransformers(phases, inputs: inputs, results: results, - messages: messages)); + messages: messages, formatter: formatter)); } /// Updates the provided transformers with [inputs] as asset inputs then @@ -36,9 +39,11 @@ testPhases(String testName, List> phases, Future applyTransformers(List> phases, {Map inputs: const {}, Map results: const {}, - List messages: const []}) { + List messages: const [], + StringFormatter formatter: StringFormatter.noTrailingWhitespace}) { - var helper = new TestHelper(phases, inputs, messages)..run(); + var helper = new TestHelper( + phases, inputs, messages, formatter: formatter)..run(); return helper.checkAll(results).then((_) => helper.tearDown()); } diff --git a/pubspec.yaml b/pubspec.yaml index f56826e..6c7254e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_transformers -version: 0.2.3+2 +version: 0.2.4 author: "Dart Team " description: Collection of utilities related to creating barback transformers. homepage: http://www.dartlang.org