-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcaser_test.go
121 lines (113 loc) · 3.22 KB
/
caser_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
package strcase
import (
"reflect"
"testing"
"unicode"
)
func TestCaserAll(t *testing.T) {
c := NewCaser(true, nil, nil)
type data struct {
input string
snake string
SNAKE string
kebab string
KEBAB string
pascal string
camel string
title string
}
for _, test := range []data{
{
input: "Hello world!",
snake: "hello_world!",
SNAKE: "HELLO_WORLD!",
kebab: "hello-world!",
KEBAB: "HELLO-WORLD!",
pascal: "HelloWorld!",
camel: "helloWorld!",
title: "Hello World!",
},
} {
t.Run(test.input, func(t *testing.T) {
output := data{
input: test.input,
snake: c.ToSnake(test.input),
SNAKE: c.ToSNAKE(test.input),
kebab: c.ToKebab(test.input),
KEBAB: c.ToKEBAB(test.input),
pascal: c.ToPascal(test.input),
camel: c.ToCamel(test.input),
title: c.ToCase(test.input, TitleCase, ' '),
}
assertTrue(t, test == output)
})
}
}
func TestNewCaser(t *testing.T) {
t.Run("Has defaults when unspecified", func(t *testing.T) {
c := NewCaser(true, nil, nil)
assertTrue(t, reflect.DeepEqual(golintInitialisms, c.initialisms))
assertTrue(t, c.splitFn != nil)
})
t.Run("Merges", func(t *testing.T) {
c := NewCaser(true, map[string]bool{"SSL": true, "HTML": false}, nil)
assertTrue(t, !reflect.DeepEqual(golintInitialisms, c.initialisms))
assertTrue(t, c.initialisms["UUID"])
assertTrue(t, c.initialisms["SSL"])
assertTrue(t, !c.initialisms["HTML"])
assertTrue(t, c.splitFn != nil)
})
t.Run("No Go initialisms", func(t *testing.T) {
c := NewCaser(false, map[string]bool{"SSL": true, "HTML": false}, NewSplitFn([]rune{' '}))
assertTrue(t, !reflect.DeepEqual(golintInitialisms, c.initialisms))
assertTrue(t, !c.initialisms["UUID"])
assertTrue(t, c.initialisms["SSL"])
assertTrue(t, !c.initialisms["HTML"])
assertEqual(t, "hTml with SSL", c.ToCase("hTml with SsL", Original, ' '))
assertTrue(t, c.splitFn != nil)
})
t.Run("Preserve number formatting", func(t *testing.T) {
c := NewCaser(
false,
map[string]bool{"SSL": true, "HTML": false},
NewSplitFn(
[]rune{'*', '.', ','},
SplitCase,
SplitAcronym,
PreserveNumberFormatting,
))
assertTrue(t, !reflect.DeepEqual(golintInitialisms, c.initialisms))
assertEqual(t, "http200", c.ToSnake("http200"))
assertEqual(t, "VERSION2.3_R3_8A_HTTP_ERROR_CODE", c.ToSNAKE("version2.3R3*8a,HTTPErrorCode"))
})
t.Run("Preserve number formatting and split before and after number", func(t *testing.T) {
c := NewCaser(
false,
map[string]bool{"SSL": true, "HTML": false},
NewSplitFn(
[]rune{'*', '.', ','},
SplitCase,
SplitAcronym,
PreserveNumberFormatting,
SplitBeforeNumber,
SplitAfterNumber,
))
assertEqual(t, "http_200", c.ToSnake("http200"))
assertEqual(t, "VERSION_2.3_R_3_8_A_HTTP_ERROR_CODE", c.ToSNAKE("version2.3R3*8a,HTTPErrorCode"))
})
t.Run("Skip non letters", func(t *testing.T) {
c := NewCaser(
false,
nil,
func(prec, curr, next rune) SplitAction {
if unicode.IsNumber(curr) {
return Noop
} else if unicode.IsSpace(curr) {
return SkipSplit
}
return Skip
})
assertEqual(t, "", c.ToSnake(""))
assertEqual(t, "1130_23_2009", c.ToCase("DateTime: 11:30 AM May 23rd, 2009", Original, '_'))
})
}