-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_test.go
177 lines (146 loc) · 4.11 KB
/
generate_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package facto
import (
_ "embed"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
func TestCammelCase(t *testing.T) {
cases := []struct {
in, want string
}{
{"aaa", "Aaa"},
{"the cow", "TheCow"},
{"the_cow", "TheCow"},
{"the-cow", "TheCow"},
}
for _, v := range cases {
t.Run(v.in, func(t *testing.T) {
out := camelCase(v.in)
if out != v.want {
t.Errorf("camelCase(%q) = %q, want %q", v.in, out, v.want)
}
})
}
}
func TestSnakeCase(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", ""},
{"camelCase", "camel_case"},
{"PascalCase", "pascal_case"},
{"snake_case", "snake_case"},
{"Pascal_Snake", "pascal_snake"},
{"SCREAMING_SNAKE", "screaming_snake"},
{"kebab-case", "kebab_case"},
{"Pascal-Kebab", "pascal_kebab"},
{"SCREAMING-KEBAB", "screaming_kebab"},
{"A", "a"},
{"AA", "aa"},
{"AAA", "aaa"},
{"AAAA", "aaaa"},
{"AaAa", "aa_aa"},
{"BatteryLifeValue", "battery_life_value"},
{"Id0Value", "id0_value"},
{"ID0Value", "id0_value"},
}
for _, v := range tests {
result := snakeCase(v.input)
if result != v.expected {
t.Errorf("snakeCase(%q) = %q, want %q", v.input, result, v.expected)
}
}
}
func TestGenerate(t *testing.T) {
dir := os.TempDir()
os.Chdir(dir)
err := Generate(dir, []string{"generate", "user"})
if err != nil {
t.Fatalf("Err should be nil, got %v", err)
}
data, err := ioutil.ReadFile(filepath.Join(dir, "factories", "user.go"))
if err != nil {
t.Fatalf("could not read factory file, got %v", err)
}
if !strings.Contains(string(data), `package factories`) {
t.Errorf("factory file should contain package name, got: \n%s", data)
}
if !strings.Contains(string(data), `func UserFactory(h facto.Helper)`) {
t.Errorf("factory file should contain func definition, got: \n%s", data)
}
}
func TestGenerateDirExists(t *testing.T) {
d, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatalf("Could not create temp dir: %v", err)
}
os.Chdir(d)
deleteAll := func() {
dir, _ := ioutil.ReadDir(d)
for _, f := range dir {
os.RemoveAll(path.Join([]string{d, f.Name()}...))
}
}
t.Run("HappyPath", func(t *testing.T) {
t.Cleanup(deleteAll)
err := Generate(d, []string{"generate", "user"})
if err != nil {
t.Fatalf("Err should be nil, got %v", err)
}
data, err := ioutil.ReadFile(filepath.Join(d, "factories", "user.go"))
if err != nil {
t.Fatalf("could not read factory file, got %v", err)
}
if !strings.Contains(string(data), `package factories`) {
t.Errorf("factory file should contain package name, got: \n%s", data)
}
if !strings.Contains(string(data), `func UserFactory(h facto.Helper)`) {
t.Errorf("factory file should contain func definition, got: \n%s", data)
}
})
t.Run("FolderExists", func(t *testing.T) {
t.Cleanup(deleteAll)
err := os.MkdirAll(filepath.Join(d, "factories"), 0777)
if err != nil {
t.Fatal("could not create folder")
}
err = Generate(d, []string{"generate", "user"})
if err != nil {
t.Fatalf("Err should be nil, got %v", err)
}
data, err := ioutil.ReadFile(filepath.Join(d, "factories", "user.go"))
if err != nil {
t.Fatalf("could not read factory file, got %v", err)
}
if !strings.Contains(string(data), `func UserFactory(h facto.Helper)`) {
t.Errorf("factory file should contain func definition, got: \n%s", data)
}
})
t.Run("FolderAndFileExists", func(t *testing.T) {
t.Cleanup(deleteAll)
err := os.MkdirAll(filepath.Join(d, "factories"), 0777)
if err != nil {
t.Fatal("could not create folder")
}
err = ioutil.WriteFile(filepath.Join(d, "factories", "user.go"), []byte(`package factories`), 0777)
if err != nil {
t.Fatalf("could not write the file")
}
err = Generate(d, []string{"generate", "user"})
if err != nil {
t.Fatalf("Err should be nil, got %v", err)
}
data, err := ioutil.ReadFile(filepath.Join(d, "factories", "user.go"))
if err != nil {
t.Fatalf("could not read factory file, got %v", err)
}
if !strings.Contains(string(data), `func UserFactory(h facto.Helper)`) {
t.Errorf("factory file should contain func definition, got: \n%s", data)
}
})
}