From ea327d12f023ab51109250d1ee2b768d659f99a5 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 8 Sep 2024 23:51:02 -0600 Subject: [PATCH 1/6] test: added ErrorAs --- README.md | 2 ++ examples_test.go | 11 +++++++++++ internal/assertions/assertions.go | 17 ++++++++++++++++ must/examples_test.go | 11 +++++++++++ must/must.go | 7 +++++++ must/must_test.go | 32 +++++++++++++++++++++++++++++++ test.go | 7 +++++++ test_test.go | 32 +++++++++++++++++++++++++++++++ 8 files changed, 119 insertions(+) diff --git a/README.md b/README.md index 8e020aa..72810e6 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ There are five key packages, - `portal` - utilities for allocating free ports for network listeners in tests ### Changes +:ballot_box_with_check: v1.11.0 adds an ErrorAs helper + :ballot_box_with_check: v1.10.0 adds a `util` package for helpers that return values - Adds ability to create and automatically clean up temporary files diff --git a/examples_test.go b/examples_test.go index 93f368e..32a2d91 100644 --- a/examples_test.go +++ b/examples_test.go @@ -318,6 +318,17 @@ func ExampleErrorIs() { // Output: } +func ExampleErrorAs() { + e1 := errors.New("e1") + e2 := FakeError("foo") + e3 := errors.New("e3") + errorChain := errors.Join(e1, e2, e3) + var target FakeError + ErrorAs(t, errorChain, &target) + fmt.Println(target.Error()) + // Output: foo +} + func ExampleFalse() { False(t, 1 == int('a')) // Output: diff --git a/internal/assertions/assertions.go b/internal/assertions/assertions.go index 2df2bd9..e6c5f8a 100644 --- a/internal/assertions/assertions.go +++ b/internal/assertions/assertions.go @@ -162,6 +162,23 @@ func ErrorIs(err error, target error) (s string) { return } +func ErrorAs[E error, Target *E](err error, target Target) (s string) { + if err == nil { + s = "expected error; got nil\n" + return + } + if target == nil { + s = "expected target not to be nil" + return + } + if !errors.As(err, target) { + s = "expected errors.As match\n" + s += bullet(" error: %v\n", err) + s += bullet("target: %v\n", target) + } + return +} + func NoError(err error) (s string) { if err != nil { s = "expected nil error\n" diff --git a/must/examples_test.go b/must/examples_test.go index f58ae32..b23238c 100644 --- a/must/examples_test.go +++ b/must/examples_test.go @@ -320,6 +320,17 @@ func ExampleErrorIs() { // Output: } +func ExampleErrorAs() { + e1 := errors.New("e1") + e2 := FakeError("foo") + e3 := errors.New("e3") + errorChain := errors.Join(e1, e2, e3) + var target FakeError + ErrorAs(t, errorChain, &target) + fmt.Println(target.Error()) + // Output: foo +} + func ExampleFalse() { False(t, 1 == int('a')) // Output: diff --git a/must/must.go b/must/must.go index 6473d01..62fd7e3 100644 --- a/must/must.go +++ b/must/must.go @@ -69,6 +69,13 @@ func ErrorIs(t T, err error, target error, settings ...Setting) { invoke(t, assertions.ErrorIs(err, target), settings...) } +// ErrorAs asserts err's tree contains an error that matches target. +// If so, it sets target to the error value. +func ErrorAs[E error, Target *E](t T, err error, target Target, settings ...Setting) { + t.Helper() + invoke(t, assertions.ErrorAs(err, target), settings...) +} + // NoError asserts err is a nil error. func NoError(t T, err error, settings ...Setting) { t.Helper() diff --git a/must/must_test.go b/must/must_test.go index db112fb..56aabb0 100644 --- a/must/must_test.go +++ b/must/must_test.go @@ -165,6 +165,38 @@ func TestErrorIs_nil(t *testing.T) { ErrorIs(tc, nil, err) } +type FakeError string + +func (e FakeError) Error() string { + return string(e) +} + +func TestErrorAs(t *testing.T) { + tc := newCase(t, `expected errors.As match`) + t.Cleanup(tc.assert) + + var target FakeError + e := errors.New("foo") + ErrorAs(tc, e, &target) +} + +func TestErrorAs_nilErr(t *testing.T) { + tc := newCase(t, `expected error; got nil`) + t.Cleanup(tc.assert) + + var target FakeError + ErrorAs(tc, nil, &target) +} + +func TestErrorAs_nilTarget(t *testing.T) { + tc := newCase(t, `expected target not to be nil`) + t.Cleanup(tc.assert) + + var target *FakeError + e := errors.New("foo") + ErrorAs(tc, e, target) +} + func TestNoError(t *testing.T) { tc := newCase(t, `expected nil error`) t.Cleanup(tc.assert) diff --git a/test.go b/test.go index bce3e52..33c0dec 100644 --- a/test.go +++ b/test.go @@ -67,6 +67,13 @@ func ErrorIs(t T, err error, target error, settings ...Setting) { invoke(t, assertions.ErrorIs(err, target), settings...) } +// ErrorAs asserts err's tree contains an error that matches target. +// If so, it sets target to the error value. +func ErrorAs[E error, Target *E](t T, err error, target Target, settings ...Setting) { + t.Helper() + invoke(t, assertions.ErrorAs(err, target), settings...) +} + // NoError asserts err is a nil error. func NoError(t T, err error, settings ...Setting) { t.Helper() diff --git a/test_test.go b/test_test.go index f784a42..0f76dcd 100644 --- a/test_test.go +++ b/test_test.go @@ -163,6 +163,38 @@ func TestErrorIs_nil(t *testing.T) { ErrorIs(tc, nil, err) } +type FakeError string + +func (e FakeError) Error() string { + return string(e) +} + +func TestErrorAs(t *testing.T) { + tc := newCase(t, `expected errors.As match`) + t.Cleanup(tc.assert) + + var target FakeError + e := errors.New("foo") + ErrorAs(tc, e, &target) +} + +func TestErrorAs_nilErr(t *testing.T) { + tc := newCase(t, `expected error; got nil`) + t.Cleanup(tc.assert) + + var target FakeError + ErrorAs(tc, nil, &target) +} + +func TestErrorAs_nilTarget(t *testing.T) { + tc := newCase(t, `expected target not to be nil`) + t.Cleanup(tc.assert) + + var target *FakeError + e := errors.New("foo") + ErrorAs(tc, e, target) +} + func TestNoError(t *testing.T) { tc := newCase(t, `expected nil error`) t.Cleanup(tc.assert) From 78901efc0c3a42dff2084930adc960062eecf8ab Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 00:09:43 -0600 Subject: [PATCH 2/6] examples: use fstest.MapFS for FS examples --- README.md | 2 ++ examples_test.go | 32 +++++++++++++++++++++++--------- must/examples_test.go | 32 +++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 72810e6..f581155 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ There are five key packages, ### Changes :ballot_box_with_check: v1.11.0 adds an ErrorAs helper + - FS examples are more reliable + :ballot_box_with_check: v1.10.0 adds a `util` package for helpers that return values - Adds ability to create and automatically clean up temporary files diff --git a/examples_test.go b/examples_test.go index 32a2d91..bf873e9 100644 --- a/examples_test.go +++ b/examples_test.go @@ -14,6 +14,7 @@ import ( "regexp" "strconv" "strings" + "testing/fstest" "time" "github.com/shoenig/test/wait" @@ -231,7 +232,10 @@ func ExampleDirExists() { } func ExampleDirExistsFS() { - DirExistsFS(t, os.DirFS("/"), "tmp") + fsys := fstest.MapFS{ + "foo": &fstest.MapFile{Mode: fs.ModeDir}, + } + DirExistsFS(t, fsys, "foo") // Output: } @@ -241,7 +245,8 @@ func ExampleDirNotExists() { } func ExampleDirNotExistsFS() { - DirNotExistsFS(t, os.DirFS("/"), "does/not/exist") + fsys := fstest.MapFS{} + DirNotExistsFS(t, fsys, "does/not/exist") // Output: } @@ -341,8 +346,12 @@ func ExampleFileContains() { } func ExampleFileContainsFS() { - _ = os.WriteFile("/tmp/example", []byte("foo bar baz"), fs.FileMode(0600)) - FileContainsFS(t, os.DirFS("/tmp"), "example", "bar") + fsys := fstest.MapFS{ + "example": &fstest.MapFile{ + Data: []byte("foo bar baz"), + }, + } + FileContainsFS(t, fsys, "example", "bar") // Output: } @@ -353,8 +362,10 @@ func ExampleFileExists() { } func ExampleFileExistsFS() { - _ = os.WriteFile("/tmp/example", []byte{}, fs.FileMode(0600)) - FileExistsFS(t, os.DirFS("/tmp"), "example") + fsys := fstest.MapFS{ + "example": &fstest.MapFile{}, + } + FileExistsFS(t, fsys, "example") // Output: } @@ -365,8 +376,10 @@ func ExampleFileMode() { } func ExampleFileModeFS() { - _ = os.WriteFile("/tmp/example_fm", []byte{}, fs.FileMode(0600)) - FileModeFS(t, os.DirFS("/tmp"), "example_fm", fs.FileMode(0600)) + fsys := fstest.MapFS{ + "example": &fstest.MapFile{Mode: 0600}, + } + FileModeFS(t, fsys, "example", fs.FileMode(0600)) // Output: } @@ -376,7 +389,8 @@ func ExampleFileNotExists() { } func ExampleFileNotExistsFS() { - FileNotExistsFS(t, os.DirFS("/tmp"), "not_existing_file") + fsys := fstest.MapFS{} + FileNotExistsFS(t, fsys, "not_existing_file") // Output: } diff --git a/must/examples_test.go b/must/examples_test.go index b23238c..ad7bc55 100644 --- a/must/examples_test.go +++ b/must/examples_test.go @@ -16,6 +16,7 @@ import ( "regexp" "strconv" "strings" + "testing/fstest" "time" "github.com/shoenig/test/wait" @@ -233,7 +234,10 @@ func ExampleDirExists() { } func ExampleDirExistsFS() { - DirExistsFS(t, os.DirFS("/"), "tmp") + fsys := fstest.MapFS{ + "foo": &fstest.MapFile{Mode: fs.ModeDir}, + } + DirExistsFS(t, fsys, "foo") // Output: } @@ -243,7 +247,8 @@ func ExampleDirNotExists() { } func ExampleDirNotExistsFS() { - DirNotExistsFS(t, os.DirFS("/"), "does/not/exist") + fsys := fstest.MapFS{} + DirNotExistsFS(t, fsys, "does/not/exist") // Output: } @@ -343,8 +348,12 @@ func ExampleFileContains() { } func ExampleFileContainsFS() { - _ = os.WriteFile("/tmp/example", []byte("foo bar baz"), fs.FileMode(0600)) - FileContainsFS(t, os.DirFS("/tmp"), "example", "bar") + fsys := fstest.MapFS{ + "example": &fstest.MapFile{ + Data: []byte("foo bar baz"), + }, + } + FileContainsFS(t, fsys, "example", "bar") // Output: } @@ -355,8 +364,10 @@ func ExampleFileExists() { } func ExampleFileExistsFS() { - _ = os.WriteFile("/tmp/example", []byte{}, fs.FileMode(0600)) - FileExistsFS(t, os.DirFS("/tmp"), "example") + fsys := fstest.MapFS{ + "example": &fstest.MapFile{}, + } + FileExistsFS(t, fsys, "example") // Output: } @@ -367,8 +378,10 @@ func ExampleFileMode() { } func ExampleFileModeFS() { - _ = os.WriteFile("/tmp/example_fm", []byte{}, fs.FileMode(0600)) - FileModeFS(t, os.DirFS("/tmp"), "example_fm", fs.FileMode(0600)) + fsys := fstest.MapFS{ + "example": &fstest.MapFile{Mode: 0600}, + } + FileModeFS(t, fsys, "example", fs.FileMode(0600)) // Output: } @@ -378,7 +391,8 @@ func ExampleFileNotExists() { } func ExampleFileNotExistsFS() { - FileNotExistsFS(t, os.DirFS("/tmp"), "not_existing_file") + fsys := fstest.MapFS{} + FileNotExistsFS(t, fsys, "not_existing_file") // Output: } From 9f8e98c8fdce16a000931849dda7a4cddc2933a0 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 00:21:53 -0600 Subject: [PATCH 3/6] examples: only require unix for file and dir examples --- README.md | 1 + examples_test.go | 36 ----------------------------- examples_unix_test.go | 44 ++++++++++++++++++++++++++++++++++++ must/examples_test.go | 36 ----------------------------- must/examples_unix_test.go | 46 ++++++++++++++++++++++++++++++++++++++ scripts/generate.sh | 1 + 6 files changed, 92 insertions(+), 72 deletions(-) create mode 100644 examples_unix_test.go create mode 100644 must/examples_unix_test.go diff --git a/README.md b/README.md index f581155..1afeba3 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ There are five key packages, :ballot_box_with_check: v1.11.0 adds an ErrorAs helper - FS examples are more reliable + - Examples run on non-Unix OS when possible :ballot_box_with_check: v1.10.0 adds a `util` package for helpers that return values diff --git a/examples_test.go b/examples_test.go index bf873e9..d6054ac 100644 --- a/examples_test.go +++ b/examples_test.go @@ -1,8 +1,6 @@ // Copyright (c) The Test Authors // SPDX-License-Identifier: MPL-2.0 -//go:build unix - package test import ( @@ -10,7 +8,6 @@ import ( "fmt" "io/fs" "math" - "os" "regexp" "strconv" "strings" @@ -226,11 +223,6 @@ func ExampleDescendingLess() { // Output: } -func ExampleDirExists() { - DirExists(t, "/tmp") - // Output: -} - func ExampleDirExistsFS() { fsys := fstest.MapFS{ "foo": &fstest.MapFile{Mode: fs.ModeDir}, @@ -239,11 +231,6 @@ func ExampleDirExistsFS() { // Output: } -func ExampleDirNotExists() { - DirNotExists(t, "/does/not/exist") - // Output: -} - func ExampleDirNotExistsFS() { fsys := fstest.MapFS{} DirNotExistsFS(t, fsys, "does/not/exist") @@ -339,12 +326,6 @@ func ExampleFalse() { // Output: } -func ExampleFileContains() { - _ = os.WriteFile("/tmp/example", []byte("foo bar baz"), fs.FileMode(0600)) - FileContains(t, "/tmp/example", "bar") - // Output: -} - func ExampleFileContainsFS() { fsys := fstest.MapFS{ "example": &fstest.MapFile{ @@ -355,12 +336,6 @@ func ExampleFileContainsFS() { // Output: } -func ExampleFileExists() { - _ = os.WriteFile("/tmp/example", []byte{}, fs.FileMode(0600)) - FileExists(t, "/tmp/example") - // Output: -} - func ExampleFileExistsFS() { fsys := fstest.MapFS{ "example": &fstest.MapFile{}, @@ -369,12 +344,6 @@ func ExampleFileExistsFS() { // Output: } -func ExampleFileMode() { - _ = os.WriteFile("/tmp/example_fm", []byte{}, fs.FileMode(0600)) - FileMode(t, "/tmp/example_fm", fs.FileMode(0600)) - // Output: -} - func ExampleFileModeFS() { fsys := fstest.MapFS{ "example": &fstest.MapFile{Mode: 0600}, @@ -383,11 +352,6 @@ func ExampleFileModeFS() { // Output: } -func ExampleFileNotExists() { - FileNotExists(t, "/tmp/not_existing_file") - // Output: -} - func ExampleFileNotExistsFS() { fsys := fstest.MapFS{} FileNotExistsFS(t, fsys, "not_existing_file") diff --git a/examples_unix_test.go b/examples_unix_test.go new file mode 100644 index 0000000..5addf47 --- /dev/null +++ b/examples_unix_test.go @@ -0,0 +1,44 @@ +// Copyright (c) The Test Authors +// SPDX-License-Identifier: MPL-2.0 + +//go:build unix + +package test + +import ( + "io/fs" + "os" +) + +func ExampleDirExists() { + DirExists(t, "/tmp") + // Output: +} + +func ExampleDirNotExists() { + DirNotExists(t, "/does/not/exist") + // Output: +} + +func ExampleFileContains() { + _ = os.WriteFile("/tmp/example", []byte("foo bar baz"), fs.FileMode(0600)) + FileContains(t, "/tmp/example", "bar") + // Output: +} + +func ExampleFileExists() { + _ = os.WriteFile("/tmp/example", []byte{}, fs.FileMode(0600)) + FileExists(t, "/tmp/example") + // Output: +} + +func ExampleFileMode() { + _ = os.WriteFile("/tmp/example_fm", []byte{}, fs.FileMode(0600)) + FileMode(t, "/tmp/example_fm", fs.FileMode(0600)) + // Output: +} + +func ExampleFileNotExists() { + FileNotExists(t, "/tmp/not_existing_file") + // Output: +} diff --git a/must/examples_test.go b/must/examples_test.go index ad7bc55..8719d3e 100644 --- a/must/examples_test.go +++ b/must/examples_test.go @@ -3,8 +3,6 @@ // Copyright (c) The Test Authors // SPDX-License-Identifier: MPL-2.0 -//go:build unix - package must import ( @@ -12,7 +10,6 @@ import ( "fmt" "io/fs" "math" - "os" "regexp" "strconv" "strings" @@ -228,11 +225,6 @@ func ExampleDescendingLess() { // Output: } -func ExampleDirExists() { - DirExists(t, "/tmp") - // Output: -} - func ExampleDirExistsFS() { fsys := fstest.MapFS{ "foo": &fstest.MapFile{Mode: fs.ModeDir}, @@ -241,11 +233,6 @@ func ExampleDirExistsFS() { // Output: } -func ExampleDirNotExists() { - DirNotExists(t, "/does/not/exist") - // Output: -} - func ExampleDirNotExistsFS() { fsys := fstest.MapFS{} DirNotExistsFS(t, fsys, "does/not/exist") @@ -341,12 +328,6 @@ func ExampleFalse() { // Output: } -func ExampleFileContains() { - _ = os.WriteFile("/tmp/example", []byte("foo bar baz"), fs.FileMode(0600)) - FileContains(t, "/tmp/example", "bar") - // Output: -} - func ExampleFileContainsFS() { fsys := fstest.MapFS{ "example": &fstest.MapFile{ @@ -357,12 +338,6 @@ func ExampleFileContainsFS() { // Output: } -func ExampleFileExists() { - _ = os.WriteFile("/tmp/example", []byte{}, fs.FileMode(0600)) - FileExists(t, "/tmp/example") - // Output: -} - func ExampleFileExistsFS() { fsys := fstest.MapFS{ "example": &fstest.MapFile{}, @@ -371,12 +346,6 @@ func ExampleFileExistsFS() { // Output: } -func ExampleFileMode() { - _ = os.WriteFile("/tmp/example_fm", []byte{}, fs.FileMode(0600)) - FileMode(t, "/tmp/example_fm", fs.FileMode(0600)) - // Output: -} - func ExampleFileModeFS() { fsys := fstest.MapFS{ "example": &fstest.MapFile{Mode: 0600}, @@ -385,11 +354,6 @@ func ExampleFileModeFS() { // Output: } -func ExampleFileNotExists() { - FileNotExists(t, "/tmp/not_existing_file") - // Output: -} - func ExampleFileNotExistsFS() { fsys := fstest.MapFS{} FileNotExistsFS(t, fsys, "not_existing_file") diff --git a/must/examples_unix_test.go b/must/examples_unix_test.go new file mode 100644 index 0000000..39c1ef4 --- /dev/null +++ b/must/examples_unix_test.go @@ -0,0 +1,46 @@ +// Code generated via scripts/generate.sh. DO NOT EDIT. + +// Copyright (c) The Test Authors +// SPDX-License-Identifier: MPL-2.0 + +//go:build unix + +package must + +import ( + "io/fs" + "os" +) + +func ExampleDirExists() { + DirExists(t, "/tmp") + // Output: +} + +func ExampleDirNotExists() { + DirNotExists(t, "/does/not/exist") + // Output: +} + +func ExampleFileContains() { + _ = os.WriteFile("/tmp/example", []byte("foo bar baz"), fs.FileMode(0600)) + FileContains(t, "/tmp/example", "bar") + // Output: +} + +func ExampleFileExists() { + _ = os.WriteFile("/tmp/example", []byte{}, fs.FileMode(0600)) + FileExists(t, "/tmp/example") + // Output: +} + +func ExampleFileMode() { + _ = os.WriteFile("/tmp/example_fm", []byte{}, fs.FileMode(0600)) + FileMode(t, "/tmp/example_fm", fs.FileMode(0600)) + // Output: +} + +func ExampleFileNotExists() { + FileNotExists(t, "/tmp/not_existing_file") + // Output: +} diff --git a/scripts/generate.sh b/scripts/generate.sh index 6a3c440..042c929 100755 --- a/scripts/generate.sh +++ b/scripts/generate.sh @@ -19,6 +19,7 @@ apply scripts_test.go apply test.go apply test_test.go apply examples_test.go +apply examples_unix_test.go cp -R testdata must/ From 517e8a6f9ca3d963b5ed0365b228ae9d53b9001f Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 00:53:14 -0600 Subject: [PATCH 4/6] examples: make compatible with go1.18 to pass lint --- examples_test.go | 4 ++-- must/examples_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples_test.go b/examples_test.go index d6054ac..90166e2 100644 --- a/examples_test.go +++ b/examples_test.go @@ -305,7 +305,7 @@ func ExampleErrorIs() { e1 := errors.New("e1") e2 := errors.New("e2") e3 := errors.New("e3") - errorChain := errors.Join(e1, e2, e3) + errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) ErrorIs(t, errorChain, e2) // Output: } @@ -314,7 +314,7 @@ func ExampleErrorAs() { e1 := errors.New("e1") e2 := FakeError("foo") e3 := errors.New("e3") - errorChain := errors.Join(e1, e2, e3) + errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) var target FakeError ErrorAs(t, errorChain, &target) fmt.Println(target.Error()) diff --git a/must/examples_test.go b/must/examples_test.go index 8719d3e..db19c44 100644 --- a/must/examples_test.go +++ b/must/examples_test.go @@ -307,7 +307,7 @@ func ExampleErrorIs() { e1 := errors.New("e1") e2 := errors.New("e2") e3 := errors.New("e3") - errorChain := errors.Join(e1, e2, e3) + errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) ErrorIs(t, errorChain, e2) // Output: } @@ -316,7 +316,7 @@ func ExampleErrorAs() { e1 := errors.New("e1") e2 := FakeError("foo") e3 := errors.New("e3") - errorChain := errors.Join(e1, e2, e3) + errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) var target FakeError ErrorAs(t, errorChain, &target) fmt.Println(target.Error()) From 391017f85cd23b453d0a1d2c3d30b31d3ba94ebf Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 01:04:58 -0600 Subject: [PATCH 5/6] examples: replace errors.Join with fmt.Errorf for go1.18 compatibility --- examples_test.go | 18 ++++++++---------- must/examples_test.go | 18 ++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/examples_test.go b/examples_test.go index 90166e2..1b316de 100644 --- a/examples_test.go +++ b/examples_test.go @@ -303,22 +303,20 @@ func ExampleErrorContains() { func ExampleErrorIs() { e1 := errors.New("e1") - e2 := errors.New("e2") - e3 := errors.New("e3") - errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) - ErrorIs(t, errorChain, e2) + e2 := fmt.Errorf("e2: %w", e1) + e3 := fmt.Errorf("e3: %w", e2) + ErrorIs(t, e3, e1) // Output: } func ExampleErrorAs() { - e1 := errors.New("e1") - e2 := FakeError("foo") - e3 := errors.New("e3") - errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) + e1 := FakeError("e1") + e2 := fmt.Errorf("e2: %w", e1) + e3 := fmt.Errorf("e3: %w", e2) var target FakeError - ErrorAs(t, errorChain, &target) + ErrorAs(t, e3, &target) fmt.Println(target.Error()) - // Output: foo + // Output: e1 } func ExampleFalse() { diff --git a/must/examples_test.go b/must/examples_test.go index db19c44..2bc4b61 100644 --- a/must/examples_test.go +++ b/must/examples_test.go @@ -305,22 +305,20 @@ func ExampleErrorContains() { func ExampleErrorIs() { e1 := errors.New("e1") - e2 := errors.New("e2") - e3 := errors.New("e3") - errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) - ErrorIs(t, errorChain, e2) + e2 := fmt.Errorf("e2: %w", e1) + e3 := fmt.Errorf("e3: %w", e2) + ErrorIs(t, e3, e1) // Output: } func ExampleErrorAs() { - e1 := errors.New("e1") - e2 := FakeError("foo") - e3 := errors.New("e3") - errorChain := fmt.Errorf("%w%w%w", e1, e2, e3) + e1 := FakeError("e1") + e2 := fmt.Errorf("e2: %w", e1) + e3 := fmt.Errorf("e3: %w", e2) var target FakeError - ErrorAs(t, errorChain, &target) + ErrorAs(t, e3, &target) fmt.Println(target.Error()) - // Output: foo + // Output: e1 } func ExampleFalse() { From 5eb07bbe804ed55499b9475cd93ee51a7e2f3914 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 09:02:25 -0600 Subject: [PATCH 6/6] assertions: make Error helper output more consistent --- internal/assertions/assertions.go | 16 ++++++++-------- must/must_test.go | 10 +++++----- test_test.go | 10 +++++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/internal/assertions/assertions.go b/internal/assertions/assertions.go index e6c5f8a..8adae00 100644 --- a/internal/assertions/assertions.go +++ b/internal/assertions/assertions.go @@ -130,14 +130,14 @@ func Unreachable() (s string) { func Error(err error) (s string) { if err == nil { - s = "expected non-nil error; is nil\n" + s = "expected non-nil error; got nil\n" } return } func EqError(err error, msg string) (s string) { if err == nil { - s = "expected error; got nil\n" + s = "expected non-nil error; got nil\n" return } e := err.Error() @@ -151,30 +151,30 @@ func EqError(err error, msg string) (s string) { func ErrorIs(err error, target error) (s string) { if err == nil { - s = "expected error; got nil\n" + s = "expected non-nil error; got nil\n" return } if !errors.Is(err, target) { s = "expected errors.Is match\n" - s += bullet(" error: %v\n", err) s += bullet("target: %v\n", target) + s += bullet(" got: %v\n", err) } return } func ErrorAs[E error, Target *E](err error, target Target) (s string) { if err == nil { - s = "expected error; got nil\n" + s = "expected non-nil error; got nil\n" return } if target == nil { - s = "expected target not to be nil" + s = "expected non-nil target; got nil\n" return } if !errors.As(err, target) { s = "expected errors.As match\n" - s += bullet(" error: %v\n", err) s += bullet("target: %v\n", target) + s += bullet(" got: %v\n", err) } return } @@ -189,7 +189,7 @@ func NoError(err error) (s string) { func ErrorContains(err error, sub string) (s string) { if err == nil { - s = "expected non-nil error\n" + s = "expected non-nil error; got nil\n" return } actual := err.Error() diff --git a/must/must_test.go b/must/must_test.go index 56aabb0..2fbfdbe 100644 --- a/must/must_test.go +++ b/must/must_test.go @@ -105,7 +105,7 @@ func TestUnreachable_PS(t *testing.T) { } func TestError(t *testing.T) { - tc := newCase(t, `expected non-nil error; is nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) Error(tc, nil) @@ -133,7 +133,7 @@ func TestEqError_PS(t *testing.T) { } func TestEqError_nil(t *testing.T) { - tc := newCase(t, `expected error; got nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) EqError(tc, nil, "blah") @@ -158,7 +158,7 @@ func TestErrorIs_PS(t *testing.T) { } func TestErrorIs_nil(t *testing.T) { - tc := newCase(t, `expected error; got nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) err := errors.New("oops") @@ -181,7 +181,7 @@ func TestErrorAs(t *testing.T) { } func TestErrorAs_nilErr(t *testing.T) { - tc := newCase(t, `expected error; got nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) var target FakeError @@ -189,7 +189,7 @@ func TestErrorAs_nilErr(t *testing.T) { } func TestErrorAs_nilTarget(t *testing.T) { - tc := newCase(t, `expected target not to be nil`) + tc := newCase(t, `expected non-nil target; got nil`) t.Cleanup(tc.assert) var target *FakeError diff --git a/test_test.go b/test_test.go index 0f76dcd..a9b8389 100644 --- a/test_test.go +++ b/test_test.go @@ -103,7 +103,7 @@ func TestUnreachable_PS(t *testing.T) { } func TestError(t *testing.T) { - tc := newCase(t, `expected non-nil error; is nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) Error(tc, nil) @@ -131,7 +131,7 @@ func TestEqError_PS(t *testing.T) { } func TestEqError_nil(t *testing.T) { - tc := newCase(t, `expected error; got nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) EqError(tc, nil, "blah") @@ -156,7 +156,7 @@ func TestErrorIs_PS(t *testing.T) { } func TestErrorIs_nil(t *testing.T) { - tc := newCase(t, `expected error; got nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) err := errors.New("oops") @@ -179,7 +179,7 @@ func TestErrorAs(t *testing.T) { } func TestErrorAs_nilErr(t *testing.T) { - tc := newCase(t, `expected error; got nil`) + tc := newCase(t, `expected non-nil error; got nil`) t.Cleanup(tc.assert) var target FakeError @@ -187,7 +187,7 @@ func TestErrorAs_nilErr(t *testing.T) { } func TestErrorAs_nilTarget(t *testing.T) { - tc := newCase(t, `expected target not to be nil`) + tc := newCase(t, `expected non-nil target; got nil`) t.Cleanup(tc.assert) var target *FakeError