Documentation
¶
Overview ¶
Package testfile has test helpers that work by comparing files.
Example ¶
package main
import (
"strings"
"testing"
"github.com/carlmjohnson/be"
"github.com/carlmjohnson/be/testfile"
)
func main() {
t := &testing.T{}
// Make some test data
input := "Hello, World!"
got := strings.ToUpper(input)
// Write files
testfile.Write(t, "example/upper.txt", got)
// Read files
file := testfile.Read(t, "example/upper.txt")
// Do some testing
be.Equal(t, file, got)
// Use the equality helper
testfile.Equal(t, "example/upper.txt", got)
// If the files aren't equal,
// got will be written to example/-failed-upper.txt
// Or use JSON helpers for complex structs
type testcase struct {
Input, Output string
}
s := testcase{input, got}
// Write out pretty printed JSON
testfile.WriteJSON(t, "example/upper.json", s)
// Read from a JSON file
var s2 testcase
testfile.ReadJSON(t, "example/upper.json", &s2)
be.Equal(t, s, s2)
// Test that s equals the contents of a file when serialized
testfile.EqualJSON(t, "example/upper.json", s)
}
Output:
Index ¶
- func Equal(t testing.TB, wantFile, gotStr string)
- func EqualJSON(t testing.TB, wantFile string, v any)
- func Equalish(t testing.TB, wantFile, gotStr string)
- func Ext(path, ext string) string
- func Read(t testing.TB, path string) string
- func ReadJSON(t testing.TB, path string, v any)
- func Run(t *testing.T, glob string, f func(t *testing.T, match string))
- func Write(t testing.TB, path, data string)
- func WriteJSON(t testing.TB, path string, v any)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
Equal tests whether gotStr is equal to the contents of wantFile. If they are not equal, it writes gotStr to a file with -failed- prepended to its name and calls t.Fatalf.
If the environmental variable TESTFILE_UPDATE is set, an unequal file will be overwritten with gotStr, but the test will still fail to prevent accidental updates from going unnoticed.
func EqualJSON ¶
EqualJSON tests whether when v is mashaled as JSON, it is equal to the contents of wantFile. The contents of wantFile must have two spaces for indentation. EqualJSON just uses string equality and does not test for JSON equivalency. If they are not equal, it writes out a file with the contents of v and calls t.Fatalf. If there is an error, it calls t.Fatalf.
If the environmental variable TESTFILE_UPDATE is set, an unequal file will be overwritten with v, but the test will still fail to prevent accidental updates from going unnoticed.
func Equalish ¶
Equalish is like Equal, but it uses strings.TrimSpace before testing for equality.
If the environmental variable TESTFILE_UPDATE is set, an unequal file will be overwritten with gotStr, but the test will still fail to prevent accidental updates from going unnoticed.
func Ext ¶ added in v0.24.1
Ext return path with its current extension stripped and ext added. It treats ext with and without a leading dot the same for simplicity of operation. If ext is "", path is returned with its current extension stripped off.
Example ¶
package main
import (
"fmt"
"github.com/carlmjohnson/be/testfile"
)
func main() {
for _, path := range []string{
"foo.txt",
"foo.bar/spam",
} {
fmt.Println(testfile.Ext(path, ""))
fmt.Println(testfile.Ext(path, ".json"))
fmt.Println(testfile.Ext(path, "gob"))
}
}
Output: foo foo.json foo.gob foo.bar/spam foo.bar/spam.json foo.bar/spam.gob
func ReadJSON ¶
ReadJSON attempts to unmarshal the contents of a file at path into v. If there is an error, it calls t.Fatalf.
func Run ¶
Run a subtest for each file matching the provided glob pattern.
Example ¶
package main
import (
"strings"
"testing"
"github.com/carlmjohnson/be/testfile"
)
func main() {
_ = func(t *testing.T) {
// For each .txt file, start a sub-test
testfile.Run(t, "testdata/*.txt", func(t *testing.T, path string) {
// Read the file
input := testfile.Read(t, path)
// Do some conversion on it
type myStruct struct{ Whatever string }
got := myStruct{strings.ToUpper(input)}
// See if the struct is equivalent to a .json file
wantFile := testfile.Ext(path, ".json")
testfile.EqualJSON(t, wantFile, got)
// If it's not equivalent,
// the got struct will be dumped
// to a file named testdata/-failed-test-name.json
})
}
}
Output:
Types ¶
This section is empty.