This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
match_yaml_test.go
180 lines (154 loc) · 5.99 KB
/
match_yaml_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package gomegamatchers_test
import (
"io/ioutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pivotal-cf-experimental/gomegamatchers"
)
type animalStringer struct {
Data string
}
func (a animalStringer) String() string {
return a.Data
}
var _ = Describe("HelpfullyMatchYAMLMatcher", func() {
var animals, plants string
BeforeEach(func() {
animals = "- cats:\n - lion\n- fish:\n - salmon"
plants = "- tropical:\n - palm\n- desert:\n - cactus"
})
Describe("Match", func() {
It("works with complex yaml", func() {
yaml, err := ioutil.ReadFile("fixtures/santa_monica_correct.yml")
Expect(err).NotTo(HaveOccurred())
isMatch, err := gomegamatchers.HelpfullyMatchYAML(yaml).Match(yaml)
Expect(err).NotTo(HaveOccurred())
Expect(isMatch).To(BeTrue())
})
Context("when arguments are strings", func() {
It("returns true when the YAML matches", func() {
isMatch, err := gomegamatchers.HelpfullyMatchYAML(animals).Match(animals)
Expect(err).NotTo(HaveOccurred())
Expect(isMatch).To(BeTrue())
})
It("returns false when the YAML does not match", func() {
isMatch, err := gomegamatchers.HelpfullyMatchYAML(animals).Match(plants)
Expect(err).NotTo(HaveOccurred())
Expect(isMatch).To(BeFalse())
})
})
Context("when an input is a byte slice", func() {
var animalBytes []byte
BeforeEach(func() {
animalBytes = []byte(animals)
})
It("returns true when the YAML matches", func() {
isMatch, err := gomegamatchers.HelpfullyMatchYAML(animalBytes).Match(animals)
Expect(err).NotTo(HaveOccurred())
Expect(isMatch).To(BeTrue())
})
It("returns false when the YAML does not match", func() {
isMatch, err := gomegamatchers.HelpfullyMatchYAML(animalBytes).Match(plants)
Expect(err).NotTo(HaveOccurred())
Expect(isMatch).To(BeFalse())
})
})
Context("when an input is a Stringer", func() {
var stringer animalStringer
BeforeEach(func() {
stringer = animalStringer{
Data: animals,
}
})
It("returns true when the YAML matches", func() {
isMatch, err := gomegamatchers.HelpfullyMatchYAML(stringer).Match(animals)
Expect(err).NotTo(HaveOccurred())
Expect(isMatch).To(BeTrue())
})
It("returns false when the YAML does not match", func() {
isMatch, err := gomegamatchers.HelpfullyMatchYAML(stringer).Match(plants)
Expect(err).NotTo(HaveOccurred())
Expect(isMatch).To(BeFalse())
})
})
Describe("errors", func() {
It("returns an error when one of the inputs is not a string, byte slice, or Stringer", func() {
_, err := gomegamatchers.HelpfullyMatchYAML(animals).Match(123213)
Expect(err.Error()).To(ContainSubstring("HelpfullyMatchYAMLMatcher matcher requires a string or stringer."))
Expect(err.Error()).To(ContainSubstring("Got:\n <int>: 123213"))
})
It("returns an error when the YAML is invalid", func() {
_, err := gomegamatchers.HelpfullyMatchYAML(animals).Match("some: invalid: yaml")
Expect(err.Error()).To(ContainSubstring("mapping values are not allowed in this context"))
})
})
})
Describe("FailureMessage", func() {
It("returns a failure message", func() {
message := gomegamatchers.HelpfullyMatchYAML("a: 1").FailureMessage("b: 2")
Expect(message).To(ContainSubstring("error at :"))
Expect(message).To(ContainSubstring(" extra key found:"))
Expect(message).To(ContainSubstring(" Expected"))
Expect(message).To(ContainSubstring(" [<string> b]"))
Expect(message).To(ContainSubstring(" not to contain key"))
Expect(message).To(ContainSubstring(" <string> b"))
})
It("provides localized error information", func() {
correctYAML, err := ioutil.ReadFile("fixtures/santa_monica_correct.yml")
Expect(err).NotTo(HaveOccurred())
incorrectYAML, err := ioutil.ReadFile("fixtures/santa_monica_incorrect.yml")
Expect(err).NotTo(HaveOccurred())
message := gomegamatchers.HelpfullyMatchYAML(string(correctYAML)).FailureMessage(string(incorrectYAML))
Expect(message).To(
SatisfyAny(
SatisfyAll(
ContainSubstring("error at [population][1980][absolute]:"),
ContainSubstring(" value mismatch:"),
ContainSubstring(" Expected"),
ContainSubstring(" <int> 999999999"),
ContainSubstring(" to equal"),
ContainSubstring(" <int> 88314"),
),
SatisfyAll(
ContainSubstring("error at [population][2000][absolute]:"),
ContainSubstring(" type mismatch:"),
ContainSubstring(" Expected"),
ContainSubstring(" <string> wrong type"),
ContainSubstring(" to be of type"),
ContainSubstring(" <int>"),
),
SatisfyAll(
ContainSubstring("error at [population][1990]:"),
ContainSubstring(" extra key found:"),
ContainSubstring(" Expected"),
ContainSubstring(" [<string> growth_rate, <string> wrong_key]"),
ContainSubstring(" not to contain key"),
ContainSubstring(" <string> wrong_key"),
),
SatisfyAll(
ContainSubstring("error at [population][1990]:"),
ContainSubstring(" extra key found:"),
ContainSubstring(" Expected"),
ContainSubstring(" [<string> wrong_key, <string> growth_rate]"),
ContainSubstring(" not to contain key"),
ContainSubstring(" <string> wrong_key"),
),
))
})
Describe("errors", func() {
It("returns the error as the message", func() {
message := gomegamatchers.HelpfullyMatchYAML(animals).FailureMessage("some: invalid: yaml")
Expect(message).To(ContainSubstring("mapping values are not allowed in this context"))
})
})
})
Describe("NegatedFailureMessage", func() {
It("returns a negated failure message", func() {
message := gomegamatchers.HelpfullyMatchYAML("a: 1").NegatedFailureMessage("b: 2")
Expect(message).To(ContainSubstring("Expected"))
Expect(message).To(ContainSubstring("<string>: b: 2"))
Expect(message).To(ContainSubstring("not to match YAML of"))
Expect(message).To(ContainSubstring("<string>: a: 1"))
})
})
})