Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Unified Diff: lib/src/test_harness.dart

Issue 897453003: add whitespace/newline stripping options to TestHelper (Closed) Base URL: git@github.com:dart-lang/code-transformers.git@master
Patch Set: update pubspec/changelog Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | lib/tests.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/test_harness.dart
diff --git a/lib/src/test_harness.dart b/lib/src/test_harness.dart
index 334020470523cbaec4e7c587317f0cb7c07d9336..a25cf527369e97a48bef77043324fa69be2bbb51 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<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);
@@ -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;
@@ -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', '');
« no previous file with comments | « CHANGELOG.md ('k') | lib/tests.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698