-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterfaces_test.go
80 lines (61 loc) · 2.03 KB
/
interfaces_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
package yarql
import (
"reflect"
"testing"
a "github.com/mjarkk/yarql/assert"
)
type InterfaceSchema struct {
Bar BarWImpl
Baz BazWImpl
Generic InterfaceType
}
type InterfaceType interface {
ResolveFoo() string
ResolveBar() string
}
type BarWImpl struct {
ExtraBarField string
}
func (BarWImpl) ResolveFoo() string { return "this is bar" }
func (BarWImpl) ResolveBar() string { return "This is bar" }
type BazWImpl struct {
ExtraBazField string
}
func (BazWImpl) ResolveFoo() string { return "this is baz" }
func (BazWImpl) ResolveBar() string { return "This is baz" }
func TestInterfaceType(t *testing.T) {
implementationMapLen := len(implementationMap)
structImplementsMapLen := len(structImplementsMap)
Implements((*InterfaceType)(nil), BarWImpl{})
a.Equal(t, implementationMapLen+1, len(implementationMap))
a.Equal(t, structImplementsMapLen+1, len(structImplementsMap))
Implements((*InterfaceType)(nil), BazWImpl{})
a.Equal(t, implementationMapLen+1, len(implementationMap))
a.Equal(t, structImplementsMapLen+2, len(structImplementsMap))
_, err := newParseCtx().check(reflect.TypeOf(InterfaceSchema{}), false)
a.Nil(t, err)
}
func TestInterfaceInvalidInput(t *testing.T) {
a.Panics(t, func() {
Implements(nil, BarWImpl{})
}, "cannot use nil as interface value")
a.Panics(t, func() {
Implements((*InterfaceType)(nil), nil)
}, "cannot use nil as type value")
a.Panics(t, func() {
Implements(struct{}{}, BarWImpl{})
}, "cannot use non interface type as interface value")
a.Panics(t, func() {
Implements((*InterfaceType)(nil), "this is not a valid type")
}, "cannot use non struct type as type value")
a.Panics(t, func() {
Implements((*interface{})(nil), BarWImpl{})
}, "cannot use inline interface type as interface value")
a.Panics(t, func() {
Implements((*InterfaceType)(nil), struct{}{})
}, "cannot use inline struct type as type value")
type InvalidStruct struct{}
a.Panics(t, func() {
Implements((*InterfaceType)(nil), InvalidStruct{})
}, "cannot use struct that doesn't implement the interface")
}