Skip to content

Commit

Permalink
Use stdlib error without OS differences for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz committed Apr 27, 2024
1 parent 8ddef3a commit fe621ff
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions be/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package be_test
import (
"errors"
"fmt"
"io/fs"
"os"
"strconv"
"testing"

"github.com/rliebz/ghost"
Expand Down Expand Up @@ -256,32 +255,32 @@ func TestErrorAs(t *testing.T) {
t.Run("match", func(t *testing.T) {
g := ghost.New(t)

var target *fs.PathError
_, err := os.Open("some-non-existing-file")
var target *strconv.NumError
_, err := strconv.Atoi("bad input")

result := be.ErrorAs(err, &target)
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`error err set as target &target
error: open some-non-existing-file: no such file or directory
target: *fs.PathError`,
error: strconv.Atoi: parsing "bad input": invalid syntax
target: *strconv.NumError`,
))

result = be.ErrorAs(fmt.Errorf("wrapping: %w", err), &target)
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`error fmt.Errorf("wrapping: %w", err) set as target &target
error: wrapping: open some-non-existing-file: no such file or directory
target: *fs.PathError`,
error: wrapping: strconv.Atoi: parsing "bad input": invalid syntax
target: *strconv.NumError`,
))
})

t.Run("no match", func(t *testing.T) {
g := ghost.New(t)

var target *fs.PathError
var target *strconv.NumError
err := errors.New("oh no")

result := be.ErrorAs(err, &target)
Expand All @@ -290,7 +289,7 @@ target: *fs.PathError`,
result.Message,
`error err cannot be set as target &target
error: oh no
target: *fs.PathError`,
target: *strconv.NumError`,
))

result = be.ErrorAs(errors.New("oh no"), &target)
Expand All @@ -299,7 +298,7 @@ target: *fs.PathError`,
result.Message,
`error errors.New("oh no") cannot be set as target &target
error: oh no
target: *fs.PathError`,
target: *strconv.NumError`,
))
})

Expand Down

0 comments on commit fe621ff

Please sign in to comment.