Something went wrong. Try again.
A lightweight test helper package
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733package test_test
import ( "bytes" "errors" "flag" "fmt" "io" "math" "os" "slices" "testing"
"go.followtheprocess.codes/snapshot" "go.followtheprocess.codes/test")
var ( update = flag.Bool("update", false, "Update snapshots") clean = flag.Bool("clean", false, "Erase all snapshots and recreate them from scratch"))
// TB is a fake implementation of [testing.TB] that simply records in internal// state whether or not it would have failed and what it would have written.type TB struct { testing.TB
out io.Writer failed bool}
func (t *TB) Helper() {}
func (t *TB) Fatal(args ...any) { t.failed = true fmt.Fprint(t.out, args...)}
func (t *TB) Fatalf(format string, args ...any) { t.failed = true fmt.Fprintf(t.out, format, args...)}
func TestTest(t *testing.T) { tests := []struct { fn func(tb testing.TB) // The test function we're... testing? name string // Name of the test case wantFail bool // Whether it should fail }{ { name: "Equal/pass", fn: func(tb testing.TB) { test.Equal(tb, "apples", "apples") }, wantFail: false, }, { name: "Equal/fail", fn: func(tb testing.TB) { test.Equal(tb, "apples", "oranges") }, wantFail: true, }, { name: "Equal/fail with context", fn: func(tb testing.TB) { test.Equal(tb, "apples", "oranges", test.Context("Apples are not oranges!")) }, wantFail: true, }, { name: "Equal/fail context format", fn: func(tb testing.TB) { test.Equal(tb, "apples", "oranges", test.Context("Apples == Oranges: %v", false)) }, wantFail: true, }, { name: "Equal/fail with title", fn: func(tb testing.TB) { test.Equal(tb, "apples", "oranges", test.Title("My fruit test")) }, wantFail: true, }, { name: "NotEqual/pass", fn: func(tb testing.TB) { test.NotEqual(tb, "apples", "oranges") }, wantFail: false, }, { name: "NotEqual/fail", fn: func(tb testing.TB) { test.NotEqual(tb, "apples", "apples") }, wantFail: true, }, { name: "NotEqual/fail with context", fn: func(tb testing.TB) { test.NotEqual(tb, 42, 42, test.Context("42 is the meaning of life")) }, wantFail: true, }, { name: "NotEqual/fail context format", fn: func(tb testing.TB) { test.NotEqual(tb, 42, 42, test.Context("42 == meaning of life: %v", true)) }, wantFail: true, }, { name: "NotEqual/fail with title", fn: func(tb testing.TB) { test.NotEqual(tb, "apples", "apples", test.Title("My fruit test")) }, wantFail: true, }, { name: "EqualFunc/pass", fn: func(tb testing.TB) { test.EqualFunc(tb, []int{1, 2, 3, 4}, []int{1, 2, 3, 4}, slices.Equal) }, wantFail: false, }, { name: "EqualFunc/fail", fn: func(tb testing.TB) { cmp := func(_, _ []string) bool { return false } // Cheating test.EqualFunc(tb, []string{"hello"}, []string{"there"}, cmp) }, wantFail: true, }, { name: "EqualFunc/fail with context", fn: func(tb testing.TB) { test.EqualFunc( tb, []string{"hello"}, []string{"there"}, slices.Equal, test.Context("some context here"), ) }, wantFail: true, }, { name: "EqualFunc/fail context format", fn: func(tb testing.TB) { test.EqualFunc( tb, []string{"hello"}, []string{"there"}, slices.Equal, test.Context("who's bad at testing... %s", "you"), ) }, wantFail: true, }, { name: "EqualFunc/fail with title", fn: func(tb testing.TB) { test.EqualFunc(tb, []string{"hello"}, []string{"there"}, slices.Equal, test.Title("Hello!")) }, wantFail: true, }, { name: "NotEqualFunc/pass", fn: func(tb testing.TB) { test.NotEqualFunc(tb, []int{1, 2, 3, 4}, []int{5, 6, 7, 8}, slices.Equal) }, wantFail: false, }, { name: "NotEqualFunc/fail", fn: func(tb testing.TB) { cmp := func(_, _ []string) bool { return true } // Cheating test.NotEqualFunc(tb, []string{"hello"}, []string{"there"}, cmp) }, wantFail: true, }, { name: "NotEqualFunc/fail with context", fn: func(tb testing.TB) { test.NotEqualFunc( tb, []string{"hello"}, []string{"hello"}, slices.Equal, test.Context("some context here"), ) }, wantFail: true, }, { name: "NotEqualFunc/fail context format", fn: func(tb testing.TB) { test.NotEqualFunc( tb, []string{"hello"}, []string{"hello"}, slices.Equal, test.Context("who's bad at testing... %s", "you"), ) }, wantFail: true, }, { name: "NotEqualFunc/fail with title", fn: func(tb testing.TB) { test.NotEqualFunc(tb, []string{"hello"}, []string{"hello"}, slices.Equal, test.Title("Hello!")) }, wantFail: true, }, { name: "NearlyEqual/pass", fn: func(tb testing.TB) { test.NearlyEqual(tb, 3.0000000001, 3.0) }, wantFail: false, }, { name: "NearlyEqual/fail", fn: func(tb testing.TB) { test.NearlyEqual(tb, 3.0000001, 3.0) }, wantFail: true, }, { name: "NearlyEqual/fail custom tolerance", fn: func(tb testing.TB) { test.NearlyEqual(tb, 3.2, 3.0, test.FloatEqualityThreshold(0.1)) }, wantFail: true, }, { name: "NearlyEqual/fail with context", fn: func(tb testing.TB) { test.NearlyEqual(tb, 3.0000001, 3.0, test.Context("Numbers don't work that way")) }, wantFail: true, }, { name: "NotNearlyEqual/pass", fn: func(tb testing.TB) { test.NotNearlyEqual(tb, 3.0000001, 3.0) }, wantFail: false, }, { name: "NotNearlyEqual/fail", fn: func(tb testing.TB) { test.NotNearlyEqual(tb, 3.0000000001, 3.0) }, wantFail: true, }, { name: "NotNearlyEqual/fail custom tolerance", fn: func(tb testing.TB) { test.NotNearlyEqual(tb, 3.05, 3.0, test.FloatEqualityThreshold(0.1)) }, wantFail: true, }, { name: "NotNearlyEqual/fail with context", fn: func(tb testing.TB) { test.NotNearlyEqual(tb, 3.0000000001, 3.0, test.Context("Numbers don't work that way")) }, wantFail: true, }, { name: "Ok/pass", fn: func(tb testing.TB) { test.Ok(tb, nil) }, wantFail: false, }, { name: "Ok/fail", fn: func(tb testing.TB) { test.Ok(tb, errors.New("uh oh")) }, wantFail: true, }, { name: "Ok/fail with context", fn: func(tb testing.TB) { test.Ok(tb, errors.New("uh oh"), test.Context("Could not frobnicate the baz")) }, wantFail: true, }, { name: "Ok/fail with title", fn: func(tb testing.TB) { test.Ok(tb, errors.New("uh oh"), test.Title("Bang!")) }, wantFail: true, }, { name: "Err/pass", fn: func(tb testing.TB) { test.Err(tb, errors.New("bang")) }, wantFail: false, }, { name: "Err/fail", fn: func(tb testing.TB) { test.Err(tb, nil) }, wantFail: true, }, { name: "Err/fail with context", fn: func(tb testing.TB) { test.Err(tb, nil, test.Context("Frobnicated the baz when it should have failed")) }, wantFail: true, }, { name: "Err/fail with title", fn: func(tb testing.TB) { test.Err(tb, nil, test.Title("Everything is fine?")) }, wantFail: true, }, { name: "ErrorIs/pass", fn: func(tb testing.TB) { sentinel := errors.New("sentinel") test.ErrorIs(tb, sentinel, sentinel) }, wantFail: false, }, { name: "ErrorIs/pass wrapped", fn: func(tb testing.TB) { sentinel := errors.New("sentinel") wrapped := fmt.Errorf("while frobnicating: %w", sentinel) test.ErrorIs(tb, wrapped, sentinel) }, wantFail: false, }, { name: "ErrorIs/fail", fn: func(tb testing.TB) { test.ErrorIs(tb, errors.New("bang"), errors.New("not bang")) }, wantFail: true, }, { name: "ErrorIs/fail nil", fn: func(tb testing.TB) { test.ErrorIs(tb, nil, errors.New("wanted this one")) }, wantFail: true, }, { name: "ErrorIs/fail with context", fn: func(tb testing.TB) { test.ErrorIs(tb, errors.New("bang"), errors.New("not bang"), test.Context("Expected the other error")) }, wantFail: true, }, { name: "ErrorIs/fail with title", fn: func(tb testing.TB) { test.ErrorIs(tb, errors.New("bang"), errors.New("not bang"), test.Title("Wrong one")) }, wantFail: true, }, { name: "ErrorAs/pass", fn: func(tb testing.TB) { boom := &inputError{msg: "boom"}
got := test.ErrorAs[*inputError](tb, boom) if got != boom { tb.Fatal("ErrorAs did not return the matched error") } }, wantFail: false, }, { name: "ErrorAs/pass wrapped", fn: func(tb testing.TB) { inner := &inputError{msg: "boom"} wrapped := fmt.Errorf("while frobnicating: %w", inner)
got := test.ErrorAs[*inputError](tb, wrapped) if got != inner { tb.Fatal("ErrorAs did not return the wrapped error") } }, wantFail: false, }, { name: "ErrorAs/fail", fn: func(tb testing.TB) { test.ErrorAs[*inputError](tb, &outputError{msg: "nope"}) }, wantFail: true, }, { name: "ErrorAs/fail nil", fn: func(tb testing.TB) { test.ErrorAs[*inputError](tb, nil) }, wantFail: true, }, { name: "ErrorAs/fail with context", fn: func(tb testing.TB) { test.ErrorAs[*inputError](tb, &outputError{msg: "nope"}, test.Context("Expected an inputError")) }, wantFail: true, }, { name: "ErrorAs/fail with title", fn: func(tb testing.TB) { test.ErrorAs[*inputError](tb, &outputError{msg: "nope"}, test.Title("Type mismatch")) }, wantFail: true, }, { name: "WantErr/pass error", fn: func(tb testing.TB) { test.WantErr(tb, errors.New("bang"), true) // Wanted an error and got one - should pass }, wantFail: false, }, { name: "WantErr/pass nil", fn: func(tb testing.TB) { test.WantErr(tb, nil, false) // Didn't want an error and got nil - should pass }, wantFail: false, }, { name: "WantErr/fail error", fn: func(tb testing.TB) { test.WantErr(tb, errors.New("bang"), false) // Got an error but didn't want one - should fail }, wantFail: true, }, { name: "WantErr/fail nil", fn: func(tb testing.TB) { test.WantErr(tb, nil, true) // Didn't get an error but wanted one - should fail }, wantFail: true, }, { name: "WantErr/fail with context", fn: func(tb testing.TB) { test.WantErr(tb, errors.New("bang"), false, test.Context("Errors are bad!")) }, wantFail: true, }, { name: "WantErr/fail with title", fn: func(tb testing.TB) { test.WantErr(tb, errors.New("bang"), false, test.Title("A very bad test")) }, wantFail: true, }, { name: "True/pass", fn: func(tb testing.TB) { test.True(tb, true) }, wantFail: false, }, { name: "True/fail", fn: func(tb testing.TB) { test.True(tb, false) }, wantFail: true, }, { name: "True/fail with context", fn: func(tb testing.TB) { test.True(tb, false, test.Context("must always be true")) }, wantFail: true, }, { name: "True/fail with title", fn: func(tb testing.TB) { test.True(tb, false, test.Title("Argh!")) }, wantFail: true, }, { name: "False/pass", fn: func(tb testing.TB) { test.False(tb, false) }, wantFail: false, }, { name: "False/fail", fn: func(tb testing.TB) { test.False(tb, true) }, wantFail: true, }, { name: "False/fail with context", fn: func(tb testing.TB) { test.False(tb, true, test.Context("must always be false")) }, wantFail: true, }, { name: "False/fail with title", fn: func(tb testing.TB) { test.False(tb, true, test.Title("Argh!")) }, wantFail: true, }, { name: "Diff/pass", fn: func(tb testing.TB) { got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n" want := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n"
test.Diff(tb, got, want) }, wantFail: false, }, { name: "Diff/pass no trailing newline", fn: func(tb testing.TB) { got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff" want := "Some\nstuff here in this file\nlines as well wow\nsome more stuff"
test.Diff(tb, got, want) }, wantFail: false, }, { name: "Diff/fail", fn: func(tb testing.TB) { got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n" want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n" test.Diff(tb, got, want) }, wantFail: true, }, { name: "Diff/fail no trailing newline", fn: func(tb testing.TB) { got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff" want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff" test.Diff(tb, got, want) }, wantFail: true, }, { name: "DiffBytes/pass", fn: func(tb testing.TB) { got := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n") want := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n")
test.DiffBytes(tb, got, want) }, wantFail: false, }, { name: "DiffBytes/fail", fn: func(tb testing.TB) { got := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n") want := []byte("Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n") test.DiffBytes(tb, got, want) }, wantFail: true, }, { name: "DiffReader/pass", fn: func(tb testing.TB) { got := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n") want := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n")
test.DiffReader(tb, bytes.NewReader(got), bytes.NewReader(want)) }, wantFail: false, }, { name: "DiffReader/fail", fn: func(tb testing.TB) { got := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n") want := []byte("Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n")
test.DiffReader(tb, bytes.NewReader(got), bytes.NewReader(want)) }, wantFail: true, }, { name: "Diff/fail with title", fn: func(tb testing.TB) { got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n" want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n" test.Diff(tb, got, want, test.Title("File drift")) }, wantFail: true, }, { name: "Diff/fail with context", fn: func(tb testing.TB) { got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n" want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n" test.Diff(tb, got, want, test.Context("config file drifted from checked-in copy")) }, wantFail: true, }, { name: "Option errors/Title empty", fn: func(tb testing.TB) { test.Equal(tb, 1, 1, test.Title("")) }, wantFail: true, }, { name: "Option errors/Context empty", fn: func(tb testing.TB) { test.Equal(tb, 1, 1, test.Context("")) }, wantFail: true, }, { name: "Option errors/Context format resolves empty", fn: func(tb testing.TB) { test.Equal(tb, 1, 1, test.Context("%s", "")) }, wantFail: true, }, { name: "Option errors/FloatEqualityThreshold positive infinity", fn: func(tb testing.TB) { test.NearlyEqual(tb, 1.0, 1.0, test.FloatEqualityThreshold(math.Inf(1))) }, wantFail: true, }, { name: "Option errors/FloatEqualityThreshold negative infinity", fn: func(tb testing.TB) { test.NearlyEqual(tb, 1.0, 1.0, test.FloatEqualityThreshold(math.Inf(-1))) }, wantFail: true, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { buf := &bytes.Buffer{} tb := &TB{out: buf} snap := snapshot.New(t, snapshot.Update(*update), snapshot.Clean(*clean), snapshot.Color(false))
if tb.failed { t.Fatalf("%s initial failed state should be false", tt.name) }
// Call the test function, passing in the mock TB that just records // what a "real" TB would have done tt.fn(tb)
if tb.failed != tt.wantFail { t.Fatalf("\nIncorrect Failure\n\ntb.failed:\t%v\nwanted:\t%v\n", tb.failed, tt.wantFail) }
// Test the output matches our snapshot file, only for failed tests // as there should be no output for passed tests if !tb.failed { if buf.Len() != 0 { t.Fatalf("\nIncorrect Output\n\nA passed test should have no output, got: %s\n", buf.String()) } } else { snap.Snap(buf.String()) } }) }}
func TestCapture(t *testing.T) { t.Run("happy", func(t *testing.T) { // Some fake user function that writes to stdout and stderr fn := func() error { fmt.Fprintln(os.Stdout, "hello stdout") fmt.Fprintln(os.Stderr, "hello stderr")
return nil }
stdout, stderr := test.CaptureOutput(t, fn)
test.Equal(t, stdout, "hello stdout\n") test.Equal(t, stderr, "hello stderr\n") })
t.Run("sad", func(t *testing.T) { // This time the user function returns an error fn := func() error { return errors.New("it broke") }
buf := &bytes.Buffer{} testTB := &TB{out: buf}
stdout, stderr := test.CaptureOutput(testTB, fn)
// Test should have failed test.True(t, testTB.failed)
// stdout and stderr should be empty test.Equal(t, stdout, "") test.Equal(t, stderr, "") })}
// inputError is a concrete error type used to exercise test.ErrorAs.type inputError struct{ msg string }
func (e *inputError) Error() string { return e.msg }
// outputError is an unrelated concrete error type used to exercise the// "wrong type" failure path of test.ErrorAs.type outputError struct{ msg string }
func (e *outputError) Error() string { return e.msg }