Something went wrong. Try again.
A lightweight test helper package
Something went wrong. Try again.
test test_test.go
11 kB · 498 lines
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499package test_test
import ( "bytes" "errors" "flag" "fmt" "io" "os" "slices" "testing"
"github.com/FollowTheProcess/snapshot" "github.com/FollowTheProcess/test")
var update = flag.Bool("update", false, "Update snapshots")
// 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(a, b []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(a, b []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: "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: "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/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: "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, }, }
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))
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, "") })}