added support for string type
[govpp.git] / vendor / github.com / onsi / gomega / matchers / match_yaml_matcher_test.go
1 package matchers_test
2
3 import (
4         . "github.com/onsi/ginkgo"
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         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"))
55                 })
56         })
57
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"))
64                 })
65         })
66
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"))
73
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"))
78                 })
79         })
80
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"))
87
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"))
92                 })
93         })
94 })