4 . "github.com/onsi/ginkgo"
5 . "github.com/onsi/gomega"
6 . "github.com/onsi/gomega/matchers"
9 var _ = Describe("MatchJSONMatcher", func() {
10 Context("When passed stringifiables", func() {
11 It("should succeed if the JSON matches", func() {
12 Ω("{}").Should(MatchJSON("{}"))
13 Ω(`{"a":1}`).Should(MatchJSON(`{"a":1}`))
16 }`).Should(MatchJSON(`{"a":1}`))
17 Ω(`{"a":1, "b":2}`).Should(MatchJSON(`{"b":2, "a":1}`))
18 Ω(`{"a":1}`).ShouldNot(MatchJSON(`{"b":2, "a":1}`))
21 It("should work with byte arrays", func() {
22 Ω([]byte("{}")).Should(MatchJSON([]byte("{}")))
23 Ω("{}").Should(MatchJSON([]byte("{}")))
24 Ω([]byte("{}")).Should(MatchJSON("{}"))
28 Context("when the expected is not valid JSON", func() {
29 It("should error and explain why", func() {
30 success, err := (&MatchJSONMatcher{JSONToMatch: `{}`}).Match(`oops`)
31 Ω(success).Should(BeFalse())
32 Ω(err).Should(HaveOccurred())
33 Ω(err.Error()).Should(ContainSubstring("Actual 'oops' should be valid JSON"))
37 Context("when the actual is not valid JSON", func() {
38 It("should error and explain why", func() {
39 success, err := (&MatchJSONMatcher{JSONToMatch: `oops`}).Match(`{}`)
40 Ω(success).Should(BeFalse())
41 Ω(err).Should(HaveOccurred())
42 Ω(err.Error()).Should(ContainSubstring("Expected 'oops' should be valid JSON"))
46 Context("when the expected is neither a string nor a stringer nor a byte array", func() {
47 It("should error", func() {
48 success, err := (&MatchJSONMatcher{JSONToMatch: 2}).Match("{}")
49 Ω(success).Should(BeFalse())
50 Ω(err).Should(HaveOccurred())
51 Ω(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got expected:\n <int>: 2"))
53 success, err = (&MatchJSONMatcher{JSONToMatch: nil}).Match("{}")
54 Ω(success).Should(BeFalse())
55 Ω(err).Should(HaveOccurred())
56 Ω(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got expected:\n <nil>: nil"))
60 Context("when the actual is neither a string nor a stringer nor a byte array", func() {
61 It("should error", func() {
62 success, err := (&MatchJSONMatcher{JSONToMatch: "{}"}).Match(2)
63 Ω(success).Should(BeFalse())
64 Ω(err).Should(HaveOccurred())
65 Ω(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got actual:\n <int>: 2"))
67 success, err = (&MatchJSONMatcher{JSONToMatch: "{}"}).Match(nil)
68 Ω(success).Should(BeFalse())
69 Ω(err).Should(HaveOccurred())
70 Ω(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got actual:\n <nil>: nil"))