1
1
package multiformatname_test
2
2
3
3
import (
4
- "fmt"
5
4
"testing"
6
5
7
6
"github.com/stretchr/testify/require"
@@ -10,59 +9,178 @@ import (
10
9
"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
11
10
)
12
11
13
- func TestNewMultiFormatName (t * testing.T ) {
14
- // [valueToTest, lowerCamel, upperCamel, kebabCase]
15
- cases := [][6 ]string {
16
- {"foo" , "foo" , "Foo" , "foo" , "foo" , "foo" },
17
- {"fooBar" , "fooBar" , "FooBar" , "foo-bar" , "foo_bar" , "foobar" },
18
- {"foo-bar" , "fooBar" , "FooBar" , "foo-bar" , "foo_bar" , "foobar" },
19
- {"foo_bar" , "fooBar" , "FooBar" , "foo-bar" , "foo_bar" , "foobar" },
20
- {"foo_barFoobar" , "fooBarFoobar" , "FooBarFoobar" , "foo-bar-foobar" , "foo_bar_foobar" , "foobarfoobar" },
21
- {"foo_-_bar" , "fooBar" , "FooBar" , "foo---bar" , "foo___bar" , "foobar" },
22
- {"foo_-_Bar" , "fooBar" , "FooBar" , "foo---bar" , "foo___bar" , "foobar" },
23
- {"fooBAR" , "fooBar" , "FooBar" , "foo-bar" , "foo_bar" , "foobar" },
24
- {"fooBar123" , "fooBar123" , "FooBar123" , "foo-bar-123" , "foo_bar_123" , "foobar123" },
12
+ func TestNewName (t * testing.T ) {
13
+ tests := []struct {
14
+ name string
15
+ arg string
16
+ want multiformatname.Name
17
+ err error
18
+ }{
19
+ {
20
+ name : "simple lowercase name" ,
21
+ arg : "foo" ,
22
+ want : multiformatname.Name {
23
+ Original : "foo" ,
24
+ LowerCamel : "foo" ,
25
+ UpperCamel : "Foo" ,
26
+ PascalCase : "Foo" ,
27
+ LowerCase : "foo" ,
28
+ UpperCase : "FOO" ,
29
+ Kebab : "foo" ,
30
+ Snake : "foo" ,
31
+ },
32
+ },
33
+ {
34
+ name : "camelCase name" ,
35
+ arg : "fooBar" ,
36
+ want : multiformatname.Name {
37
+ Original : "fooBar" ,
38
+ LowerCamel : "fooBar" ,
39
+ UpperCamel : "FooBar" ,
40
+ PascalCase : "FooBar" ,
41
+ LowerCase : "foobar" ,
42
+ UpperCase : "FOOBAR" ,
43
+ Kebab : "foo-bar" ,
44
+ Snake : "foo_bar" ,
45
+ },
46
+ },
47
+ {
48
+ name : "kebab-case name" ,
49
+ arg : "foo-bar" ,
50
+ want : multiformatname.Name {
51
+ Original : "foo-bar" ,
52
+ LowerCamel : "fooBar" ,
53
+ UpperCamel : "FooBar" ,
54
+ PascalCase : "FooBar" ,
55
+ LowerCase : "foobar" ,
56
+ UpperCase : "FOOBAR" ,
57
+ Kebab : "foo-bar" ,
58
+ Snake : "foo_bar" ,
59
+ },
60
+ },
61
+ {
62
+ name : "snake_case name" ,
63
+ arg : "foo_bar" ,
64
+ want : multiformatname.Name {
65
+ Original : "foo_bar" ,
66
+ LowerCamel : "fooBar" ,
67
+ UpperCamel : "FooBar" ,
68
+ PascalCase : "FooBar" ,
69
+ LowerCase : "foobar" ,
70
+ UpperCase : "FOOBAR" ,
71
+ Kebab : "foo-bar" ,
72
+ Snake : "foo_bar" ,
73
+ },
74
+ },
75
+ {
76
+ name : "mixed snake_case and camelCase name" ,
77
+ arg : "foo_barFoobar" ,
78
+ want : multiformatname.Name {
79
+ Original : "foo_barFoobar" ,
80
+ LowerCamel : "fooBarFoobar" ,
81
+ UpperCamel : "FooBarFoobar" ,
82
+ PascalCase : "FooBarFoobar" ,
83
+ LowerCase : "foobarfoobar" ,
84
+ UpperCase : "FOOBARFOOBAR" ,
85
+ Kebab : "foo-bar-foobar" ,
86
+ Snake : "foo_bar_foobar" ,
87
+ },
88
+ },
89
+ {
90
+ name : "mixed underscores and dashes" ,
91
+ arg : "foo_-_bar" ,
92
+ want : multiformatname.Name {
93
+ Original : "foo_-_bar" ,
94
+ LowerCamel : "fooBar" ,
95
+ UpperCamel : "Foo__Bar" ,
96
+ PascalCase : "FooBar" ,
97
+ LowerCase : "foobar" ,
98
+ UpperCase : "FOOBAR" ,
99
+ Kebab : "foo---bar" ,
100
+ Snake : "foo___bar" ,
101
+ },
102
+ },
103
+ {
104
+ name : "mixed underscores, dashes, and numbers" ,
105
+ arg : "foo_-_Bar1" ,
106
+ want : multiformatname.Name {
107
+ Original : "foo_-_Bar1" ,
108
+ LowerCamel : "fooBar1" ,
109
+ UpperCamel : "Foo__Bar_1" ,
110
+ PascalCase : "FooBar1" ,
111
+ LowerCase : "foobar1" ,
112
+ UpperCase : "FOOBAR1" ,
113
+ Kebab : "foo---bar-1" ,
114
+ Snake : "foo___bar_1" ,
115
+ },
116
+ },
117
+ {
118
+ name : "uppercase variant in simple name" ,
119
+ arg : "fooBAR" ,
120
+ want : multiformatname.Name {
121
+ Original : "fooBAR" ,
122
+ LowerCamel : "fooBar" ,
123
+ UpperCamel : "FooBar" ,
124
+ PascalCase : "FooBar" ,
125
+ LowerCase : "foobar" ,
126
+ UpperCase : "FOOBAR" ,
127
+ Kebab : "foo-bar" ,
128
+ Snake : "foo_bar" ,
129
+ },
130
+ },
131
+ {
132
+ name : "uppercase variant with starting capital" ,
133
+ arg : "FooBAR" ,
134
+ want : multiformatname.Name {
135
+ Original : "FooBAR" ,
136
+ LowerCamel : "fooBar" ,
137
+ UpperCamel : "FooBar" ,
138
+ PascalCase : "FooBar" ,
139
+ LowerCase : "foobar" ,
140
+ UpperCase : "FOOBAR" ,
141
+ Kebab : "foo-bar" ,
142
+ Snake : "foo_bar" ,
143
+ },
144
+ },
145
+ {
146
+ name : "camelCase name with numbers" ,
147
+ arg : "fooBar123" ,
148
+ want : multiformatname.Name {
149
+ Original : "fooBar123" ,
150
+ LowerCamel : "fooBar123" ,
151
+ UpperCamel : "FooBar_123" ,
152
+ PascalCase : "FooBar123" ,
153
+ LowerCase : "foobar123" ,
154
+ UpperCase : "FOOBAR123" ,
155
+ Kebab : "foo-bar-123" ,
156
+ Snake : "foo_bar_123" ,
157
+ },
158
+ },
159
+ {
160
+ name : "multiple numbers in name" ,
161
+ arg : "para_2_m_s_43_tr_1" ,
162
+ want : multiformatname.Name {
163
+ Original : "para_2_m_s_43_tr_1" ,
164
+ LowerCamel : "para2MS43Tr1" ,
165
+ UpperCamel : "Para_2MS_43Tr_1" ,
166
+ PascalCase : "Para2MS43Tr1" ,
167
+ LowerCase : "para2ms43tr1" ,
168
+ UpperCase : "PARA2MS43TR1" ,
169
+ Kebab : "para-2-m-s-43-tr-1" ,
170
+ Snake : "para_2_m_s_43_tr_1" ,
171
+ },
172
+ },
25
173
}
26
-
27
- // test cases
28
- for _ , testCase := range cases {
29
- name , err := multiformatname .NewName (testCase [0 ])
30
- require .NoError (t , err )
31
- require .Equal (
32
- t ,
33
- testCase [0 ],
34
- name .Original ,
35
- )
36
- require .Equal (
37
- t ,
38
- testCase [1 ],
39
- name .LowerCamel ,
40
- fmt .Sprintf ("%s should be converted to the correct lower camel format" , testCase [0 ]),
41
- )
42
- require .Equal (
43
- t ,
44
- testCase [2 ],
45
- name .UpperCamel ,
46
- fmt .Sprintf ("%s should be converted the correct upper camel format" , testCase [0 ]),
47
- )
48
- require .Equal (
49
- t ,
50
- testCase [3 ],
51
- name .Kebab ,
52
- fmt .Sprintf ("%s should be converted the correct kebab format" , testCase [0 ]),
53
- )
54
- require .Equal (
55
- t ,
56
- testCase [4 ],
57
- name .Snake ,
58
- fmt .Sprintf ("%s should be converted the correct snake format" , testCase [0 ]),
59
- )
60
- require .Equal (
61
- t ,
62
- testCase [5 ],
63
- name .LowerCase ,
64
- fmt .Sprintf ("%s should be converted the correct lowercase format" , testCase [0 ]),
65
- )
174
+ for _ , tt := range tests {
175
+ t .Run (tt .name , func (t * testing.T ) {
176
+ got , err := multiformatname .NewName (tt .arg )
177
+ if tt .err != nil {
178
+ require .ErrorIs (t , err , tt .err )
179
+ return
180
+ }
181
+ require .NoError (t , err )
182
+ require .EqualValues (t , tt .want , got )
183
+ })
66
184
}
67
185
}
68
186
0 commit comments