Skip to content

Commit

Permalink
up: test - add new func ErrIs for check two err is equals
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 25, 2022
1 parent fe47518 commit f43767b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions testutil/assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func New(t TestingT) *Assertions {
}

// IsOk for last check
func (as Assertions) IsOk() bool {
func (as *Assertions) IsOk() bool {
return as.ok
}

// IsFail for last check
func (as Assertions) IsFail() bool {
func (as *Assertions) IsFail() bool {
return !as.ok
}
23 changes: 23 additions & 0 deletions testutil/assert/assertions_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,28 @@ func (as *Assertions) PanicsErrMsg(fn PanicRunFunc, errMsg string, fmtAndArgs ..
return as
}

// Contains asserts that the given data(string,slice,map) should contain element
func (as *Assertions) Contains(src, elem any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Contains(as.t, src, elem, fmtAndArgs...)
return as
}

// NotContains asserts that the given data(string,slice,map) should not contain element
func (as *Assertions) NotContains(src, elem any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = NotContains(as.t, src, elem, fmtAndArgs...)
return as
}

// ContainsKey asserts that the given map is contains key
func (as *Assertions) ContainsKey(mp, key any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = ContainsKey(as.t, mp, key, fmtAndArgs...)
return as
}

// StrContains asserts that the given strings is contains sub-string
func (as *Assertions) StrContains(s, sub string, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = StrContains(as.t, s, sub, fmtAndArgs...)
Expand All @@ -99,54 +103,71 @@ func (as *Assertions) Err(err error, fmtAndArgs ...any) *Assertions {
return as
}

// ErrIs asserts that the given error is equals wantErr
func (as *Assertions) ErrIs(err, wantErr error, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = ErrIs(as.t, err, wantErr, fmtAndArgs...)
return as
}

// ErrMsg asserts that the given is a not nil error and error message equals wantMsg
func (as *Assertions) ErrMsg(err error, errMsg string, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = ErrMsg(as.t, err, errMsg, fmtAndArgs...)
return as
}

// ErrSubMsg asserts that the given is a not nil error and the error message contains subMsg
func (as *Assertions) ErrSubMsg(err error, subMsg string, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = ErrSubMsg(as.t, err, subMsg, fmtAndArgs...)
return as
}

// Len assert given length is equals to wantLn
func (as *Assertions) Len(give any, wantLn int, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Len(as.t, give, wantLn, fmtAndArgs...)
return as
}

// LenGt assert given length is greater than to minLn
func (as *Assertions) LenGt(give any, minLn int, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = LenGt(as.t, give, minLn, fmtAndArgs...)
return as
}

// Eq asserts that the want should equal to the given
func (as *Assertions) Eq(want, give any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Eq(as.t, want, give, fmtAndArgs...)
return as
}

// Neq asserts that the want should not be equal to the given.
// alias of NotEq()
func (as *Assertions) Neq(want, give any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Neq(as.t, want, give, fmtAndArgs...)
return as
}

// NotEq asserts that the want should not be equal to the given
func (as *Assertions) NotEq(want, give any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = NotEq(as.t, want, give, fmtAndArgs...)
return as
}

// Lt asserts that the give(intX) should not be less than max
func (as *Assertions) Lt(give, max int, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Lt(as.t, give, max, fmtAndArgs...)
return as
}

// Gt asserts that the give(intX) should not be greater than max
func (as *Assertions) Gt(give, min int, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Gt(as.t, give, min, fmtAndArgs...)
Expand All @@ -160,12 +181,14 @@ func (as *Assertions) IsType(wantType, give any, fmtAndArgs ...any) *Assertions
return as
}

// Fail reports a failure through
func (as *Assertions) Fail(failMsg string, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Fail(as.t, failMsg, fmtAndArgs...)
return as
}

// FailNow fails test
func (as *Assertions) FailNow(failMsg string, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = FailNow(as.t, failMsg, fmtAndArgs...)
Expand Down
20 changes: 20 additions & 0 deletions testutil/assert/asserts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assert

import (
"errors"
"fmt"
"reflect"
"runtime/debug"
Expand Down Expand Up @@ -298,6 +299,21 @@ func Err(t TestingT, err error, fmtAndArgs ...any) bool {
return true
}

// ErrIs asserts that the given error is equals wantErr
func ErrIs(t TestingT, err, wantErr error, fmtAndArgs ...any) bool {
if err == nil {
t.Helper()
return fail(t, "An error is expected but got nil.", fmtAndArgs)
}

if !errors.Is(err, wantErr) {
t.Helper()
return fail(t, fmt.Sprintf("Expect given err is equals %#v.", wantErr), fmtAndArgs)
}

return true
}

// ErrMsg asserts that the given is a not nil error and error message equals wantMsg
func ErrMsg(t TestingT, err error, wantMsg string, fmtAndArgs ...any) bool {
if err == nil {
Expand Down Expand Up @@ -338,6 +354,7 @@ func ErrSubMsg(t TestingT, err error, subMsg string, fmtAndArgs ...any) bool {
// -------------------- Len --------------------
//

// Len assert given length is equals to wantLn
func Len(t TestingT, give any, wantLn int, fmtAndArgs ...any) bool {
gln := reflects.Len(reflect.ValueOf(give))
if gln < 0 {
Expand All @@ -352,6 +369,7 @@ func Len(t TestingT, give any, wantLn int, fmtAndArgs ...any) bool {
return false
}

// LenGt assert given length is greater than to minLn
func LenGt(t TestingT, give any, minLn int, fmtAndArgs ...any) bool {
gln := reflects.Len(reflect.ValueOf(give))
if gln < 0 {
Expand Down Expand Up @@ -416,6 +434,7 @@ func NotEq(t TestingT, want, give any, fmtAndArgs ...any) bool {
return true
}

// Lt asserts that the give(intX) should not be less than max
func Lt(t TestingT, give, max int, fmtAndArgs ...any) bool {
gInt, err := mathutil.ToInt(give)
if err == nil && gInt <= max {
Expand All @@ -426,6 +445,7 @@ func Lt(t TestingT, give, max int, fmtAndArgs ...any) bool {
return fail(t, fmt.Sprintf("Given should later than or equal %d(but was %d)", max, gInt), fmtAndArgs)
}

// Gt asserts that the give(intX) should not be greater than max
func Gt(t TestingT, give, min int, fmtAndArgs ...any) bool {
gInt, err := mathutil.ToInt(give)
if err == nil && gInt >= min {
Expand Down

0 comments on commit f43767b

Please sign in to comment.