Skip to content

Commit

Permalink
Merge pull request #1 from ninthclowd/master
Browse files Browse the repository at this point in the history
Adds WithDescription option to Match(), allowing multiple gold files in the same test
  • Loading branch information
tommy351 authored Jul 20, 2021
2 parents f5cabd6 + 0e49ef8 commit a45c384
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
6 changes: 6 additions & 0 deletions examples/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ var _ = Describe("Examples", func() {
"e": []string{"a", "b", "c"},
}).To(goldga.Match())
})

It("multiple gold files in the same test", func() {
Expect("foo").To(goldga.Match(goldga.WithDescription("first gold file")))
Expect("bar").To(goldga.Match(goldga.WithDescription("second gold file")))
Expect("foobar").To(goldga.Match(goldga.WithDescription("third gold file")))
})
})
9 changes: 9 additions & 0 deletions examples/testdata/main.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
}
}
'''
"Examples multiple gold files in the same test (first gold file)" = '''
(string) (len=3) "foo"
'''
"Examples multiple gold files in the same test (second gold file)" = '''
(string) (len=3) "bar"
'''
"Examples multiple gold files in the same test (third gold file)" = '''
(string) (len=6) "foobar"
'''
"Examples string" = '''
(string) (len=3) "abc"
'''
19 changes: 17 additions & 2 deletions matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ import (
"github.com/spf13/afero"
)

type Option func(*Matcher)

// WithDescription adds an optional description to the gold file, allowing multiple gold files per test
func WithDescription(description string) Option {
return func(matcher *Matcher) {
if s, ok := matcher.Storage.(*SuiteStorage); ok {
s.Name = fmt.Sprintf("%s (%s)", s.Name, description)
}
}
}

func getUpdateFile() bool {
update, _ := strconv.ParseBool(os.Getenv("UPDATE_GOLDEN"))
return update
}

func Match() *Matcher {
return &Matcher{
func Match(options ...Option) *Matcher {
m := &Matcher{
Serializer: DefaultSerializer,
Transformer: DefaultTransformer,
Storage: &SuiteStorage{
Expand All @@ -28,6 +39,10 @@ func Match() *Matcher {
Differ: DefaultDiffer,
UpdateFile: getUpdateFile(),
}
for _, option := range options {
option(m)
}
return m
}

var _ types.GomegaMatcher = (*Matcher)(nil)
Expand Down
10 changes: 10 additions & 0 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,13 @@ var _ = Describe("Matcher", func() {
testError(HaveOccurred())
})
})

var _ = Describe("Options", func() {
Describe("WithDescription", func() {
It("should append a description to the test name, allowing multiple gold files per test", func() {
Expect("foo").To(Match(WithDescription("First Gold File")))
Expect("bar").To(Match(WithDescription("Second Gold File")))
Expect("foobar").To(Match(WithDescription("Third Gold File")))
})
})
})
11 changes: 11 additions & 0 deletions testdata/matcher.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Generated by goldga. DO NOT EDIT.
[snapshots]
"Options WithDescription should append a description to the test name, allowing multiple gold files per test (First Gold File)" = '''
(string) (len=3) "foo"
'''
"Options WithDescription should append a description to the test name, allowing multiple gold files per test (Second Gold File)" = '''
(string) (len=3) "bar"
'''
"Options WithDescription should append a description to the test name, allowing multiple gold files per test (Third Gold File)" = '''
(string) (len=6) "foobar"
'''

0 comments on commit a45c384

Please sign in to comment.