Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
add whitespace/newline stripping options to TestHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemac53 committed Feb 3, 2015
1 parent a638bdc commit 29966ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
60 changes: 53 additions & 7 deletions lib/src/test_harness.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,11 +34,13 @@ class TestHelper implements PackageProvider {
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.messages, {this.formatter: StringFormatter.noTrailingWhitespace})
: files = files,
packages = files.keys.map((s) => idFromString(s).package) {
barback = new Barback(this);
Expand Down Expand Up @@ -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<String, String> files) {
return barback.results.first.then((_) {
if (files == null) return null;
Expand All @@ -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', '');
13 changes: 9 additions & 4 deletions lib/tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Transformer>> phases,
Map<String, String> inputs, Map<String, String> results,
[List<String> messages]) {
[List<String> 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
Expand All @@ -36,9 +39,11 @@ testPhases(String testName, List<List<Transformer>> phases,
Future applyTransformers(List<List<Transformer>> phases,
{Map<String, String> inputs: const {},
Map<String, String> results: const {},
List<String> messages: const []}) {
List<String> 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());
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: code_transformers
version: 0.2.3+2
version: 0.2.4
author: "Dart Team <[email protected]>"
description: Collection of utilities related to creating barback transformers.
homepage: http://www.dartlang.org
Expand Down

0 comments on commit 29966ec

Please sign in to comment.