Skip to content

Commit 768bf63

Browse files
committed
Add tests
1 parent 049aa50 commit 768bf63

File tree

3 files changed

+189
-16
lines changed

3 files changed

+189
-16
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ States.Aliases() // [COMMITTED IN_PROGRESS DONE]
6262
**`Check if Value is in Enum`**
6363

6464
```go
65-
States.Is("COMMITTED") // true
66-
States.Is("BLOCKED") // true
67-
States.Is("IN_PROGRESS") // false
68-
States.Is("COMPLETED") // false
65+
States.In(State("COMMITTED")) // true
66+
States.In(State("BLOCKED")) // true
67+
States.In(State("IN_PROGRESS")) // false
68+
States.In(State("COMPLETED")) // false
6969
```
7070

7171
**`Get Enum Values`**

enum_test.go

+180-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,198 @@
11
package enum_test
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/neoxelox/enum"
78
)
89

9-
type State string
10+
type LiteState string
1011

11-
type enumStates = struct {
12-
COMMITTED State
13-
IN_PROGRESS State
14-
DONE State
12+
type enumLiteStates = struct {
13+
COMMITTED LiteState
14+
IN_PROGRESS LiteState
15+
DONE LiteState
1516
enum.Enum
1617
}
1718

18-
func TestNew(t *testing.T) {
19-
States := enum.New(&enumStates{
19+
func TestNewLite(t *testing.T) {
20+
liteStates := enum.New(&enumLiteStates{
2021
COMMITTED: "COMMITTED",
2122
IN_PROGRESS: "BLOCKED",
2223
DONE: "DONE",
23-
}).(*enumStates)
24+
}).(*enumLiteStates)
2425

25-
if States.COMMITTED != "COMMITTED" {
26-
t.Fatalf("Failed committed test")
26+
if reflect.TypeOf(liteStates.COMMITTED).Kind() != reflect.String {
27+
t.Fatalf("Type of Lite enum is not a primitive string")
28+
}
29+
30+
if liteStates.COMMITTED != "COMMITTED" {
31+
t.Fatalf("Lite enum is not a comparable with an string literal")
32+
}
33+
}
34+
35+
func TestIsLite(t *testing.T) {
36+
liteStates := enum.New(&enumLiteStates{
37+
COMMITTED: "COMMITTED",
38+
IN_PROGRESS: "BLOCKED",
39+
DONE: "DONE",
40+
}).(*enumLiteStates)
41+
42+
if !liteStates.Is("IN_PROGRESS") {
43+
t.Fatalf("IN_PROGRESS should be an alias of Lite enum")
44+
}
45+
46+
if liteStates.Is("BLOCKED") {
47+
t.Fatalf("BLOCKED should not be an alias of Lite enum")
48+
}
49+
}
50+
51+
func TestAliasesLite(t *testing.T) {
52+
liteStates := enum.New(&enumLiteStates{
53+
COMMITTED: "COMMITTED",
54+
IN_PROGRESS: "BLOCKED",
55+
DONE: "DONE",
56+
}).(*enumLiteStates)
57+
58+
aliases := liteStates.Aliases()
59+
if len(aliases) != 3 {
60+
t.Fatalf("Aliases of Lite enum should be three")
61+
}
62+
63+
expectedAliases := []string{"COMMITTED", "IN_PROGRESS", "DONE"}
64+
for i := range aliases {
65+
if aliases[i] != expectedAliases[i] {
66+
t.Fatalf("Expected alias: %v, instead got: %v, in Lite enum", expectedAliases[i], aliases[i])
67+
}
68+
}
69+
}
70+
71+
func TestInLite(t *testing.T) {
72+
liteStates := enum.New(&enumLiteStates{
73+
COMMITTED: "COMMITTED",
74+
IN_PROGRESS: "BLOCKED",
75+
DONE: "DONE",
76+
}).(*enumLiteStates)
77+
78+
if !liteStates.In(LiteState("BLOCKED")) {
79+
t.Fatalf("BLOCKED should be a value of Lite enum")
80+
}
81+
82+
if liteStates.In(LiteState("IN_PROGRESS")) {
83+
t.Fatalf("IN_PROGRESS should not be a value of Lite enum")
84+
}
85+
}
86+
87+
func TestValuesLite(t *testing.T) {
88+
liteStates := enum.New(&enumLiteStates{
89+
COMMITTED: "COMMITTED",
90+
IN_PROGRESS: "BLOCKED",
91+
DONE: "DONE",
92+
}).(*enumLiteStates)
93+
94+
values := liteStates.Values()
95+
if len(values) != 3 {
96+
t.Fatalf("Values of Lite enum should be three")
97+
}
98+
99+
expectedValues := []LiteState{LiteState("COMMITTED"), LiteState("BLOCKED"), LiteState("DONE")}
100+
for i := range values {
101+
if alias := (values[i]).(LiteState); alias != expectedValues[i] {
102+
t.Fatalf("Expected alias: %v, instead got: %v, in Lite enum", expectedValues[i], alias)
103+
}
104+
}
105+
}
106+
107+
type StrongState struct{ string }
108+
109+
type enumStrongStates = struct {
110+
COMMITTED StrongState
111+
IN_PROGRESS StrongState
112+
DONE StrongState
113+
enum.Enum
114+
}
115+
116+
func TestNewStrong(t *testing.T) {
117+
strongStates := enum.New(&enumStrongStates{
118+
COMMITTED: StrongState{"COMMITTED"},
119+
IN_PROGRESS: StrongState{"BLOCKED"},
120+
DONE: StrongState{"DONE"},
121+
}).(*enumStrongStates)
122+
123+
if reflect.TypeOf(strongStates.COMMITTED).Kind() != reflect.Struct {
124+
t.Fatalf("Type of Strong enum is not a primitive struct")
125+
}
126+
}
127+
128+
func TestIsStrong(t *testing.T) {
129+
strongStates := enum.New(&enumStrongStates{
130+
COMMITTED: StrongState{"COMMITTED"},
131+
IN_PROGRESS: StrongState{"BLOCKED"},
132+
DONE: StrongState{"DONE"},
133+
}).(*enumStrongStates)
134+
135+
if !strongStates.Is("IN_PROGRESS") {
136+
t.Fatalf("IN_PROGRESS should be an alias of Strong enum")
137+
}
138+
139+
if strongStates.Is("BLOCKED") {
140+
t.Fatalf("BLOCKED should not be an alias of Strong enum")
141+
}
142+
}
143+
144+
func TestAliasesStrong(t *testing.T) {
145+
strongStates := enum.New(&enumStrongStates{
146+
COMMITTED: StrongState{"COMMITTED"},
147+
IN_PROGRESS: StrongState{"BLOCKED"},
148+
DONE: StrongState{"DONE"},
149+
}).(*enumStrongStates)
150+
151+
aliases := strongStates.Aliases()
152+
if len(aliases) != 3 {
153+
t.Fatalf("Aliases of Strong enum should be three")
154+
}
155+
156+
expectedAliases := []string{"COMMITTED", "IN_PROGRESS", "DONE"}
157+
for i := range aliases {
158+
if aliases[i] != expectedAliases[i] {
159+
t.Fatalf("Expected alias: %v, instead got: %v, in Strong enum", expectedAliases[i], aliases[i])
160+
}
161+
}
162+
}
163+
164+
func TestInStrong(t *testing.T) {
165+
strongStates := enum.New(&enumStrongStates{
166+
COMMITTED: StrongState{"COMMITTED"},
167+
IN_PROGRESS: StrongState{"BLOCKED"},
168+
DONE: StrongState{"DONE"},
169+
}).(*enumStrongStates)
170+
171+
if !strongStates.In(StrongState{"BLOCKED"}) {
172+
t.Fatalf("BLOCKED should be a value of Strong enum")
173+
}
174+
175+
if strongStates.In(StrongState{"IN_PROGRESS"}) {
176+
t.Fatalf("IN_PROGRESS should not be a value of Strong enum")
177+
}
178+
}
179+
180+
func TestValuesStrong(t *testing.T) {
181+
strongStates := enum.New(&enumStrongStates{
182+
COMMITTED: StrongState{"COMMITTED"},
183+
IN_PROGRESS: StrongState{"BLOCKED"},
184+
DONE: StrongState{"DONE"},
185+
}).(*enumStrongStates)
186+
187+
values := strongStates.Values()
188+
if len(values) != 3 {
189+
t.Fatalf("Values of Strong enum should be three")
190+
}
191+
192+
expectedValues := []StrongState{StrongState{"COMMITTED"}, StrongState{"BLOCKED"}, StrongState{"DONE"}}
193+
for i := range values {
194+
if alias := (values[i]).(StrongState); alias != expectedValues[i] {
195+
t.Fatalf("Expected alias: %v, instead got: %v, in Strong enum", expectedValues[i], alias)
196+
}
27197
}
28198
}

tasks.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ def fail(message):
1515
@task(
1616
help={
1717
"test": "<PACKAGE_PATH>::<TEST_NAME>. If empty, it will run all tests.",
18+
"verbose": "Show stdout of tests.",
1819
"show": "Show coverprofile page.",
1920
}
2021
)
21-
def test(c, test="", show=False):
22+
def test(c, test="", verbose=False, show=False):
2223
"""Run tests."""
2324
test_regex = "./..."
2425

2526
test = test.split("::")
2627
if len(test) == 2:
2728
test_regex = f"-run {test[1]} {test[0]}"
2829

29-
r = c.run(f"go test -race -count=1 -cover {'-coverprofile=coverage.out' if show else ''} {test_regex}")
30+
r = c.run(
31+
f"go test {'-v' if verbose else ''} -race -count=1 -cover {'-coverprofile=coverage.out' if show else ''} {test_regex}"
32+
)
3033

3134
packages = 0
3235
coverage = 0.0

0 commit comments

Comments
 (0)