github.com/onsi/gomega@v1.32.0/matchers/match_json_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 "encoding/json" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/matchers" 9 ) 10 11 var _ = Describe("MatchJSONMatcher", func() { 12 Context("When passed stringifiables", func() { 13 It("should succeed if the JSON matches", func() { 14 Expect("{}").Should(MatchJSON("{}")) 15 Expect(`{"a":1}`).Should(MatchJSON(`{"a":1}`)) 16 Expect(`{ 17 "a":1 18 }`).Should(MatchJSON(`{"a":1}`)) 19 Expect(`{"a":1, "b":2}`).Should(MatchJSON(`{"b":2, "a":1}`)) 20 Expect(`{"a":1}`).ShouldNot(MatchJSON(`{"b":2, "a":1}`)) 21 22 Expect(`{"a":"a", "b":"b"}`).ShouldNot(MatchJSON(`{"a":"a", "b":"b", "c":"c"}`)) 23 Expect(`{"a":"a", "b":"b", "c":"c"}`).ShouldNot(MatchJSON(`{"a":"a", "b":"b"}`)) 24 25 Expect(`{"a":null, "b":null}`).ShouldNot(MatchJSON(`{"c":"c", "d":"d"}`)) 26 Expect(`{"a":null, "b":null, "c":null}`).ShouldNot(MatchJSON(`{"a":null, "b":null, "d":null}`)) 27 }) 28 29 It("should work with byte arrays", func() { 30 Expect([]byte("{}")).Should(MatchJSON([]byte("{}"))) 31 Expect("{}").Should(MatchJSON([]byte("{}"))) 32 Expect([]byte("{}")).Should(MatchJSON("{}")) 33 }) 34 35 It("should work with json.RawMessage", func() { 36 Expect([]byte(`{"a": 1}`)).Should(MatchJSON(json.RawMessage(`{"a": 1}`))) 37 }) 38 }) 39 40 When("a key mismatch is found", func() { 41 It("reports the first found mismatch", func() { 42 subject := MatchJSONMatcher{JSONToMatch: `5`} 43 actual := `7` 44 subject.Match(actual) 45 46 failureMessage := subject.FailureMessage(`7`) 47 Expect(failureMessage).ToNot(ContainSubstring("first mismatched key")) 48 49 subject = MatchJSONMatcher{JSONToMatch: `{"a": 1, "b.g": {"c": 2, "1": ["hello", "see ya"]}}`} 50 actual = `{"a": 1, "b.g": {"c": 2, "1": ["hello", "goodbye"]}}` 51 subject.Match(actual) 52 53 failureMessage = subject.FailureMessage(actual) 54 Expect(failureMessage).To(ContainSubstring(`first mismatched key: "b.g"."1"[1]`)) 55 }) 56 }) 57 58 When("the expected is not valid JSON", func() { 59 It("should error and explain why", func() { 60 success, err := (&MatchJSONMatcher{JSONToMatch: `{}`}).Match(`oops`) 61 Expect(success).Should(BeFalse()) 62 Expect(err).Should(HaveOccurred()) 63 Expect(err.Error()).Should(ContainSubstring("Actual 'oops' should be valid JSON")) 64 }) 65 }) 66 67 When("the actual is not valid JSON", func() { 68 It("should error and explain why", func() { 69 success, err := (&MatchJSONMatcher{JSONToMatch: `oops`}).Match(`{}`) 70 Expect(success).Should(BeFalse()) 71 Expect(err).Should(HaveOccurred()) 72 Expect(err.Error()).Should(ContainSubstring("Expected 'oops' should be valid JSON")) 73 }) 74 }) 75 76 When("the expected is neither a string nor a stringer nor a byte array", func() { 77 It("should error", func() { 78 success, err := (&MatchJSONMatcher{JSONToMatch: 2}).Match("{}") 79 Expect(success).Should(BeFalse()) 80 Expect(err).Should(HaveOccurred()) 81 Expect(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got expected:\n <int>: 2")) 82 83 success, err = (&MatchJSONMatcher{JSONToMatch: nil}).Match("{}") 84 Expect(success).Should(BeFalse()) 85 Expect(err).Should(HaveOccurred()) 86 Expect(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got expected:\n <nil>: nil")) 87 }) 88 }) 89 90 When("the actual is neither a string nor a stringer nor a byte array", func() { 91 It("should error", func() { 92 success, err := (&MatchJSONMatcher{JSONToMatch: "{}"}).Match(2) 93 Expect(success).Should(BeFalse()) 94 Expect(err).Should(HaveOccurred()) 95 Expect(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got actual:\n <int>: 2")) 96 97 success, err = (&MatchJSONMatcher{JSONToMatch: "{}"}).Match(nil) 98 Expect(success).Should(BeFalse()) 99 Expect(err).Should(HaveOccurred()) 100 Expect(err.Error()).Should(ContainSubstring("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got actual:\n <nil>: nil")) 101 }) 102 }) 103 104 It("shows negated failure message", func() { 105 failuresMessages := InterceptGomegaFailures(func() { 106 Expect("1").ToNot(MatchJSON("1")) 107 }) 108 Expect(failuresMessages).To(Equal([]string{"Expected\n <string>: 1\nnot to match JSON of\n <string>: 1"})) 109 }) 110 })