Skip to content

Commit

Permalink
Use Equal as a default matcher for string contents
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatcasey authored and ryanmoran committed Sep 19, 2022
1 parent f6e335d commit c0704fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
14 changes: 12 additions & 2 deletions matchers/be_a_file_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"fmt"
"os"

"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)

type BeAFileMatchingMatcher struct {
expected interface{}
expectedMatcher types.GomegaMatcher
contents string
}

func BeAFileMatching(expectedMatcher types.GomegaMatcher) *BeAFileMatchingMatcher {
func BeAFileMatching(expected interface{}) *BeAFileMatchingMatcher {
return &BeAFileMatchingMatcher{
expectedMatcher: expectedMatcher,
expected: expected,
}
}

Expand All @@ -26,6 +28,14 @@ func (matcher *BeAFileMatchingMatcher) Match(actual interface{}) (success bool,
return false, fmt.Errorf("BeAFileMatchingMatcher expects a file path")
}

if expectedString, ok := matcher.expected.(string); ok {
matcher.expectedMatcher = gomega.Equal(expectedString)
} else {
if matcher.expectedMatcher, ok = matcher.expected.(types.GomegaMatcher); !ok {
return false, fmt.Errorf("BeAFileMatching expects a string or a types.GomegaMatcher")
}
}

bytes, err := os.ReadFile(actualFilename)
if err != nil {
return false, err
Expand Down
26 changes: 26 additions & 0 deletions matchers/be_a_file_matching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ func testBeAFileMatching(t *testing.T, context spec.G, it spec.S) {
Expect(os.WriteFile(filename, []byte("hello world"), os.ModePerm)).To(Succeed())
})

context("uses Equal by default", func() {
it("will pass", func() {
matcher = matchers.BeAFileMatching("hello world")

match, err := matcher.Match(filename)
Expect(match).To(Equal(true))
Expect(err).NotTo(HaveOccurred())
})

it("will fail", func() {
matcher = matchers.BeAFileMatching("foobar")

match, err := matcher.Match(filename)
Expect(match).To(Equal(false))
Expect(err).NotTo(HaveOccurred())
})
})

context("will wrap ContainsSubstring", func() {
it("will pass", func() {
matcher = matchers.BeAFileMatching(ContainSubstring("hello"))
Expand Down Expand Up @@ -120,6 +138,14 @@ func testBeAFileMatching(t *testing.T, context spec.G, it spec.S) {
})

context("failure cases", func() {
context("BeAFileMatching is called with a non-string and non-types.GomegaMatcher", func() {
it("will return an error", func() {
matcher = matchers.BeAFileMatching(42)
_, err := matcher.Match(filename)
Expect(err).To(MatchError("BeAFileMatching expects a string or a types.GomegaMatcher"))
})
})

context("Match is called with a non-string", func() {
it("will return an error", func() {
matcher = matchers.BeAFileMatching(ContainSubstring(""))
Expand Down

0 comments on commit c0704fd

Please sign in to comment.