-
Notifications
You must be signed in to change notification settings - Fork 19
/
enum_test.go
163 lines (142 loc) · 3.57 KB
/
enum_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
package enum_test
import (
"testing"
"github.com/matryer/is"
"github.com/orsinium-labs/enum"
)
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
func TestMember_Value(t *testing.T) {
is := is.New(t)
is.Equal(Red.Value, "red")
is.Equal(Green.Value, "green")
is.Equal(Blue.Value, "blue")
is.Equal(enum.Member[string]{"blue"}.Value, "blue")
is.Equal(enum.Member[int]{14}.Value, 14)
}
func TestEnum_Parse(t *testing.T) {
is := is.New(t)
var parsed *Color
parsed = Colors.Parse("red")
is.Equal(parsed, &Red)
parsed = Colors.Parse("purple")
is.Equal(parsed, nil)
}
func TestEnum_Empty(t *testing.T) {
is := is.New(t)
is.True(!Colors.Empty())
is.True(enum.New[int, enum.Member[int]]().Empty())
}
func TestEnum_Len(t *testing.T) {
is := is.New(t)
is.Equal(Colors.Len(), 3)
is.Equal(enum.New[int, enum.Member[int]]().Len(), 0)
}
func TestEnum_Contains(t *testing.T) {
is := is.New(t)
is.True(Colors.Contains(Red))
is.True(Colors.Contains(Green))
is.True(Colors.Contains(Blue))
blue := Color{"blue"}
is.True(Colors.Contains(blue))
purple := Color{"purple"}
is.True(!Colors.Contains(purple))
}
func TestEnum_Members(t *testing.T) {
is := is.New(t)
exp := []Color{Red, Green, Blue}
is.Equal(Colors.Members(), exp)
}
func TestEnum_Choice(t *testing.T) {
is := is.New(t)
// Select a random color
m := Colors.Choice(0)
is.True(m != nil)
is.True(Colors.Contains(*m))
// Select a specific color using a specific random seed
m = Colors.Choice(254)
is.True(m != nil)
is.Equal(*m, Red)
// Select a specific color using a specific random seed
m = Colors.Choice(1337)
is.True(m != nil)
is.Equal(*m, Green)
// Select a specific color using a specific random seed
m = Colors.Choice(42)
is.True(m != nil)
is.Equal(*m, Blue)
// Selecting a random member from an empty Enum returns nil
emptyEnums := enum.New[string, Color]()
is.True(emptyEnums.Choice(0) == nil)
}
func TestEnum_Values(t *testing.T) {
is := is.New(t)
exp := []string{"red", "green", "blue"}
is.Equal(Colors.Values(), exp)
}
func TestEnum_Value(t *testing.T) {
is := is.New(t)
is.Equal(Colors.Value(Red), "red")
}
func TestEnum_Index(t *testing.T) {
is := is.New(t)
is.Equal(Colors.Index(Red), 0)
is.Equal(Colors.Index(Green), 1)
is.Equal(Colors.Index(Blue), 2)
}
func TestEnum_Index_Panic(t *testing.T) {
is := is.New(t)
defer func() {
r := recover()
is.Equal(r, "the given Member does not belong to this Enum")
}()
Colors.Index(Color{"purple"})
}
func TestBuilder(t *testing.T) {
is := is.New(t)
type Country enum.Member[string]
var (
b = enum.NewBuilder[string, Country]()
NL = b.Add(Country{"Netherlands"})
FR = b.Add(Country{"France"})
BE = b.Add(Country{"Belgium"})
Countries = b.Enum()
)
is.Equal(Countries.Members(), []Country{NL, FR, BE})
}
type BookValue struct {
Title string
ISBN string
}
type Book enum.Member[BookValue]
var (
EfficientGo = Book{BookValue{"Efficient Go", "978-1098105716"}}
ConcurrencyInGo = Book{BookValue{"Concurrency in Go", "978-1491941195"}}
Books = enum.New(EfficientGo, ConcurrencyInGo)
)
func (b BookValue) Equal(v BookValue) bool {
return b.ISBN == v.ISBN
}
func TestParse(t *testing.T) {
is := is.New(t)
tests := []struct {
isbn string
want *Book
}{
{"978-1098105716", &EfficientGo},
{"978-1491941195", &ConcurrencyInGo},
{"invalid-isbn", nil},
}
for _, tt := range tests {
t.Run(tt.isbn, func(t *testing.T) {
v := BookValue{ISBN: tt.isbn}
got := enum.Parse(Books, v)
is.Equal(got, tt.want)
})
}
}