-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
235 lines (215 loc) · 8.18 KB
/
parser_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package gosoon_test
import (
. "gosoon"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Json", func() {
var (
err error
)
Context("Given brutally invalid JSON", func() {
It("Should error out", func() {
_, err = Json(`F0oTo^Bar}{kkKkK`)
Expect(err).To(HaveOccurred())
})
})
Context("Given an empty string", func() {
It("Should error out", func() {
_, err = Json("")
Expect(err).To(HaveOccurred())
})
})
Context("Given JSON that beings correctly, but goes o' so badly", func() {
It("Should error out for '{'", func() {
_, err = Json("{")
Expect(err).To(HaveOccurred())
})
It("Should error out for '{<ws>'", func() {
_, err = Json("{\n")
Expect(err).To(HaveOccurred())
})
It("Should error out for '{a'", func() {
_, err = Json("{a")
Expect(err).To(HaveOccurred())
})
It("Should error out for '{}a'", func() {
_, err = Json("{}a")
Expect(err).To(HaveOccurred())
})
It("Should error out for '{}<ws>a'", func() {
_, err = Json("{} a")
Expect(err).To(HaveOccurred())
})
})
Context("Given JSON with an invalid key", func() {
It(`Should error out for '{a:""}'`, func() {
_, err = Json(`{a:""}`)
Expect(err).To(HaveOccurred())
})
})
Context("Given an empty JSON object", func() {
It("Should not error out for '{}'", func() {
_, err = Json("{}")
Expect(err).NotTo(HaveOccurred())
})
It("Should not error out for ' {}'", func() {
_, err = Json(" {}")
Expect(err).NotTo(HaveOccurred())
})
It("Should not error out for '<tab>{}'", func() {
_, err = Json("\t{}")
Expect(err).NotTo(HaveOccurred())
})
It("Should not error out for '<newline>{}'", func() {
_, err = Json("\n{}")
Expect(err).NotTo(HaveOccurred())
})
It("Should not error out for '<ws><ws>{}'", func() {
_, err = Json("\n {}")
Expect(err).NotTo(HaveOccurred())
})
It("Should not error out for '{<ws>}'", func() {
_, err = Json("{\t}")
Expect(err).NotTo(HaveOccurred())
})
It("Should not error out for '{}<ws>'", func() {
_, err = Json("{}\n")
Expect(err).NotTo(HaveOccurred())
})
It("Should not error out for '{}<ws><ws>'", func() {
_, err = Json("{}\n\t")
Expect(err).NotTo(HaveOccurred())
})
})
Context("Given a JSON object with at string key and a string field and some whitespace between tokens", func() {
parsedJson, _ := Json("{ \"Golang's\" : \t \"pretty fun\"\n }")
Describe("AttributeValue", func() {
It("Should be that attribute's value", func() {
Expect(parsedJson.AttributeValue("Golang's")).To(Equal("pretty fun"))
})
})
})
Context("Given a JSON object with a string key, and a string field", func() {
var parsedJson ParsedJson
var err error
BeforeEach(func() {
parsedJson, err = Json(`{"Kazark":"The Man"}`)
})
It("Should not error out", func() {
Expect(err).NotTo(HaveOccurred())
})
Describe("AttributeValue", func() {
Context("Given a key other than that of the object", func() {
It("Should return an empty string", func() {
Expect(parsedJson.AttributeValue("")).To(Equal(""))
})
})
Context("Given the key that is in the object", func() {
It("Should return the string's value", func() {
Expect(parsedJson.AttributeValue("Kazark")).To(Equal("The Man"))
})
})
})
})
})
var _ = Describe("JsonString", func() {
Context("Given an empty string with no JSON", func() {
It("Should error out", func() {
_, err := JsonString("")
Expect(err).To(HaveOccurred())
})
})
Context("Given a string with just a double-quote", func() {
It("Should error out", func() {
_, err := JsonString(`"`)
Expect(err).To(HaveOccurred())
})
})
Context("Given a string with just a double-quote that does not have a closing quote", func() {
It("Should error out", func() {
_, err := JsonString(`"no ending double quote`)
Expect(err).To(HaveOccurred())
})
})
Context("Given an empty JSON string", func() {
It("Should not error out", func() {
_, err := JsonString(`""`)
Expect(err).ToNot(HaveOccurred())
})
It("Should return an empty string", func() {
value, _ := JsonString(`""`)
Expect(value).To(Equal(""))
})
})
Context("Given a JSON string with unicode characters in it", func() {
It("Should not error out", func() {
_, err := JsonString(`"omega=Ω"`)
Expect(err).ToNot(HaveOccurred())
})
It(`Should return the correct one character string for "<u-char>"`, func() {
value, _ := JsonString(`"Ω"`)
Expect(value).To(Equal("Ω"))
})
It(`Should return the correct two character string for "<char><u-char>"`, func() {
value, _ := JsonString(`"kΩ"`)
Expect(value).To(Equal("kΩ"))
})
})
Context("Given a JSON string with an escaped character in it", func() {
It("Should not error out", func() {
_, err := JsonString(`" asdg \" asdg a"`)
Expect(err).ToNot(HaveOccurred())
})
It("Should return a string containing the quotation mark", func() {
value, _ := JsonString(`" This is a \" test"`)
Expect(value).To(Equal(` This is a " test`))
})
It("Should return a string containing the newline", func() {
value, _ := JsonString(`" This is a \n test"`)
Expect(value).To(Equal(" This is a \n test"))
})
It("Should return a string containing the tab", func() {
value, _ := JsonString(`" This is a \t test"`)
Expect(value).To(Equal(" This is a \t test"))
})
It("Should return a string containing the return", func() {
value, _ := JsonString(`" This is a \r test"`)
Expect(value).To(Equal(" This is a \r test"))
})
It("Should return a string containing the formfeed", func() {
value, _ := JsonString(`" This is a \f test"`)
Expect(value).To(Equal(" This is a \f test"))
})
It("Should return a string containing the backspace", func() {
value, _ := JsonString(`" This is a \b test"`)
Expect(value).To(Equal(" This is a \b test"))
})
It("Should return a string containing the solidus", func() {
value, _ := JsonString(`" This is a \∕ test"`)
Expect(value).To(Equal(" This is a ∕ test"))
})
It("Should return a string containing the reverse solidus", func() {
value, _ := JsonString(`" This is a \\ test"`)
Expect(value).To(Equal(" This is a \\ test"))
})
})
Context("Given a JSON string with simple characters in it", func() {
It("Should not error out", func() {
_, err := JsonString(`" asdg asdg a"`)
Expect(err).ToNot(HaveOccurred())
})
It(`Should return the correct one character string for "<char>"`, func() {
value, _ := JsonString(`"k"`)
Expect(value).To(Equal("k"))
})
It(`Should return the correct two characters string for "<char><char>"`, func() {
value, _ := JsonString(`"ke"`)
Expect(value).To(Equal("ke"))
})
It(`Should return the correct three characters string for "<char><char><char>"`, func() {
value, _ := JsonString(`"kei"`)
Expect(value).To(Equal("kei"))
})
})
})