Skip to content

Commit

Permalink
[type: refactor] assert enhance. (#108)
Browse files Browse the repository at this point in the history
[type:feat] add assert
  • Loading branch information
moremind committed Jul 25, 2022
1 parent 972a1fe commit ff01f62
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
73 changes: 73 additions & 0 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package assert

import (
"fmt"
"path/filepath"
"reflect"
"regexp"
"runtime"
"testing"
)
Expand Down Expand Up @@ -80,6 +82,77 @@ func NotEqual(t *testing.T, exp, got interface{}, args ...interface{}) {
assert(t, result, fn, 1)
}

// Panic asserts that function fn() panics.
// It assumes that recover() either returns a string or
// an error and fails if the message does not match
// the regular expression in 'matches'.
// @param t tet
// @param fn function
// @param matches matcher
func Panic(t *testing.T, fn func(), matches string) {
if x := doesPanic(2, fn, matches); x != "" {
fmt.Println(x)
t.Fail()
}
}

// doesPanic do panic
// @param skip judge skip
// @param fn func
// @param expr got
// @return err error
func doesPanic(skip int, fn func(), expr string) (err string) {
defer func() {
r := recover()
if r == nil {
err = fail(skip, "did not panic")
return
}
var v string
switch r.(type) {
case error:
v = r.(error).Error()
case string:
v = r.(string)
}
err = matches(skip, v, expr)
}()
fn()
return ""
}

// Matches asserts that a value matches a given regular expression.
// @param t test
// @param value value
// @param expr got
func Matches(t *testing.T, value, expr string) {
if x := matches(2, value, expr); x != "" {
fmt.Println(x)
t.Fail()
}
}

// matches matches got and expr value
// @param skip judge skip
// @param value value
// @param expr got
// @return string result
func matches(skip int, value, expr string) string {
ok, err := regexp.MatchString(expr, value)
if err != nil {
return fail(skip, "invalid pattern %q. %s", expr, err)
}
if !ok {
return fail(skip, "got %s which does not match %s", value, expr)
}
return ""
}

func fail(skip int, format string, args ...interface{}) string {
_, file, line, _ := runtime.Caller(skip)
return fmt.Sprintf("\t%s:%d: %s\n", filepath.Base(file), line, fmt.Sprintf(format, args...))
}

// assert assertion
// @param t test
// @param result result
Expand Down
36 changes: 36 additions & 0 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,39 @@ func TestIsTrue(t *testing.T) {
func TestIsFalse(t *testing.T) {
IsFalse(t, reflect.DeepEqual("foo", "bar"), "msg!")
}

func TestPanicPanics(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, ""), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

func TestPanicPanicsAndMatches(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, "foo"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

func TestPanicPanicsAndDoesNotMatch(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, "bar"), "\tassert.go:118: got foo which does not match bar\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

func TestPanicPanicsAndDoesNotPanic(t *testing.T) {
if got, want := doesPanic(2, func() {}, "bar"), "\tassert.go:121: did not panic\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

func TestMatchesMatches(t *testing.T) {
if got, want := matches(2, "aaa", "a"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

func TestMatchesDoesNotMatch(t *testing.T) {
if got, want := matches(2, "aaa", "b"), "\tassert_test.go:102: got aaa which does not match b\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

0 comments on commit ff01f62

Please sign in to comment.