-
Notifications
You must be signed in to change notification settings - Fork 3
/
internal_test.go
42 lines (38 loc) · 1010 Bytes
/
internal_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
// Copyright (c) 2015, Michael J. Fromberger
package shell
import (
"fmt"
"testing"
)
func TestQuote(t *testing.T) {
type testCase struct{ in, want string }
tests := []testCase{
{"", "''"}, // empty is special
{"abc", "abc"}, // nothing to quote
{"--flag", "--flag"}, // "
{"'abc", `\'abc`}, // single quote only
{"abc'", `abc\'`}, // "
{`shan't`, `shan\'t`}, // "
{"--flag=value", `'--flag=value'`},
{"a b\tc", "'a b\tc'"},
{`a"b"c`, `'a"b"c'`},
{`'''`, `\'\'\'`},
{`\`, `'\'`},
{`'a=b`, `\''a=b'`}, // quotes and other stuff
{`a='b`, `'a='\''b'`}, // "
{`a=b'`, `'a=b'\'`}, // "
}
// Verify that all the designated special characters get quoted.
for _, c := range shouldQuote + mustQuote {
tests = append(tests, testCase{
in: string(c),
want: fmt.Sprintf(`'%c'`, c),
})
}
for _, test := range tests {
got := Quote(test.in)
if got != test.want {
t.Errorf("Quote %q: got %q, want %q", test.in, got, test.want)
}
}
}