OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library matcher.escape_test; | |
6 | |
7 import 'package:matcher/src/util.dart'; | 5 import 'package:matcher/src/util.dart'; |
8 import 'package:test/test.dart'; | 6 import 'package:test/test.dart'; |
9 | 7 |
10 void main() { | 8 void main() { |
11 group('escaping should work with', () { | 9 group('escaping should work with', () { |
12 _testEscaping('no escaped chars', 'Hello, world!', 'Hello, world!'); | 10 _testEscaping('no escaped chars', 'Hello, world!', 'Hello, world!'); |
13 _testEscaping('newline', '\n', r'\n'); | 11 _testEscaping('newline', '\n', r'\n'); |
14 _testEscaping('carriage return', '\r', r'\r'); | 12 _testEscaping('carriage return', '\r', r'\r'); |
15 _testEscaping('form feed', '\f', r'\f'); | 13 _testEscaping('form feed', '\f', r'\f'); |
16 _testEscaping('backspace', '\b', r'\b'); | 14 _testEscaping('backspace', '\b', r'\b'); |
17 _testEscaping('tab', '\t', r'\t'); | 15 _testEscaping('tab', '\t', r'\t'); |
18 _testEscaping('vertical tab', '\v', r'\v'); | 16 _testEscaping('vertical tab', '\v', r'\v'); |
19 _testEscaping('null byte', '\x00', r'\x00'); | 17 _testEscaping('null byte', '\x00', r'\x00'); |
20 _testEscaping('ASCII control character', '\x11', r'\x11'); | 18 _testEscaping('ASCII control character', '\x11', r'\x11'); |
21 _testEscaping('delete', '\x7F', r'\x7F'); | 19 _testEscaping('delete', '\x7F', r'\x7F'); |
22 _testEscaping('escape combos', r'\n', r'\\n'); | 20 _testEscaping('escape combos', r'\n', r'\\n'); |
23 _testEscaping('All characters', | 21 _testEscaping( |
| 22 'All characters', |
24 'A new line\nA charriage return\rA form feed\fA backspace\b' | 23 'A new line\nA charriage return\rA form feed\fA backspace\b' |
25 'A tab\tA vertical tab\vA slash\\A null byte\x00A control char\x1D' | 24 'A tab\tA vertical tab\vA slash\\A null byte\x00A control char\x1D' |
26 'A delete\x7F', | 25 'A delete\x7F', |
27 r'A new line\nA charriage return\rA form feed\fA backspace\b' | 26 r'A new line\nA charriage return\rA form feed\fA backspace\b' |
28 r'A tab\tA vertical tab\vA slash\\A null byte\x00A control char\x1D' | 27 r'A tab\tA vertical tab\vA slash\\A null byte\x00A control char\x1D' |
29 r'A delete\x7F'); | 28 r'A delete\x7F'); |
30 }); | 29 }); |
31 | 30 |
32 group('unequal strings remain unequal when escaped', () { | 31 group('unequal strings remain unequal when escaped', () { |
33 _testUnequalStrings('with a newline', '\n', r'\n'); | 32 _testUnequalStrings('with a newline', '\n', r'\n'); |
34 _testUnequalStrings('with slash literals', '\\', r'\\'); | 33 _testUnequalStrings('with slash literals', '\\', r'\\'); |
35 }); | 34 }); |
36 } | 35 } |
37 | 36 |
38 /// Creates a [test] with name [name] that verifies [source] escapes to value | 37 /// Creates a [test] with name [name] that verifies [source] escapes to value |
39 /// [target]. | 38 /// [target]. |
40 void _testEscaping(String name, String source, String target) { | 39 void _testEscaping(String name, String source, String target) { |
41 test(name, () { | 40 test(name, () { |
42 var escaped = escape(source); | 41 var escaped = escape(source); |
43 expect(escaped == target, isTrue, | 42 expect(escaped == target, isTrue, |
44 reason: "Expected escaped value: $target\n" | 43 reason: "Expected escaped value: $target\n" |
45 " Actual escaped value: $escaped"); | 44 " Actual escaped value: $escaped"); |
46 }); | 45 }); |
47 } | 46 } |
48 | 47 |
49 /// Creates a [test] with name [name] that ensures two different [String] values | 48 /// Creates a [test] with name [name] that ensures two different [String] values |
50 /// [s1] and [s2] remain unequal when escaped. | 49 /// [s1] and [s2] remain unequal when escaped. |
51 void _testUnequalStrings(String name, String s1, String s2) { | 50 void _testUnequalStrings(String name, String s1, String s2) { |
52 test(name, () { | 51 test(name, () { |
53 // Explicitly not using the equals matcher | 52 // Explicitly not using the equals matcher |
54 expect(s1 != s2, isTrue, reason: 'The source values should be unequal'); | 53 expect(s1 != s2, isTrue, reason: 'The source values should be unequal'); |
55 | 54 |
56 var escapedS1 = escape(s1); | 55 var escapedS1 = escape(s1); |
57 var escapedS2 = escape(s2); | 56 var escapedS2 = escape(s2); |
58 | 57 |
59 // Explicitly not using the equals matcher | 58 // Explicitly not using the equals matcher |
60 expect(escapedS1 != escapedS2, isTrue, | 59 expect(escapedS1 != escapedS2, isTrue, |
61 reason: 'Unequal strings, when escaped, should remain unequal.'); | 60 reason: 'Unequal strings, when escaped, should remain unequal.'); |
62 }); | 61 }); |
63 } | 62 } |
OLD | NEW |