Index: test/escape_test.dart |
diff --git a/test/escape_test.dart b/test/escape_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..035967c0a5f9f80bbddc0fdf0a5f97e9e29d3e74 |
--- /dev/null |
+++ b/test/escape_test.dart |
@@ -0,0 +1,58 @@ |
+// Copyright (c) 2015, 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 matcher.escape_test; |
+ |
+import 'package:matcher/src/util.dart'; |
+import 'package:unittest/unittest.dart'; |
+ |
+void main() { |
+ group('escaping should work with', () { |
+ _testEscaping('no escaped chars', 'Hello, world!', 'Hello, world!'); |
+ _testEscaping('newline', '\n', r'\n'); |
+ _testEscaping('carriage return', '\r', r'\r'); |
+ _testEscaping('form feed', '\f', r'\f'); |
+ _testEscaping('backspace', '\b', r'\b'); |
+ _testEscaping('tab', '\t', r'\t'); |
+ _testEscaping('vertical tab', '\v', r'\v'); |
+ _testEscaping('escape combos', r'\n', r'\\n'); |
+ _testEscaping('All characters', |
+ 'A new line\nA charriage return\rA form feed\fA backspace\b' |
+ 'A tab\tA vertical tab\vA slash\\', |
+ r'A new line\nA charriage return\rA form feed\fA backspace\b' |
+ r'A tab\tA vertical tab\vA slash\\'); |
+ }); |
+ |
+ group('unequal strings remain unequal when escaped', () { |
+ _testUnequalStrings('with a newline', '\n', r'\n'); |
+ _testUnequalStrings('with slash literals', '\\', r'\\'); |
+ }); |
+} |
+ |
+/// Creates a [test] with name [name] that verifies [source] escapes to value |
+/// [target]. |
+void _testEscaping(String name, String source, String target) { |
+ test(name, () { |
+ var escaped = escape(source); |
+ expect(escaped == target, isTrue, |
+ reason: "Expected escaped value: $target\n" |
+ " Actual escaped value: $escaped"); |
+ }); |
+} |
+ |
+/// Creates a [test] with name [name] that ensures two different [String] values |
+/// [s1] and [s2] remain unequal when escaped. |
+void _testUnequalStrings(String name, String s1, String s2) { |
+ test(name, () { |
+ // Explicitly not using the equals matcher |
+ expect(s1 != s2, isTrue, reason: 'The source values should be unequal'); |
+ |
+ var escapedS1 = escape(s1); |
+ var escapedS2 = escape(s2); |
+ |
+ // Explicitly not using the equals matcher |
+ expect(escapedS1 != escapedS2, isTrue, |
+ reason: 'Unequal strings, when escaped, should remain unequal.'); |
+ }); |
+} |