Skip to content

Commit

Permalink
👔 up(stdio,assert): add more util func and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 8, 2023
1 parent 531fa6b commit 000ca90
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
6 changes: 6 additions & 0 deletions stdio/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ func WriteBytes(bs []byte) {
func WriteString(s string) {
_, _ = os.Stdout.WriteString(s)
}

// Writeln to stdout
func Writeln(s string) {
_, _ = os.Stdout.WriteString(s)
_, _ = os.Stdout.Write([]byte("\n"))
}
6 changes: 6 additions & 0 deletions stdio/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ func TestNewIOReader(t *testing.T) {
r = stdio.NewIOReader(strings.NewReader("hi"))
assert.Eq(t, "hi", stdio.ReadString(r))
}

func TestWriteBytes(t *testing.T) {
stdio.WriteBytes([]byte("hi,"))
stdio.WriteString("inhere.")
stdio.Writeln("welcome")
}
41 changes: 40 additions & 1 deletion testutil/assert/assertions_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ func (as *Assertions) Err(err error, fmtAndArgs ...any) *Assertions {
return as
}

// Error asserts that the given is a not nil error
func (as *Assertions) Error(err error, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Err(as.t, err, fmtAndArgs...)
return as
}

// ErrIs asserts that the given error is equals wantErr
func (as *Assertions) ErrIs(err, wantErr error, fmtAndArgs ...any) *Assertions {
as.t.Helper()
Expand Down Expand Up @@ -153,6 +160,15 @@ func (as *Assertions) Eq(want, give any, fmtAndArgs ...any) *Assertions {
return as
}

// Equal asserts that the want should equal to the given
//
// Alias of Eq()
func (as *Assertions) Equal(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 {
Expand All @@ -168,20 +184,43 @@ func (as *Assertions) NotEq(want, give any, fmtAndArgs ...any) *Assertions {
return as
}

// NotEqual asserts that the want should not be equal to the given
//
// Alias of NotEq()
func (as *Assertions) NotEqual(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
// Lte asserts that the give(intX) should not be less than or equal to max
func (as *Assertions) Lte(give, max int, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Lte(as.t, give, max, fmtAndArgs...)
return as
}

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

// Gte asserts that the give(intX) should not be greater than or equal to min
func (as *Assertions) Gte(give, min int, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Gte(as.t, give, min, fmtAndArgs...)
return as
}

// IsType type equals assert
func (as *Assertions) IsType(wantType, give any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
Expand Down
25 changes: 24 additions & 1 deletion testutil/assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,33 @@ func TestAssertions_Chain(t *testing.T) {
err := errors.New("error message")

as := assert.New(t).
NotEmpty(err).
NotNil(err).
Err(err).
ErrMsg(err, "error message")
ErrMsg(err, "error message").
Eq("error message", err.Error()).
Neq("message", err.Error()).
Equal("error message", err.Error()).
Contains(err.Error(), "message").
StrContains(err.Error(), "message").
NotContains(err.Error(), "success").
Gt(4, 3).
Lt(2, 3)

assert.True(t, as.IsOk())
assert.False(t, as.IsFail())

iv := 23
as = assert.New(t).
IsType(1, iv).
NotEq(22, iv).
NotEqual(22, iv).
Lte(iv, 23).
Gte(iv, 23).
Empty(0).
True(true).
False(false).
Nil(nil)

assert.True(t, as.IsOk())
}
18 changes: 15 additions & 3 deletions testutil/assert/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func False(t TestingT, give bool, fmtAndArgs ...any) bool {

// Empty asserts that the give should be empty
func Empty(t TestingT, give any, fmtAndArgs ...any) bool {
empty := stdutil.IsEmpty(give)
empty := isEmpty(give)
if !empty {
t.Helper()
return fail(t, fmt.Sprintf("Should be empty, but was:\n%#v", give), fmtAndArgs)
Expand All @@ -65,7 +65,7 @@ func Empty(t TestingT, give any, fmtAndArgs ...any) bool {

// NotEmpty asserts that the give should not be empty
func NotEmpty(t TestingT, give any, fmtAndArgs ...any) bool {
nEmpty := !stdutil.IsEmpty(give)
nEmpty := !isEmpty(give)
if !nEmpty {
t.Helper()
return fail(t, fmt.Sprintf("Should not be empty, but was:\n%#v", give), fmtAndArgs)
Expand Down Expand Up @@ -474,7 +474,13 @@ 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
// Lte asserts that the give(intX) should not be less than or equals to max
func Lte(t TestingT, give, max int, fmtAndArgs ...any) bool {
t.Helper()
return Lt(t, give, max+1, fmtAndArgs...)
}

// Gt asserts that the give(intX) should not be greater than min
func Gt(t TestingT, give, min int, fmtAndArgs ...any) bool {
gInt, err := mathutil.ToInt(give)
if err == nil && gInt >= min {
Expand All @@ -485,6 +491,12 @@ func Gt(t TestingT, give, min int, fmtAndArgs ...any) bool {
return fail(t, fmt.Sprintf("Given should gater than or equal %d(but was %d)", min, gInt), fmtAndArgs)
}

// Gte asserts that the give(intX) should not be greater than or equals to min
func Gte(t TestingT, give, min int, fmtAndArgs ...any) bool {
t.Helper()
return Gt(t, give, min+1, fmtAndArgs...)
}

// IsType assert data type equals
//
// Usage:
Expand Down
8 changes: 8 additions & 0 deletions testutil/assert/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
"github.com/gookit/goutil/strutil"
)

// isEmpty value check
func isEmpty(v any) bool {
if v == nil {
return true
}
return reflects.IsEmpty(reflect.ValueOf(v))
}

func checkEqualArgs(expected, actual any) error {
if expected == nil && actual == nil {
return nil
Expand Down

0 comments on commit 000ca90

Please sign in to comment.