forked from orsinium-labs/enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum_test.go
127 lines (111 loc) · 2.84 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
package enum_test
import (
"testing"
"github.com/matryer/is"
"github.com/vamshiaruru32/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.Val, "red")
is.Equal(Green.Val, "green")
is.Equal(Blue.Val, "blue")
is.Equal(enum.Member[string]{"blue"}.Val, "blue")
is.Equal(enum.Member[int]{14}.Val, 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})
}