Skip to content

Commit 4061a5d

Browse files
authored
feat: allow disabling of iota (#273)
1 parent f2d3ebc commit 4061a5d

File tree

13 files changed

+2016
-45
lines changed

13 files changed

+2016
-45
lines changed

example/buggy.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build example
2+
// +build example
3+
4+
//go:generate ../bin/go-enum -b example --no-iota
5+
package example
6+
7+
// ENUM(
8+
// A = 0
9+
// B = 2
10+
// C = 1
11+
// )
12+
type Buggy uint

example/buggy_enum.go

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/buggy_test.go

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
//go:build example
2+
// +build example
3+
4+
package example
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestBuggyConstants(t *testing.T) {
14+
// Test that constants have correct values
15+
assert.Equal(t, Buggy(0), BuggyA, "BuggyA should have value 0")
16+
assert.Equal(t, Buggy(2), BuggyB, "BuggyB should have value 2")
17+
assert.Equal(t, Buggy(1), BuggyC, "BuggyC should have value 1")
18+
}
19+
20+
func TestBuggyString(t *testing.T) {
21+
tests := []struct {
22+
name string
23+
value Buggy
24+
expected string
25+
}{
26+
{
27+
name: "BuggyA",
28+
value: BuggyA,
29+
expected: "A",
30+
},
31+
{
32+
name: "BuggyB",
33+
value: BuggyB,
34+
expected: "B",
35+
},
36+
{
37+
name: "BuggyC",
38+
value: BuggyC,
39+
expected: "C",
40+
},
41+
{
42+
name: "Invalid value",
43+
value: Buggy(99),
44+
expected: "Buggy(99)",
45+
},
46+
{
47+
name: "Invalid value 3",
48+
value: Buggy(3),
49+
expected: "Buggy(3)",
50+
},
51+
}
52+
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
assert.Equal(t, tt.expected, tt.value.String())
56+
})
57+
}
58+
}
59+
60+
func TestBuggyIsValid(t *testing.T) {
61+
tests := []struct {
62+
name string
63+
value Buggy
64+
expected bool
65+
}{
66+
{
67+
name: "BuggyA is valid",
68+
value: BuggyA,
69+
expected: true,
70+
},
71+
{
72+
name: "BuggyB is valid",
73+
value: BuggyB,
74+
expected: true,
75+
},
76+
{
77+
name: "BuggyC is valid",
78+
value: BuggyC,
79+
expected: true,
80+
},
81+
{
82+
name: "Invalid value 99",
83+
value: Buggy(99),
84+
expected: false,
85+
},
86+
{
87+
name: "Invalid value 3",
88+
value: Buggy(3),
89+
expected: false,
90+
},
91+
{
92+
name: "Invalid value 3",
93+
value: Buggy(3),
94+
expected: false,
95+
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
assert.Equal(t, tt.expected, tt.value.IsValid())
101+
})
102+
}
103+
}
104+
105+
func TestParseBuggy(t *testing.T) {
106+
tests := []struct {
107+
name string
108+
input string
109+
expected Buggy
110+
errorExpected bool
111+
}{
112+
{
113+
name: "Parse A",
114+
input: "A",
115+
expected: BuggyA,
116+
errorExpected: false,
117+
},
118+
{
119+
name: "Parse B",
120+
input: "B",
121+
expected: BuggyB,
122+
errorExpected: false,
123+
},
124+
{
125+
name: "Parse C",
126+
input: "C",
127+
expected: BuggyC,
128+
errorExpected: false,
129+
},
130+
{
131+
name: "Parse invalid string",
132+
input: "D",
133+
expected: Buggy(0),
134+
errorExpected: true,
135+
},
136+
{
137+
name: "Parse empty string",
138+
input: "",
139+
expected: Buggy(0),
140+
errorExpected: true,
141+
},
142+
{
143+
name: "Parse lowercase a",
144+
input: "a",
145+
expected: Buggy(0),
146+
errorExpected: true,
147+
},
148+
{
149+
name: "Parse numeric string",
150+
input: "1",
151+
expected: Buggy(0),
152+
errorExpected: true,
153+
},
154+
}
155+
156+
for _, tt := range tests {
157+
t.Run(tt.name, func(t *testing.T) {
158+
result, err := ParseBuggy(tt.input)
159+
160+
if tt.errorExpected {
161+
require.Error(t, err, "Expected error for input: %s", tt.input)
162+
assert.Contains(t, err.Error(), "not a valid Buggy", "Error should contain 'not a valid Buggy'")
163+
assert.Equal(t, tt.expected, result, "Result should be zero value on error")
164+
} else {
165+
require.NoError(t, err, "Unexpected error for input: %s", tt.input)
166+
assert.Equal(t, tt.expected, result, "Parsed value should match expected")
167+
}
168+
})
169+
}
170+
}
171+
172+
func TestBuggyErrorType(t *testing.T) {
173+
// Test that ErrInvalidBuggy is properly defined
174+
assert.NotNil(t, ErrInvalidBuggy)
175+
assert.Equal(t, "not a valid Buggy", ErrInvalidBuggy.Error())
176+
177+
// Test that ParseBuggy returns an error that wraps ErrInvalidBuggy
178+
_, err := ParseBuggy("invalid")
179+
require.Error(t, err)
180+
assert.ErrorIs(t, err, ErrInvalidBuggy)
181+
}
182+
183+
func TestBuggyEdgeCases(t *testing.T) {
184+
t.Run("Zero value behavior", func(t *testing.T) {
185+
var b Buggy
186+
// Zero value should be 0, which is BuggyA
187+
assert.Equal(t, Buggy(0), b)
188+
assert.Equal(t, BuggyA, b)
189+
assert.True(t, b.IsValid())
190+
assert.Equal(t, "A", b.String())
191+
})
192+
193+
t.Run("Non-sequential values", func(t *testing.T) {
194+
// Test that the enum handles non-sequential values correctly
195+
// A=0, B=2, C=1 - so value 1 should map to C, not B
196+
assert.Equal(t, "C", Buggy(1).String())
197+
assert.Equal(t, "B", Buggy(2).String())
198+
assert.True(t, Buggy(1).IsValid())
199+
assert.True(t, Buggy(2).IsValid())
200+
})
201+
}
202+
203+
func BenchmarkBuggyString(b *testing.B) {
204+
values := []Buggy{BuggyA, BuggyB, BuggyC, Buggy(99)}
205+
206+
b.ResetTimer()
207+
for i := 0; i < b.N; i++ {
208+
for _, v := range values {
209+
_ = v.String()
210+
}
211+
}
212+
}
213+
214+
func BenchmarkParseBuggy(b *testing.B) {
215+
inputs := []string{"A", "B", "C", "invalid"}
216+
217+
b.ResetTimer()
218+
for i := 0; i < b.N; i++ {
219+
for _, input := range inputs {
220+
_, _ = ParseBuggy(input)
221+
}
222+
}
223+
}
224+
225+
func BenchmarkBuggyIsValid(b *testing.B) {
226+
values := []Buggy{BuggyA, BuggyB, BuggyC, Buggy(99)}
227+
228+
b.ResetTimer()
229+
for i := 0; i < b.N; i++ {
230+
for _, v := range values {
231+
_ = v.IsValid()
232+
}
233+
}
234+
}

0 commit comments

Comments
 (0)