-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_test.go
90 lines (69 loc) · 2.38 KB
/
bool_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
package gsv_test
import (
"github.com/agent-api/gsv"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("BoolSchema", func() {
It("implements the Schema interface", func() {
schema := gsv.Bool()
var _ gsv.Schema = schema
})
Describe("BoolSchema())", func() {
It("creates a new boolSchema", func() {
v := gsv.Int()
Expect(v).ToNot(BeNil())
})
})
XDescribe("JSON Unmarshaling", func() {
type TestNestedStringSchema struct {
NestedTest *gsv.StringSchema `json:"nested_test"`
}
type TestStringSchema struct {
Test *gsv.StringSchema `json:"test"`
NestedSchema *TestNestedStringSchema `json:"nested_schema"`
}
It("correctly unmarshals nested string schemas", func() {
jsonData := `{"test": "testing json data", "nested_schema": {"nested_test": "nested test"}}`
var schema TestStringSchema
schema.NestedSchema = &TestNestedStringSchema{}
_, err := gsv.Parse([]byte(jsonData), &schema)
Expect(err).NotTo(HaveOccurred())
val, ok := schema.Test.Value()
Expect(ok).To(BeTrue())
Expect(val).To(Equal("testing json data"))
val, ok = schema.NestedSchema.NestedTest.Value()
Expect(ok).To(BeTrue())
Expect(val).To(Equal("nested test"))
})
It("handles optional fields correctly", func() {
jsonData := `{"nested_schema": {"nested_test": "nested test"}}`
var schema TestStringSchema
schema.Test = gsv.String().Optional()
schema.NestedSchema = &TestNestedStringSchema{}
_, err := gsv.Parse([]byte(jsonData), &schema)
Expect(err).NotTo(HaveOccurred())
val, ok := schema.Test.Value()
Expect(ok).To(BeFalse())
Expect(val).To(Equal(""))
val, ok = schema.NestedSchema.NestedTest.Value()
Expect(ok).To(BeTrue())
Expect(val).To(Equal("nested test"))
})
It("enforces required fields", func() {
jsonData := `{"nested_schema": {"nested_test": "nested test"}}`
var schema TestStringSchema
schema.Test = gsv.String()
schema.NestedSchema = &TestNestedStringSchema{}
_, err := gsv.Parse([]byte(jsonData), &schema)
Expect(err).To(HaveOccurred())
})
XIt("handles null schema pointers when they're not instantiated", func() {
jsonData := `{"nested_schema": {"nested_test": "nested test"}}`
var schema TestStringSchema
schema.NestedSchema = &TestNestedStringSchema{}
_, err := gsv.Parse([]byte(jsonData), &schema)
Expect(err).To(HaveOccurred())
})
})
})