-
Notifications
You must be signed in to change notification settings - Fork 0
/
escape_test.go
101 lines (72 loc) · 1.95 KB
/
escape_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package teamspeak
import (
"testing"
)
func escapeTestHelper(in, expected string, t *testing.T) {
out := Escape(in)
if out != expected {
t.Errorf("Escape(%v) should have returned %v, not %v", in, expected, out)
}
}
func unescapeTestHelper(in, expected string, t *testing.T) {
out := Unescape(in)
if out != expected {
t.Errorf("Unescape(%v) should have returned %v, not %v", in, expected, out)
}
}
func TestEscape(t *testing.T) {
// Test a vanilla string with no escaped characters
escapeTestHelper("foobarbaz", "foobarbaz", t)
// Backslash!
escapeTestHelper("\\", "\\\\", t)
// Slash
escapeTestHelper("/", "\\/", t)
// Whitespace
escapeTestHelper(" ", "\\s", t)
// Pipe
escapeTestHelper("|", "\\p", t)
// Bell
escapeTestHelper("\a", "\\a", t)
// Backspace
escapeTestHelper("\b", "\\b", t)
// Formfeed
escapeTestHelper("\f", "\\f", t)
// Newline
escapeTestHelper("\n", "\\n", t)
// Carraige Return
escapeTestHelper("\r", "\\r", t)
// Horizontal Tab
escapeTestHelper("\t", "\\t", t)
// Vertical Tab
escapeTestHelper("\v", "\\v", t)
// All the things
escapeTestHelper("foo\\/ |\a\b\f\n\r\t\v", "foo\\\\\\/\\s\\p\\a\\b\\f\\n\\r\\t\\v", t)
}
func TestUnescape(t *testing.T) {
// Test a vanilla string with no escaped characters
unescapeTestHelper("foobarbaz", "foobarbaz", t)
// Backslash!
unescapeTestHelper("\\\\", "\\", t)
// Slash
unescapeTestHelper("\\/", "/", t)
// Whitespace
unescapeTestHelper("\\s", " ", t)
// Pipe
unescapeTestHelper("\\p", "|", t)
// Bell
unescapeTestHelper("\\a", "\a", t)
// Backspace
unescapeTestHelper("\\b", "\b", t)
// Formfeed
unescapeTestHelper("\\f", "\f", t)
// Newline
unescapeTestHelper("\\n", "\n", t)
// Carraige Return
unescapeTestHelper("\\r", "\r", t)
// Horizontal Tab
unescapeTestHelper("\\t", "\t", t)
// Vertical Tab
unescapeTestHelper("\\v", "\v", t)
// All the things
unescapeTestHelper("foo\\\\\\/\\s\\p\\a\\b\\f\\n\\r\\t\\v", "foo\\/ |\a\b\f\n\r\t\v", t)
}