-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
92 lines (77 loc) · 2.14 KB
/
utils_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
package jsonstruct_test
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/cneill/jsonstruct"
)
func TestGetGoName(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
expected string
}{
{"separators1", "this_is-a.test_name", "ThisIsATestName"},
{"separators2", "remote_-.URL", "RemoteURL"},
{"weirdcase", "ThiSiSaNumber2", "ThiSiSaNumber2"},
{"separators_with_initialism", "This_Is_An_ID", "ThisIsAnID"},
{"underscore_start", "_underscored", "Underscored"},
{"garbage_characters", "($@%)@$%)(@", "Unknown"},
{"garbage_characters_with_content", "@)(#$)@(#$)@#($garbage@#)$@)#($@)#($", "Garbage"},
{"garbage_separator", "@t@e@s@t", "Test"},
{"spaces", " spaces", "Spaces"},
{"multiple_spaces", "here are some spaces", "HereAreSomeSpaces"},
{"leading_number", "0", "JSON0"},
{"dot_special_case", ".", "Dot"},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.expected, jsonstruct.GetGoName(test.input))
})
}
}
func FuzzGetGoName(f *testing.F) {
validRegex := regexp.MustCompile(`^[A-Z][a-zA-Z0-9]*$`)
seeds := []string{
"this_is-a.test_name",
"remote_-.URL",
"ThiSiSaNumber2",
"This_Is_An_ID",
"_underscored",
"($@%)@$%)(@",
"@)(#$)@(#$)@#($garbage@#)$@)#($@)#($",
"@t@e@s@t",
" spaces",
"here are some spaces",
}
for _, seed := range seeds {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, input string) {
transformed := jsonstruct.GetGoName(input)
assert.True(t, validRegex.MatchString(transformed) || transformed == "")
})
}
func TestGetFileGoName(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
expected string
}{
{"simple", "file_name.json", "FileName"},
{"path", "/path/to/file.json", "File"},
{"path_no_extension", "/path/to/file", "File"},
// TODO: handle multiple extensions {"json.gz", "file_name.json.gz", "FileName"},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.expected, jsonstruct.GetFileGoName(test.input))
})
}
}