-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplanner_enum_test.go
More file actions
145 lines (136 loc) · 4.28 KB
/
Copy pathplanner_enum_test.go
File metadata and controls
145 lines (136 loc) · 4.28 KB
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
package morph
import (
"maps"
"slices"
"testing"
"github.com/seeruk/morph/plan"
"github.com/seeruk/morph/types"
"github.com/stretchr/testify/assert"
)
func Test_normalizeEnumConstants(t *testing.T) {
tt := []struct {
name string
pattern string
in []types.ConstantDecl
out []string
ambiguous map[string][]string
diags []plan.Diagnostic
}{
{
name: "supported auto cases",
in: []types.ConstantDecl{
testConstantDecl("Example", "ExampleFoo", "foo"),
testConstantDecl("Example", "Example_BAR", "bar"),
testConstantDecl("TestExample", "TestExample_TEST_EXAMPLE_EnumValue", "enum_value"),
testConstantDecl("TestExample", "TestExample_TEST_EXAMPLE_extremelyWeird-value_kind", "unrelated_actual_value"),
},
out: []string{
"FOO",
"BAR",
"ENUM_VALUE",
"EXTREMELY_WEIRD_VALUE_KIND",
},
},
{
name: "protobuf-style pattern cases",
pattern: "{{ .Type.Pascal }}_{{ .Type.Screaming }}_{{ .Value.Screaming }}",
in: []types.ConstantDecl{
testConstantDecl("TestExample", "TestExample_TEST_EXAMPLE_SOME_VALUE", "some_value"),
testConstantDecl("TestExample", "TestExample_TEST_EXAMPLE_ANOTHER_VALUE", "value_another"),
},
out: []string{
"SOME_VALUE",
"ANOTHER_VALUE",
},
},
{
name: "multi-value pattern cases",
pattern: "{{ .Type.Pascal }}_{{ .Value.Screaming }}_{{ .Value.Camel }}",
in: []types.ConstantDecl{
testConstantDecl("TestExample", "TestExample_SOME_VALUE_someValue", "some_value"),
testConstantDecl("TestExample", "TestExample_ANOTHER_VALUE_anotherValue", "value_another"),
},
out: []string{
"SOME_VALUE",
"ANOTHER_VALUE",
},
},
{
name: "invalid screaming-screaming pattern",
pattern: "{{ .Type.Pascal }}_{{ .Value.Screaming }}_{{ .Value.Screaming }}",
in: []types.ConstantDecl{
testConstantDecl("TestExample", "TestExample_SOME_VALUE_SOME_VALUE", "some_value"),
testConstantDecl("TestExample", "TestExample_ANOTHER_VALUE_ANOTHER_VALUE", "value_another"),
},
diags: []plan.Diagnostic{
{
Level: plan.DiagnosticLevelFatal,
Path: "TestExample :: enum value TestExample_SOME_VALUE_SOME_VALUE",
Message: "failed to normalize enum constant name: enum pattern template validation failed: ambiguous enum value boundary between Screaming and Screaming using \"_\"; configure enum.patterns or enum.values",
},
{
Level: plan.DiagnosticLevelFatal,
Path: "TestExample :: enum value TestExample_ANOTHER_VALUE_ANOTHER_VALUE",
Message: "failed to normalize enum constant name: enum pattern template validation failed: ambiguous enum value boundary between Screaming and Screaming using \"_\"; configure enum.patterns or enum.values",
},
},
},
{
name: "ambiguous enum values",
in: []types.ConstantDecl{
testConstantDecl("TestExample", "TestExample_TestValue", "test_value"),
testConstantDecl("TestExample", "TestExample_TEST_VALUE", "test_value"),
},
ambiguous: map[string][]string{
"TEST_VALUE": {"TestExample_TEST_VALUE", "TestExample_TestValue"},
},
},
{
name: "ambiguous auto type name and value",
in: []types.ConstantDecl{
testConstantDecl("Test", "Test_TestValue", "test_value"),
},
out: []string{
"TEST_VALUE",
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
input := make(map[string]types.ConstantDecl, len(tc.in))
for _, decl := range tc.in {
input[decl.Name] = decl
}
out, ambiguous, diags := constantsByNormalizedName(input, tc.pattern, nil, "")
if len(tc.out) > 0 {
assert.ElementsMatch(t, tc.out, slices.Collect(maps.Keys(out)))
} else {
assert.Empty(t, out)
}
if len(tc.ambiguous) > 0 {
assert.Equal(t, tc.ambiguous, ambiguous)
} else {
assert.Empty(t, ambiguous)
}
if len(tc.diags) > 0 {
assert.ElementsMatch(t, tc.diags, diags)
} else {
assert.Empty(t, diags)
}
})
}
}
func Test_casingScreamingSnake(t *testing.T) {
tt := map[string]string{
"example_value": "EXAMPLE_VALUE",
"ExampleValue": "EXAMPLE_VALUE",
"single": "SINGLE",
"multi-word-value": "MULTI_WORD_VALUE",
"mp3-player": "MP3_PLAYER",
}
for input, expected := range tt {
t.Run(input, func(t *testing.T) {
assert.Equal(t, expected, casingScreamingSnake(input))
})
}
}