-
Notifications
You must be signed in to change notification settings - Fork 4
/
expanded_json_matcher_test.go
162 lines (135 loc) · 6.79 KB
/
expanded_json_matcher_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
package unmarshalledmatchers_test
import (
. "github.com/benjamintf1/unmarshalledmatchers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("MatchUnorderedJSONMatcher", func() {
Context("When passed stringifiables", func() {
It("should succeed if the JSON matches", func() {
Ω("{}").Should(MatchUnorderedJSON("{}"))
Ω(`{"a":1}`).Should(MatchUnorderedJSON(`{"a":1}`))
Ω(`{
"a":1
}`).Should(MatchUnorderedJSON(`{"a":1}`))
Ω(`{"a":1, "b":2}`).Should(MatchUnorderedJSON(`{"b":2, "a":1}`))
Ω(`{"a":1}`).ShouldNot(MatchUnorderedJSON(`{"b":2, "a":1}`))
Ω(`{"a":"a", "b":"b"}`).ShouldNot(MatchUnorderedJSON(`{"a":"a", "b":"b", "c":"c"}`))
Ω(`{"a":"a", "b":"b", "c":"c"}`).ShouldNot(MatchUnorderedJSON(`{"a":"a", "b":"b"}`))
Ω(`{"a":null, "b":null}`).ShouldNot(MatchUnorderedJSON(`{"c":"c", "d":"d"}`))
Ω(`{"a":null, "b":null, "c":null}`).ShouldNot(MatchUnorderedJSON(`{"a":null, "b":null, "d":null}`))
})
It("should work with byte arrays", func() {
Ω([]byte("{}")).Should(MatchUnorderedJSON([]byte("{}")))
Ω("{}").Should(MatchUnorderedJSON([]byte("{}")))
Ω([]byte("{}")).Should(MatchUnorderedJSON("{}"))
})
})
Context("When there are arrays that are unordered", func() {
It("should succeed if the JSON matches", func() {
Ω(`{"a":[1,2,3]}`).Should(MatchUnorderedJSON(`{"a":[3,2,1]}`))
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).Should(MatchUnorderedJSON(`{"a":[3,2,1],"b":[3,2,1],"c":[3,2,1]}`))
Ω(`[[1,2,3],[4,5,6],[7,8,9]]`).Should(MatchUnorderedJSON(`[[9,8,7],[6,5,4],[3,2,1]]`))
Ω(`[[1,2,3],[1,2,3],[1,2,3]]`).Should(MatchUnorderedJSON(`[[3,2,1],[3,2,1],[3,2,1]]`))
})
})
Context("When some of the keys are ordered", func() {
It("should succeed if the JSON matches", func() {
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).Should(MatchUnorderedJSON(`{"a":[1,2,3],"b":[3,2,1],"c":[3,2,1]}`, WithOrderedListKeys("a")))
})
It("should not succeed if a ordered key doesn't match", func() {
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).ShouldNot(MatchUnorderedJSON(`{"a":[3,2,1],"b":[3,2,1],"c":[3,2,1]}`, WithOrderedListKeys("a")))
})
})
Context("When some of the keys are Unordered", func() {
It("should succeed if the JSON matches", func() {
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).Should(MatchOrderedJSON(`{"a":[3,2,1],"b":[1,2,3],"c":[1,2,3]}`, WithUnorderedListKeys("a")))
})
})
Context("SubsetMatching", func() {
It("should succeed if the JSON is contained", func() {
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).Should(ContainOrderedJSON(`{"b":[1,2,3]}`))
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).Should(ContainUnorderedJSON(`{"b":[3,2,1]}`))
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).Should(ContainUnorderedJSON(`{"a":[1,2,3],"b":[3,2,1]}`, WithOrderedListKeys("a")))
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).Should(ContainOrderedJSON(`{"a":[3,2,1],"b":[1,2,3]}`, WithUnorderedListKeys("a")))
})
It("should Not succeed if the JSON isnt contained", func() {
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).ShouldNot(ContainOrderedJSON(`{"b":[3,2,1]}`))
Ω(`{"b":[3,2,1]}`).ShouldNot(ContainOrderedJSON(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`))
Ω(`{"a":[1,2,3],"b":[1,2,3],"c":[1,2,3]}`).ShouldNot(ContainUnorderedJSON(`{"a":[3,2,1],"b":[3,2,1]}`, WithOrderedListKeys("a")))
})
})
Context("when a key mismatch is found", func() {
It("reports the first found mismatch", func() {
subject := ExpandedJsonMatcher{JSONToMatch: `5`}
actual := `7`
subject.Match(actual)
failureMessage := subject.FailureMessage(`7`)
Ω(failureMessage).ToNot(ContainSubstring("first mismatched key"))
subject = ExpandedJsonMatcher{JSONToMatch: `{"a": 1, "b.g": {"c": 2, "1": ["hello", "see ya"]}}`}
actual = `{"a": 1, "b.g": {"c": 2, "1": ["hello", "goodbye"]}}`
subject.Match(actual)
failureMessage = subject.FailureMessage(actual)
Ω(failureMessage).To(ContainSubstring(`first mismatched key: "b.g"."1"`)) //Only report on the array, not the index
})
It("reports the first found mismatch as Json does when the key is ordered", func() {
subject := ExpandedJsonMatcher{JSONToMatch: `5`}
actual := `7`
subject.Match(actual)
failureMessage := subject.FailureMessage(`7`)
Ω(failureMessage).ToNot(ContainSubstring("first mismatched key"))
subject = ExpandedJsonMatcher{
JSONToMatch: `{"a": 1, "b.g": {"c": 2, "1": ["hello", "see ya"]}}`,
DeepMatcher: UnmarshalledDeepMatcher{
Ordered: false,
Subset: false,
InvertOrderingKeys: map[interface{}]bool{"1": true},
},
}
actual = `{"a": 1, "b.g": {"c": 2, "1": ["hello", "goodbye"]}}`
subject.Match(actual)
failureMessage = subject.FailureMessage(actual)
Ω(failureMessage).To(ContainSubstring(`first mismatched key: "b.g"."1"[1]`))
})
})
Context("when the expected is not valid JSON", func() {
It("should error and explain why", func() {
success, err := (&ExpandedJsonMatcher{JSONToMatch: `{}`}).Match(`oops`)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("Actual 'oops' should be valid JSON"))
})
})
Context("when the actual is not valid JSON", func() {
It("should error and explain why", func() {
success, err := (&ExpandedJsonMatcher{JSONToMatch: `oops`}).Match(`{}`)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("Expected 'oops' should be valid JSON"))
})
})
Context("when the expected is neither a string nor a stringer nor a byte array", func() {
It("should error", func() {
success, err := (&ExpandedJsonMatcher{JSONToMatch: 2}).Match("{}")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("ExpandedJsonMatcher matcher requires a string, stringer, or []byte. Got expected:\n <int>: 2"))
success, err = (&ExpandedJsonMatcher{JSONToMatch: nil}).Match("{}")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("ExpandedJsonMatcher matcher requires a string, stringer, or []byte. Got expected:\n <nil>: nil"))
})
})
Context("when the actual is neither a string nor a stringer nor a byte array", func() {
It("should error", func() {
success, err := (&ExpandedJsonMatcher{JSONToMatch: "{}"}).Match(2)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("ExpandedJsonMatcher matcher requires a string, stringer, or []byte. Got actual:\n <int>: 2"))
success, err = (&ExpandedJsonMatcher{JSONToMatch: "{}"}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("ExpandedJsonMatcher matcher requires a string, stringer, or []byte. Got actual:\n <nil>: nil"))
})
})
})