-
Notifications
You must be signed in to change notification settings - Fork 1
/
value_panics.go
74 lines (64 loc) · 1.52 KB
/
value_panics.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
package j
import (
"testing"
)
// checkPanics makes sure the Value actually
// does panics when incorrect methods are called on.
func checkPanics(t *testing.T, v Value) {
typ := v.Type()
checkMethod := func(mustPanic bool, method func(Value), name string) {
defer func() {
r := recover()
if mustPanic && r == nil {
t.Errorf("Expected panic on calling %s on %v type", name, typ)
} else if !mustPanic && r != nil {
t.Errorf("Unexpected panic on calling %s on %v", name, typ)
}
}()
method(v)
}
type methodCheck struct {
mustPanic bool
method func(Value)
name string
}
for _, mc := range []methodCheck{
methodCheck{
mustPanic: typ != ObjectType,
method: func(v Value) { v.Members() },
name: "Members",
},
methodCheck{
mustPanic: typ != ArrayType,
method: func(v Value) { v.Values() },
name: "Values",
},
methodCheck{
mustPanic: typ != BoolType,
method: func(v Value) { v.Bool() },
name: "Bool",
},
methodCheck{
mustPanic: typ != NumberType,
method: func(v Value) { v.Int64() },
name: "Int64",
},
methodCheck{
mustPanic: typ != NumberType,
method: func(v Value) { v.Float64() },
name: "Float64",
},
methodCheck{
mustPanic: typ != NumberType,
method: func(v Value) { v.Raw() },
name: "Raw",
},
methodCheck{
mustPanic: typ != StringType,
method: func(v Value) { _ = v.String() },
name: "String",
},
} {
checkMethod(mc.mustPanic, mc.method, mc.name)
}
}