/
setters_test.go
124 lines (95 loc) · 2.77 KB
/
setters_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package jsonstruct_test
import (
"encoding/json"
"time"
"github.com/myshkin5/jsonstruct"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Setters", func() {
var (
values jsonstruct.JSONStruct
)
Describe("SetString()", func() {
It("overrides existing values", func() {
err := json.Unmarshal([]byte(`{
"this": "that",
"parent": {
"child": "value"
}
}`), &values)
Expect(err).NotTo(HaveOccurred())
Expect(values.SetString(".this", "something else")).To(Succeed())
value, ok := values.String(".this")
Expect(ok).To(BeTrue())
Expect(value).To(Equal("something else"))
Expect(values.SetString(".parent.child", "new value")).To(Succeed())
value, ok = values.String(".parent.child")
Expect(ok).To(BeTrue())
Expect(value).To(Equal("new value"))
})
It("can set a string to empty string", func() {
values = jsonstruct.New()
Expect(values.SetString(".value", "something")).To(Succeed())
Expect(values.SetString(".value", "")).To(Succeed())
value, ok := values.String(".value")
Expect(ok).To(BeTrue())
Expect(value).To(Equal(""))
Expect(values.SetString(".another", "")).To(Succeed())
another, ok := values.String(".another")
Expect(ok).To(BeTrue())
Expect(another).To(Equal(""))
})
It("can set values multiple levels deep", func() {
values = jsonstruct.New()
Expect(values.SetString(".one.two.three", "hi")).To(Succeed())
value, ok := values.String(".one.two.three")
Expect(ok).To(BeTrue())
Expect(value).To(Equal("hi"))
})
})
Describe("SetInt()", func() {
It("overrides existing values", func() {
err := json.Unmarshal([]byte(`{
"this": "that",
"parent": {
"child": 98765
}
}`), &values)
Expect(err).NotTo(HaveOccurred())
Expect(values.SetInt(".this", 1000000)).To(Succeed())
value, ok := values.Int(".this")
Expect(ok).To(BeTrue())
Expect(value).To(Equal(1000000))
Expect(values.SetInt(".parent.child", 12345)).To(Succeed())
value, ok = values.Int(".parent.child")
Expect(ok).To(BeTrue())
Expect(value).To(Equal(12345))
})
})
Describe("SetDuration()", func() {
It("sets a duration value as a string", func() {
values = jsonstruct.New()
Expect(values.SetDuration(".duration-path", 32*time.Second)).To(Succeed())
Expect(values["duration-path"]).To(Equal("32s"))
})
})
Describe("SetList()", func() {
It("sets a list", func() {
values = jsonstruct.New()
sub := jsonstruct.New()
sub.SetString(".x", "y")
list := []interface{}{1, "u", sub}
Expect(values.SetList(".doc.list", list)).To(Succeed())
data, err := json.Marshal(values)
Expect(err).NotTo(HaveOccurred())
Expect(data).To(MatchJSON(`{
"doc": {
"list": [
1, "u", { "x": "y" }
]
}
}`))
})
})
})