Skip to content

Commit

Permalink
test: add assertion for Close
Browse files Browse the repository at this point in the history
Add an assertion for calling .Close() and ensuring no error is returned.
  • Loading branch information
shoenig committed Aug 8, 2023
1 parent b8f0ac5 commit f8b70b8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -1044,6 +1045,15 @@ func FilePathValid(path string) (s string) {
return
}

func Close(c io.Closer) (s string) {
err := c.Close()
if err != nil {
s = "calling Close failed\n"
s += bullet("error: %v\n", err)
}
return
}

func StrEqFold(exp, val string) (s string) {
if !strings.EqualFold(exp, val) {
s = "expected strings to be equal ignoring case\n"
Expand Down
7 changes: 7 additions & 0 deletions must/must.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -597,6 +598,12 @@ func FilePathValid(t T, path string, settings ...Setting) {
invoke(t, assertions.FilePathValid(path), settings...)
}

// Close asserts c.Close does not cause an error.
func Close(t T, c io.Closer) {
t.Helper()
invoke(t, assertions.Close(c))
}

// StrEqFold asserts exp and val are equivalent, ignoring case.
func StrEqFold(t T, exp, val string, settings ...Setting) {
t.Helper()
Expand Down
16 changes: 16 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,22 @@ func TestFilePathValid(t *testing.T) {
FilePathValid(tc, "foo/../bar")
}

type closer struct {
err error
}

func (c *closer) Close() error {
return c.err
}

func TestClose(t *testing.T) {
tc := newCase(t, `calling Close failed`)
t.Cleanup(tc.assert)

c := &closer{err: errors.New("oops")}
Close(tc, c)
}

func TestStrEqFold(t *testing.T) {
tc := newCase(t, `expected strings to be equal ignoring case`)
t.Cleanup(tc.assert)
Expand Down

0 comments on commit f8b70b8

Please sign in to comment.