Skip to content

Commit

Permalink
fix: error msg on assert.NoError (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
katcipis authored Nov 8, 2022
1 parent 8ce6c95 commit fadce9f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package assert_test

import (
"errors"
"fmt"
"math"
"testing"

Expand Down Expand Up @@ -412,3 +414,64 @@ func TestAssertErrMessages(t *testing.T) {
a.Error(nil, "func msg")
})
}

func TestAssertNoErrMessages(t *testing.T) {
err := errors.New("test")
errmsg := fmt.Sprintf("unexpected error[%v]", err)

t.Run("no error: no details", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := errmsg
assert.EqualStrings(t, want, got)
})
a.NoError(err)
})

t.Run("no error: constructor msg and details msg with fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := fmt.Sprintf("%s: func fmt 777: constructor fmt 666", errmsg)
assert.EqualStrings(t, want, got)
}, "constructor fmt %d", 666)
a.NoError(err, "func fmt %d", 777)
})

t.Run("no error: constructor msg and details msg no fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := fmt.Sprintf("%s: func msg: constructor msg", errmsg)
assert.EqualStrings(t, want, got)
}, "constructor msg")
a.NoError(err, "func msg")
})

t.Run("no error: constructor msg with fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := fmt.Sprintf("%s: constructor fmt 666", errmsg)
assert.EqualStrings(t, want, got)
}, "constructor fmt %d", 666)
a.NoError(err)
})

t.Run("no error: constructor msg no fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := fmt.Sprintf("%s: constructor msg", errmsg)
assert.EqualStrings(t, want, got)
}, "constructor msg")
a.NoError(err)
})

t.Run("no error: detail msg with fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := fmt.Sprintf("%s: func fmt 666", errmsg)
assert.EqualStrings(t, want, got)
})
a.NoError(err, "func fmt %d", 666)
})

t.Run("no error: detail msg no fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := fmt.Sprintf("%s: func msg", errmsg)
assert.EqualStrings(t, want, got)
})
a.NoError(err, "func msg")
})
}
2 changes: 1 addition & 1 deletion assert/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func (assert *Assert) NoError(err error, details ...interface{}) {
assert.t.Helper()
if err != nil {
assert.fail(details, "unexpected error[%s]%s", err)
assert.fail(details, "unexpected error[%s]", err)
}
}

Expand Down

0 comments on commit fadce9f

Please sign in to comment.