Skip to content

Commit a45c384

Browse files
authored
Merge pull request #1 from ninthclowd/master
Adds WithDescription option to Match(), allowing multiple gold files in the same test
2 parents f5cabd6 + 0e49ef8 commit a45c384

5 files changed

Lines changed: 53 additions & 2 deletions

File tree

examples/main_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ var _ = Describe("Examples", func() {
3131
"e": []string{"a", "b", "c"},
3232
}).To(goldga.Match())
3333
})
34+
35+
It("multiple gold files in the same test", func() {
36+
Expect("foo").To(goldga.Match(goldga.WithDescription("first gold file")))
37+
Expect("bar").To(goldga.Match(goldga.WithDescription("second gold file")))
38+
Expect("foobar").To(goldga.Match(goldga.WithDescription("third gold file")))
39+
})
3440
})

examples/testdata/main.golden

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
}
1717
}
1818
'''
19+
"Examples multiple gold files in the same test (first gold file)" = '''
20+
(string) (len=3) "foo"
21+
'''
22+
"Examples multiple gold files in the same test (second gold file)" = '''
23+
(string) (len=3) "bar"
24+
'''
25+
"Examples multiple gold files in the same test (third gold file)" = '''
26+
(string) (len=6) "foobar"
27+
'''
1928
"Examples string" = '''
2029
(string) (len=3) "abc"
2130
'''

matcher.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@ import (
1111
"github.com/spf13/afero"
1212
)
1313

14+
type Option func(*Matcher)
15+
16+
// WithDescription adds an optional description to the gold file, allowing multiple gold files per test
17+
func WithDescription(description string) Option {
18+
return func(matcher *Matcher) {
19+
if s, ok := matcher.Storage.(*SuiteStorage); ok {
20+
s.Name = fmt.Sprintf("%s (%s)", s.Name, description)
21+
}
22+
}
23+
}
24+
1425
func getUpdateFile() bool {
1526
update, _ := strconv.ParseBool(os.Getenv("UPDATE_GOLDEN"))
1627
return update
1728
}
1829

19-
func Match() *Matcher {
20-
return &Matcher{
30+
func Match(options ...Option) *Matcher {
31+
m := &Matcher{
2132
Serializer: DefaultSerializer,
2233
Transformer: DefaultTransformer,
2334
Storage: &SuiteStorage{
@@ -28,6 +39,10 @@ func Match() *Matcher {
2839
Differ: DefaultDiffer,
2940
UpdateFile: getUpdateFile(),
3041
}
42+
for _, option := range options {
43+
option(m)
44+
}
45+
return m
3146
}
3247

3348
var _ types.GomegaMatcher = (*Matcher)(nil)

matcher_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,13 @@ var _ = Describe("Matcher", func() {
151151
testError(HaveOccurred())
152152
})
153153
})
154+
155+
var _ = Describe("Options", func() {
156+
Describe("WithDescription", func() {
157+
It("should append a description to the test name, allowing multiple gold files per test", func() {
158+
Expect("foo").To(Match(WithDescription("First Gold File")))
159+
Expect("bar").To(Match(WithDescription("Second Gold File")))
160+
Expect("foobar").To(Match(WithDescription("Third Gold File")))
161+
})
162+
})
163+
})

testdata/matcher.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Generated by goldga. DO NOT EDIT.
2+
[snapshots]
3+
"Options WithDescription should append a description to the test name, allowing multiple gold files per test (First Gold File)" = '''
4+
(string) (len=3) "foo"
5+
'''
6+
"Options WithDescription should append a description to the test name, allowing multiple gold files per test (Second Gold File)" = '''
7+
(string) (len=3) "bar"
8+
'''
9+
"Options WithDescription should append a description to the test name, allowing multiple gold files per test (Third Gold File)" = '''
10+
(string) (len=6) "foobar"
11+
'''

0 commit comments

Comments
 (0)