4 . "github.com/onsi/ginkgo"
5 . "github.com/onsi/gomega"
7 . "github.com/onsi/gomega/matchers"
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}`))
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`))
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`))
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`))
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`))
38 It("should fail if the YAML does not match", func() {
39 Expect("a: 1").ShouldNot(MatchYAML(`{"b":2, "a":1}`))
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"))
49 Context("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"))
58 Context("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"))
67 Context("when the expected is neither a string nor a stringer nor a byte array", func() {
68 It("should error", func() {
69 success, err := (&MatchYAMLMatcher{YAMLToMatch: 2}).Match("")
70 Expect(success).Should(BeFalse())
71 Expect(err).Should(HaveOccurred())
72 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n <int>: 2"))
74 success, err = (&MatchYAMLMatcher{YAMLToMatch: nil}).Match("")
75 Expect(success).Should(BeFalse())
76 Expect(err).Should(HaveOccurred())
77 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n <nil>: nil"))
81 Context("when the actual is neither a string nor a stringer nor a byte array", func() {
82 It("should error", func() {
83 success, err := (&MatchYAMLMatcher{YAMLToMatch: ""}).Match(2)
84 Expect(success).Should(BeFalse())
85 Expect(err).Should(HaveOccurred())
86 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n <int>: 2"))
88 success, err = (&MatchYAMLMatcher{YAMLToMatch: ""}).Match(nil)
89 Expect(success).Should(BeFalse())
90 Expect(err).Should(HaveOccurred())
91 Expect(err.Error()).Should(ContainSubstring("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n <nil>: nil"))