From 000ca9064266197bd0626b6d60464113ca407f49 Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 9 Mar 2023 00:05:39 +0800 Subject: [PATCH] :necktie: up(stdio,assert): add more util func and add more tests --- stdio/stdio.go | 6 ++++ stdio/stdio_test.go | 6 ++++ testutil/assert/assertions_methods.go | 41 ++++++++++++++++++++++++++- testutil/assert/assertions_test.go | 25 +++++++++++++++- testutil/assert/asserts.go | 18 ++++++++++-- testutil/assert/util.go | 8 ++++++ 6 files changed, 99 insertions(+), 5 deletions(-) diff --git a/stdio/stdio.go b/stdio/stdio.go index b7d8f7a6e..62527043d 100644 --- a/stdio/stdio.go +++ b/stdio/stdio.go @@ -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")) +} diff --git a/stdio/stdio_test.go b/stdio/stdio_test.go index a5facbc52..8ce0cb6f3 100644 --- a/stdio/stdio_test.go +++ b/stdio/stdio_test.go @@ -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") +} diff --git a/testutil/assert/assertions_methods.go b/testutil/assert/assertions_methods.go index 91ee53a99..158c95be9 100644 --- a/testutil/assert/assertions_methods.go +++ b/testutil/assert/assertions_methods.go @@ -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() @@ -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 { @@ -168,6 +184,15 @@ 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() @@ -175,13 +200,27 @@ func (as *Assertions) Lt(give, max int, fmtAndArgs ...any) *Assertions { 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() diff --git a/testutil/assert/assertions_test.go b/testutil/assert/assertions_test.go index 4b3e692c1..d7a6c6156 100644 --- a/testutil/assert/assertions_test.go +++ b/testutil/assert/assertions_test.go @@ -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()) } diff --git a/testutil/assert/asserts.go b/testutil/assert/asserts.go index 39d23cf22..753d2c277 100644 --- a/testutil/assert/asserts.go +++ b/testutil/assert/asserts.go @@ -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) @@ -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) @@ -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 { @@ -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: diff --git a/testutil/assert/util.go b/testutil/assert/util.go index fdbaf7fa1..0c35f23c1 100644 --- a/testutil/assert/util.go +++ b/testutil/assert/util.go @@ -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