Skip to content

Commit

Permalink
Fix order of arguments passed to File (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jul 12, 2024
1 parent 30360ab commit 7e82f43
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@ func Data(t testing.TB) string {
return filepath.Join(cwd, "testdata")
}

// File fails if the contents of the given file do not match want.
// File fails if got does not match the contents of the given file.
//
// It takes the name of a file (relative to $CWD/testdata) and the contents to compare.
// It takes a string and the name of a file (relative to $CWD/testdata) to compare.
//
// If the contents differ, the test will fail with output equivalent to [Diff].
//
// Files with differing line endings (e.g windows CR LF \r\n vs unix LF \n) will be normalised to
// \n prior to comparison so this function will behave identically across multiple platforms.
//
// test.File(t, "expected.txt", "hello\n")
func File(t testing.TB, file, want string) {
// test.File(t, "hello\n", "expected.txt")
func File(t testing.TB, got, file string) {
t.Helper()
file = filepath.Join(Data(t), file)
contents, err := os.ReadFile(file)
Expand All @@ -213,7 +213,7 @@ func File(t testing.TB, file, want string) {

contents = bytes.ReplaceAll(contents, []byte("\r\n"), []byte("\n"))

Diff(t, string(contents), want)
Diff(t, got, string(contents))
}

// CaptureOutput captures and returns data printed to stdout and stderr by the provided function fn, allowing
Expand Down
6 changes: 3 additions & 3 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestPass(t *testing.T) {
"DeepEqual string slice": func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"hello"}) },
"WantErr true": func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), true) },
"WantErr false": func(tb testing.TB) { test.WantErr(tb, nilErr(), false) },
"File": func(tb testing.TB) { test.File(tb, "file.txt", "hello\n") },
"File": func(tb testing.TB) { test.File(tb, "hello\n", "file.txt") },
}

for name, fn := range passFns {
Expand Down Expand Up @@ -181,8 +181,8 @@ func TestFail(t *testing.T) {
"DeepEqual string slice": func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"world"}) },
"WantErr true": func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), false) },
"WantErr false": func(tb testing.TB) { test.WantErr(tb, nilErr(), true) },
"File wrong": func(tb testing.TB) { test.File(tb, "file.txt", "wrong\n") },
"File missing": func(tb testing.TB) { test.File(tb, "missing.txt", "wrong\n") },
"File wrong": func(tb testing.TB) { test.File(tb, "wrong\n", "file.txt") },
"File missing": func(tb testing.TB) { test.File(tb, "wrong\n", "missing.txt") },
}

for name, fn := range failFns {
Expand Down

0 comments on commit 7e82f43

Please sign in to comment.