-
Notifications
You must be signed in to change notification settings - Fork 16
/
string_slice_test.go
110 lines (91 loc) · 4.2 KB
/
string_slice_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
102
103
104
105
106
107
108
109
110
package goflags
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNormalizedStringSlice(t *testing.T) {
expectedABC := []string{"aa", "bb", "cc"}
expectedFilePath := []string{"/root/home/file0"}
validTests := map[string][]string{
"aa,bb,cc": expectedABC,
" aa, bb, cc ": expectedABC,
" `aa`, 'bb', \"cc\" ": expectedABC,
" `aa`, bb, \"cc\" ": expectedABC,
" `aa, bb, cc\" ": expectedABC,
" \"aa\", bb, cc\" ": expectedABC,
"\n aa, \tbb, cc\r ": expectedABC,
"\"value1\",value,'value3'": {"value1", "value", "value3"},
"\"value1\",VALUE,'value3'": {"value1", "value", "value3"},
"\"/root/home/file0\"": expectedFilePath,
"'/root/home/file0'": expectedFilePath,
"`/root/home/file0`": expectedFilePath,
"\"/root/home/file0\",": expectedFilePath,
",\"/root/home/file0\",": expectedFilePath,
",\"/root/home/file0\"": expectedFilePath,
",,\"/root/home/file0\"": expectedFilePath,
"\"\",,\"/root/home/file0\"": expectedFilePath,
"\" \",\"/root/home/file0\"": expectedFilePath,
"\"/root/home/file0\",\"\"": expectedFilePath,
"/root/home/file0": expectedFilePath,
"\"/root/home/file2\",\"/root/home/file3\"": {"/root/home/file2", "/root/home/file3"},
"/root/home/file4,/root/home/file5": {"/root/home/file4", "/root/home/file5"},
"\"/root/home/file4,/root/home/file5\"": {"/root/home/file4,/root/home/file5"},
"\"/root/home/file6\",/root/home/file7": {"/root/home/file6", "/root/home/file7"},
"\"c:\\my files\\bug,bounty\"": {"c:\\my files\\bug,bounty"},
"\"c:\\my files\\bug,bounty\",c:\\my_files\\bug bounty": {"c:\\my files\\bug,bounty", "c:\\my_files\\bug bounty"},
}
for value, expected := range validTests {
result, err := ToStringSlice(value, NormalizedStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, result, expected)
}
invalidTests := []string{
"\"/root/home/file0",
"'/root/home/file0",
"`/root/home/file0",
"\"/root/home/file0'",
"\"/root/home/file0`",
}
for _, value := range invalidTests {
result, err := ToStringSlice(value, NormalizedStringSliceOptions)
assert.Nil(t, result)
assert.NotNil(t, err)
}
}
func TestNormalizedOriginalStringSlice(t *testing.T) {
result, err := ToStringSlice("/Users/Home/Test/test.yaml", NormalizedOriginalStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"/Users/Home/Test/test.yaml"}, result, "could not get correct path")
result, err = ToStringSlice("'test user'", NormalizedOriginalStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"test user"}, result, "could not get correct path")
}
func TestFileNormalizedStringSliceOptions(t *testing.T) {
result, err := ToStringSlice("/Users/Home/Test/test.yaml", FileNormalizedStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"/users/home/test/test.yaml"}, result, "could not get correct path")
result, err = ToStringSlice("'Test User'", FileNormalizedStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"test user"}, result, "could not get correct path")
}
func TestFileStringSliceOptions(t *testing.T) {
filename := "test.txt"
_ = os.WriteFile(filename, []byte("value1,value2\nvalue3"), 0644)
defer os.RemoveAll(filename)
result, err := ToStringSlice(filename, FileStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"value1,value2", "value3"}, result, "could not get correct path")
// command line input value
result, err = ToStringSlice("string:\"contains, comma and quotes.\"", FileStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"string:\"contains, comma and quotes.\""}, result, "could not get correct path")
}
func TestFileNormalizedOriginalStringSliceOptions(t *testing.T) {
result, err := ToStringSlice("/Users/Home/Test/test.yaml", FileNormalizedOriginalStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"/Users/Home/Test/test.yaml"}, result, "could not get correct path")
result, err = ToStringSlice("'Test User'", FileNormalizedOriginalStringSliceOptions)
assert.Nil(t, err)
assert.Equal(t, []string{"Test User"}, result, "could not get correct path")
}