-
Notifications
You must be signed in to change notification settings - Fork 1
/
enums_test.go
107 lines (83 loc) · 2.58 KB
/
enums_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
package yarql
import (
"testing"
a "github.com/mjarkk/yarql/assert"
)
func TestRegisterEnum(t *testing.T) {
type TestEnumString string
res, err := registerEnumCheck(map[string]TestEnumString{
"A": "B",
})
a.NoError(t, err)
a.NotNil(t, res)
type TestEnumUint uint
res, err = registerEnumCheck(map[string]TestEnumUint{
"A": 1,
})
a.NoError(t, err)
a.NotNil(t, res)
type TestEnumInt int
res, err = registerEnumCheck(map[string]TestEnumInt{
"A": 1,
})
a.NoError(t, err)
a.NotNil(t, res)
}
func TestEmptyEnumShouldNotBeRegistered(t *testing.T) {
type TestEnum string
res, err := registerEnumCheck(map[string]TestEnum{})
a.NoError(t, err)
a.Nil(t, res)
}
func TestRegisterEnumFails(t *testing.T) {
type TestEnum string
_, err := registerEnumCheck(0)
a.Error(t, err, "Cannot generate an enum of non map types")
_, err = registerEnumCheck(nil)
a.Error(t, err, "Cannot generate an enum of non map types 2")
_, err = registerEnumCheck(map[int]TestEnum{1: "a"})
a.Error(t, err, "Enum must have a string key type")
_, err = registerEnumCheck(map[string]struct{}{"a": {}})
a.Error(t, err, "Enum value cannot be complex")
_, err = registerEnumCheck(map[string]string{"foo": "bar"})
a.Error(t, err, "Enum value must be a custom type")
_, err = registerEnumCheck(map[string]TestEnum{"": ""})
a.Error(t, err, "Enum keys cannot be empty")
// Maybe fix this??
// _, err = registerEnumCheck(map[string]TestEnum{
// "Foo": "Baz",
// "Bar": "Baz",
// })
// Error(t, err, "Enum cannot have duplicated values")
_, err = registerEnumCheck(map[string]TestEnum{"1": ""})
a.Error(t, err, "Enum cannot have an invalid graphql name, where first letter is number")
_, err = registerEnumCheck(map[string]TestEnum{"_": ""})
a.Error(t, err, "Enum cannot have an invalid graphql name, where first letter is underscore")
_, err = registerEnumCheck(map[string]TestEnum{"A!!!!": ""})
a.Error(t, err, "Enum cannot have an invalid graphql name, where remainder of name is invalid")
}
type TestEnum2 uint8
const (
TestEnum2Foo TestEnum2 = iota
TestEnum2Bar
TestEnum2Baz
)
type TestEnumFunctionInput struct{}
func (TestEnumFunctionInput) ResolveBar(args struct{ E TestEnum2 }) TestEnum2 {
return args.E
}
func TestEnum(t *testing.T) {
s := NewSchema()
added, err := s.RegisterEnum(map[string]TestEnum2{
"FOO": TestEnum2Foo,
"BAR": TestEnum2Bar,
"BAZ": TestEnum2Baz,
})
a.True(t, added)
a.NoError(t, err)
res, errs := bytecodeParse(t, s, `{bar(e: BAZ)}`, TestEnumFunctionInput{}, M{}, ResolveOptions{NoMeta: true})
for _, err := range errs {
panic(err)
}
a.Equal(t, `{"bar":"BAZ"}`, res)
}