@@ -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.
2622class 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 ' , '' );
0 commit comments