github.com/onsi/gomega@v1.32.0/matchers/match_yaml_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 . "github.com/onsi/ginkgo/v2" 5 . "github.com/onsi/gomega" 6 7 . "github.com/onsi/gomega/matchers" 8 ) 9 10 var _ = Describe("MatchYAMLMatcher", func() { 11 Context("When passed stringifiables", func() { 12 It("should succeed if the YAML matches", func() { 13 Expect("---").Should(MatchYAML("")) 14 Expect("a: 1").Should(MatchYAML(`{"a":1}`)) 15 Expect("a: 1\nb: 2").Should(MatchYAML(`{"b":2, "a":1}`)) 16 }) 17 18 It("should explain if the YAML does not match when it should", func() { 19 message := (&MatchYAMLMatcher{YAMLToMatch: "a: 1"}).FailureMessage("b: 2") 20 Expect(message).To(MatchRegexp(`Expected\s+<string>: b: 2\s+to match YAML of\s+<string>: a: 1`)) 21 }) 22 23 It("should normalise the expected and actual when explaining if the YAML does not match when it should", func() { 24 message := (&MatchYAMLMatcher{YAMLToMatch: "a: 'one'"}).FailureMessage("{b: two}") 25 Expect(message).To(MatchRegexp(`Expected\s+<string>: b: two\s+to match YAML of\s+<string>: a: one`)) 26 }) 27 28 It("should explain if the YAML matches when it should not", func() { 29 message := (&MatchYAMLMatcher{YAMLToMatch: "a: 1"}).NegatedFailureMessage("a: 1") 30 Expect(message).To(MatchRegexp(`Expected\s+<string>: a: 1\s+not to match YAML of\s+<string>: a: 1`)) 31 }) 32 33 It("should normalise the expected and actual when explaining if the YAML matches when it should not", func() { 34 message := (&MatchYAMLMatcher{YAMLToMatch: "a: 'one'"}).NegatedFailureMessage("{a: one}") 35 Expect(message).To(MatchRegexp(`Expected\s+<string>: a: one\s+not to match YAML of\s+<string>: a: one`)) 36 }) 37 38 It("should fail if the YAML does not match", func() { 39 Expect("a: 1").ShouldNot(MatchYAML(`{"b":2, "a":1}`)) 40 }) 41 42 It("should work with byte arrays", func() { 43 Expect([]byte("a: 1")).Should(MatchYAML([]byte("a: 1"))) 44 Expect("a: 1").Should(MatchYAML([]byte("a: 1"))) 45 Expect([]byte("a: 1")).Should(MatchYAML("a: 1")) 46 }) 47 }) 48 49 When("the expected is not valid YAML", func() { 50 It("should error and explain why", func() { 51 success, err := (&MatchYAMLMatcher{YAMLToMatch: ""}).Match("good:\nbad") 52 Expect(success).Should(BeFalse()) 53 Expect(err).Should(HaveOccurred()) 54 Expect(err.Error()).Should(ContainSubstring("Actual 'good:\nbad' should be valid YAML")) 55 }) 56 }) 57 58 When("the actual is not valid YAML", func() { 59 It("should error and explain why", func() { 60 success, err := (&MatchYAMLMatcher{YAMLToMatch: "good:\nbad"}).Match("") 61 Expect(success).Should(BeFalse()) 62 Expect(err).Should(HaveOccurred()) 63 Expect(err.Error()).Should(ContainSubstring("Expected 'good:\nbad' should be valid YAML")) 64 }) 65 66 It("errors when passed directly to Message", func() { 67 Expect(func() { 68 matcher := MatchYAMLMatcher{YAMLToMatch: "good"} 69 matcher.FailureMessage("good:\nbad") 70 }).To(Panic()) 71 }) 72 }) 73 74 When("the expected is neither a string nor a stringer nor a byte array", func() { 75 It("should error", func() { 76 success, err := (&MatchYAMLMatcher{YAMLToMatch: 2}).Match("") 77 Expect(success).Should(BeFalse()) 78 Expect(err).Should(HaveOccurred()) 79 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n <int>: 2")) 80 81 success, err = (&MatchYAMLMatcher{YAMLToMatch: nil}).Match("") 82 Expect(success).Should(BeFalse()) 83 Expect(err).Should(HaveOccurred()) 84 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n <nil>: nil")) 85 }) 86 }) 87 88 When("the actual is neither a string nor a stringer nor a byte array", func() { 89 It("should error", func() { 90 success, err := (&MatchYAMLMatcher{YAMLToMatch: ""}).Match(2) 91 Expect(success).Should(BeFalse()) 92 Expect(err).Should(HaveOccurred()) 93 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n <int>: 2")) 94 95 success, err = (&MatchYAMLMatcher{YAMLToMatch: ""}).Match(nil) 96 Expect(success).Should(BeFalse()) 97 Expect(err).Should(HaveOccurred()) 98 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n <nil>: nil")) 99 }) 100 }) 101 })