Skip to content

Commit

Permalink
feature: is.Error(v, nil)
Browse files Browse the repository at this point in the history
  • Loading branch information
halimath committed Dec 18, 2023
1 parent b599f40 commit 499422f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ Expectation | Type constraints | Description
`is.StringHavingSuffix` | `string` | Expects the given value to be a string having a given suffix
`is.EqualToStringByLines` | `string` | Similar to EqualTo used on two strings but reports differences on a line-by-line basis

### A note on error testing

`expect` provides two expectations targeting `error` specificially: `is.Error` and `is.NoError`. The later one
is straight forward and expects the given value to be `nil`. `is.Error` works by applying the standard library
function `errors.Is` and expects the given error to contain the target error as part of its error wrapping
chain. In addition, `is.Error` also supports the target error to be `nil`. In this case, `is.Error(v, nil)`
behaves identical to `is.Error(v)`. This allows an easy and convenient way of writing table based tests that
expect both error and non-error conditions.

### Deep equality

The `is.DeepEqualTo` expectation is special as compared to the other ones. It uses a recursive algorithm to
Expand Down
9 changes: 7 additions & 2 deletions cmd/expect-migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,16 @@ func (v *expectationRewriteVisitor) Visit(node ast.Node) ast.Visitor {
}
}

if _, ok := node.(*ast.ExprStmt); ok {
exprStmt, ok := node.(*ast.ExprStmt)
if !ok {
if currentBlock != nil {
currentBlock.lastExpectation = nil
}

return v
}

call, ok := node.(*ast.CallExpr)
call, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
if currentBlock != nil {
currentBlock.lastExpectation = nil
Expand Down
6 changes: 6 additions & 0 deletions is/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import (

// Error matches got to contain target in its chain. The check is performed
// using errors.Is.
// A special case happens when target is nil. In this case, Error behaves identical to NoError. This improves
// testing convenience when writing table based tests that test on both error and non error conditions.
func Error(got, target error) expect.Expectation {
if target == nil {
return NoError(got)
}

return expect.ExpectFunc(func(t expect.TB) {
t.Helper()

Expand Down
1 change: 1 addition & 0 deletions is/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestError(t *testing.T) {

err := errors.New("failed")

Error(nil, nil).Expect(&tm)
Error(nil, err).Expect(&tm)
Error(errors.New("other"), err).Expect(&tm)
Error(err, err).Expect(&tm)
Expand Down

0 comments on commit 499422f

Please sign in to comment.