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

Unified Diff: lib/src/matcher/util.dart

Issue 1135653004: pkg/(unit)test: include a copy of old matcher (Closed) Base URL: https://github.com/dart-lang/test.git@stable
Patch Set: another nit Created 5 years, 7 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 | « lib/src/matcher/throws_matchers.dart ('k') | lib/src/simple_configuration.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/matcher/util.dart
diff --git a/lib/src/matcher/util.dart b/lib/src/matcher/util.dart
new file mode 100644
index 0000000000000000000000000000000000000000..b98c1a84af8ac8106bae34571f41e1b4bc5fd251
--- /dev/null
+++ b/lib/src/matcher/util.dart
@@ -0,0 +1,65 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library unittest.matcher.util;
+
+import 'core_matchers.dart';
+import 'interfaces.dart';
+
+/// A [Map] between whitespace characters and their escape sequences.
+const _escapeMap = const {
+ '\n': r'\n',
+ '\r': r'\r',
+ '\f': r'\f',
+ '\b': r'\b',
+ '\t': r'\t',
+ '\v': r'\v',
+ '\x7F': r'\x7F', // delete
+};
+
+/// A [RegExp] that matches whitespace characters that should be escaped.
+final _escapeRegExp = new RegExp(
+ "[\\x00-\\x07\\x0E-\\x1F${_escapeMap.keys.map(_getHexLiteral).join()}]");
+
+/// Useful utility for nesting match states.
+void addStateInfo(Map matchState, Map values) {
+ var innerState = new Map.from(matchState);
+ matchState.clear();
+ matchState['state'] = innerState;
+ matchState.addAll(values);
+}
+
+/// Takes an argument and returns an equivalent [Matcher].
+///
+/// If the argument is already a matcher this does nothing,
+/// else if the argument is a function, it generates a predicate
+/// function matcher, else it generates an equals matcher.
+Matcher wrapMatcher(x) {
+ if (x is Matcher) {
+ return x;
+ } else if (x is Function) {
+ return predicate(x);
+ } else {
+ return equals(x);
+ }
+}
+
+/// Returns [str] with all whitespace characters represented as their escape
+/// sequences.
+///
+/// Backslash characters are escaped as `\\`
+String escape(String str) {
+ str = str.replaceAll('\\', r'\\');
+ return str.replaceAllMapped(_escapeRegExp, (match) {
+ var mapped = _escapeMap[match[0]];
+ if (mapped != null) return mapped;
+ return _getHexLiteral(match[0]);
+ });
+}
+
+/// Given single-character string, return the hex-escaped equivalent.
+String _getHexLiteral(String input) {
+ int rune = input.runes.single;
+ return r'\x' + rune.toRadixString(16).toUpperCase().padLeft(2, '0');
+}
« no previous file with comments | « lib/src/matcher/throws_matchers.dart ('k') | lib/src/simple_configuration.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698