@@ -17,10 +17,6 @@ AssetId idFromString(String s) {
17
17
return new AssetId (s.substring (0 , index), s.substring (index + 1 ));
18
18
}
19
19
20
- String _removeTrailingWhitespace (String str) =>
21
- str.splitMapJoin ('\n ' ,
22
- onNonMatch: (s) => s.replaceAll (new RegExp (r'\s+$' ), '' ));
23
-
24
20
/// A helper package provider that has files stored in memory, also wraps
25
21
/// [Barback] to simply our tests.
26
22
class TestHelper implements PackageProvider {
@@ -38,11 +34,13 @@ class TestHelper implements PackageProvider {
38
34
var resultSubscription;
39
35
var logSubscription;
40
36
37
+ final StringFormatter formatter;
38
+
41
39
Future <Asset > getAsset (AssetId id) =>
42
40
new Future .value (new Asset .fromString (id, files[idToString (id)]));
43
41
44
42
TestHelper (List <List <Transformer >> transformers, Map <String , String > files,
45
- this .messages)
43
+ this .messages, { this .formatter : StringFormatter .noTrailingWhitespace} )
46
44
: files = files,
47
45
packages = files.keys.map ((s) => idFromString (s).package) {
48
46
barback = new Barback (this );
@@ -100,12 +98,14 @@ class TestHelper implements PackageProvider {
100
98
101
99
Future check (String assetIdString, String content) {
102
100
return this [assetIdString].then ((value) {
103
- value = _removeTrailingWhitespace (value);
104
- content = _removeTrailingWhitespace (content);
101
+ value = formatter. formatString (value);
102
+ content = formatter. formatString (content);
105
103
expect (value, content, reason: 'Final output of $assetIdString differs.' );
106
104
});
107
105
}
108
106
107
+
108
+
109
109
Future checkAll (Map <String , String > files) {
110
110
return barback.results.first.then ((_) {
111
111
if (files == null ) return null ;
@@ -122,3 +122,49 @@ class TestHelper implements PackageProvider {
122
122
});
123
123
}
124
124
}
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 ' , '' );
0 commit comments