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

Commit 29966ec

Browse files
committed
add whitespace/newline stripping options to TestHelper
[email protected] Review URL: https://codereview.chromium.org//897453003
1 parent a638bdc commit 29966ec

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.4
2+
3+
* Added some basic string formatting options to `testPhases` to make it a bit
4+
less strict if desired.
5+
16
## 0.2.3+2
27

38
* Added logic to discover the location of the dart SDK when the dart binary is a

lib/src/test_harness.dart

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ AssetId idFromString(String s) {
1717
return new AssetId(s.substring(0, index), s.substring(index + 1));
1818
}
1919

20-
String _removeTrailingWhitespace(String str) =>
21-
str.splitMapJoin('\n',
22-
onNonMatch: (s) => s.replaceAll(new RegExp(r'\s+$'), ''));
23-
2420
/// A helper package provider that has files stored in memory, also wraps
2521
/// [Barback] to simply our tests.
2622
class TestHelper implements PackageProvider {
@@ -38,11 +34,13 @@ class TestHelper implements PackageProvider {
3834
var resultSubscription;
3935
var logSubscription;
4036

37+
final StringFormatter formatter;
38+
4139
Future<Asset> getAsset(AssetId id) =>
4240
new Future.value(new Asset.fromString(id, files[idToString(id)]));
4341

4442
TestHelper(List<List<Transformer>> transformers, Map<String, String> files,
45-
this.messages)
43+
this.messages, {this.formatter: StringFormatter.noTrailingWhitespace})
4644
: files = files,
4745
packages = files.keys.map((s) => idFromString(s).package) {
4846
barback = new Barback(this);
@@ -100,12 +98,14 @@ class TestHelper implements PackageProvider {
10098

10199
Future check(String assetIdString, String content) {
102100
return this[assetIdString].then((value) {
103-
value = _removeTrailingWhitespace(value);
104-
content = _removeTrailingWhitespace(content);
101+
value = formatter.formatString(value);
102+
content = formatter.formatString(content);
105103
expect(value, content, reason: 'Final output of $assetIdString differs.');
106104
});
107105
}
108106

107+
108+
109109
Future checkAll(Map<String, String> files) {
110110
return barback.results.first.then((_) {
111111
if (files == null) return null;
@@ -122,3 +122,49 @@ class TestHelper implements PackageProvider {
122122
});
123123
}
124124
}
125+
126+
class StringFormatter {
127+
// Formatting options
128+
final bool stripLeadingWhitespace;
129+
final bool stripTrailingWhitespace;
130+
final bool stripNewlines;
131+
132+
// Static variations for convenience
133+
static const noLeadingWhitespace =
134+
const StringFormatter(stripLeadingWhitespace: true);
135+
136+
static const noTrailingWhitespace =
137+
const StringFormatter(stripTrailingWhitespace: true);
138+
139+
static const noSurroundingWhitespace = const StringFormatter(
140+
stripLeadingWhitespace: true, stripTrailingWhitespace: true);
141+
142+
static const noNewlines = const StringFormatter(stripNewlines: true);
143+
144+
static const noNewlinesOrSurroundingWhitespace = const StringFormatter(
145+
stripLeadingWhitespace: true,
146+
stripTrailingWhitespace: true,
147+
stripNewlines: true);
148+
149+
const StringFormatter({
150+
this.stripLeadingWhitespace: false,
151+
this.stripTrailingWhitespace: false,
152+
this.stripNewlines: false});
153+
154+
String formatString(String str) {
155+
if (stripLeadingWhitespace) str = _removeLeadingWhitespace(str);
156+
if (stripTrailingWhitespace) str = _removeTrailingWhitespace(str);
157+
if (stripNewlines) str = _removeNewlines(str);
158+
return str;
159+
}
160+
}
161+
162+
String _removeTrailingWhitespace(String str) =>
163+
str.splitMapJoin('\n',
164+
onNonMatch: (s) => s.replaceAll(new RegExp(r'\s+$'), ''));
165+
166+
String _removeLeadingWhitespace(String str) =>
167+
str.splitMapJoin('\n',
168+
onNonMatch: (s) => s.replaceAll(new RegExp(r'^\s+'), ''));
169+
170+
String _removeNewlines(String str) => str.replaceAll('\n', '');

lib/tests.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import 'package:unittest/unittest.dart';
1616
import 'src/test_harness.dart';
1717
import 'src/dart_sdk.dart';
1818

19+
export 'src/test_harness.dart' show StringFormatter;
20+
1921
/// Defines a test which invokes [applyTransformers].
2022
testPhases(String testName, List<List<Transformer>> phases,
2123
Map<String, String> inputs, Map<String, String> results,
22-
[List<String> messages]) {
24+
[List<String> messages,
25+
StringFormatter formatter = StringFormatter.noTrailingWhitespace]) {
2326
test(testName,
2427
() => applyTransformers(phases, inputs: inputs, results: results,
25-
messages: messages));
28+
messages: messages, formatter: formatter));
2629
}
2730

2831
/// Updates the provided transformers with [inputs] as asset inputs then
@@ -36,9 +39,11 @@ testPhases(String testName, List<List<Transformer>> phases,
3639
Future applyTransformers(List<List<Transformer>> phases,
3740
{Map<String, String> inputs: const {},
3841
Map<String, String> results: const {},
39-
List<String> messages: const []}) {
42+
List<String> messages: const [],
43+
StringFormatter formatter: StringFormatter.noTrailingWhitespace}) {
4044

41-
var helper = new TestHelper(phases, inputs, messages)..run();
45+
var helper = new TestHelper(
46+
phases, inputs, messages, formatter: formatter)..run();
4247
return helper.checkAll(results).then((_) => helper.tearDown());
4348
}
4449

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_transformers
2-
version: 0.2.3+2
2+
version: 0.2.4
33
author: "Dart Team <[email protected]>"
44
description: Collection of utilities related to creating barback transformers.
55
homepage: http://www.dartlang.org

0 commit comments

Comments
 (0)