Skip to content

Commit

Permalink
up: testutil/assert - update some assert func logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 11, 2022
1 parent 71b3271 commit 233578f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 85 deletions.
1 change: 0 additions & 1 deletion testutil/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ type TestingT interface {
Helper()
Name() string
Error(args ...any)
Errorf(format string, args ...any)
}
91 changes: 7 additions & 84 deletions testutil/assert/asserts.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package assert

import (
"bufio"
"fmt"
"reflect"
"runtime"
"runtime/debug"
"strings"

"github.com/gookit/color"
"github.com/gookit/goutil/common"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/stdutil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil"
)

// Nil asserts that the given is a nil value
Expand Down Expand Up @@ -191,7 +185,7 @@ func Contains(t TestingT, src, elem any, fmtAndArgs ...any) bool {
}

// not found
return fail(t, fmt.Sprintf("%#v should contain %#v", src, elem), fmtAndArgs)
return fail(t, fmt.Sprintf("%#v\nShould contain: %#v", src, elem), fmtAndArgs)
}

// NotContains asserts that the given data(string,slice,map) should not contain element
Expand All @@ -214,7 +208,7 @@ func NotContains(t TestingT, src, elem any, fmtAndArgs ...any) bool {
}

// found
return fail(t, fmt.Sprintf("%#v should not contain %#v", src, elem), fmtAndArgs)
return fail(t, fmt.Sprintf("%#v\nShould not contain: %#v", src, elem), fmtAndArgs)
}

// ContainsKey asserts that the given map is contains key
Expand Down Expand Up @@ -415,7 +409,11 @@ type failNower interface {
// FailNow fails test
func FailNow(t TestingT, failMsg string, fmtAndArgs ...any) bool {
t.Helper()
// TODO
fail(t, failMsg, fmtAndArgs)

if fnr, ok := t.(failNower); ok {
fnr.FailNow()
}
return false
}

Expand Down Expand Up @@ -463,78 +461,3 @@ func fail(t TestingT, failMsg string, fmtAndArgs []any) bool {
t.Error("\n" + formatLabeledTexts(labeledTexts))
return false
}

func callerInfos() []string {
num := 3
ss := make([]string, 0, num)
cs := sysutil.CallersInfos(4, num, func(file string, fc *runtime.Func) bool {
// This is a huge edge case, but it will panic if this is the case
if file == "<autogenerated>" {
return false
}

fcName := fc.Name()
if fcName == "testing.tRunner" || strings.Contains(fcName, "goutil/testutil/assert") {
return false
}

// eg: runtime.goexit
if strings.HasPrefix(fcName, "runtime.") {
return false
}
return true
})

// cwd := sysutil.Workdir()
for _, info := range cs {
filePath := info.File
if !ShowFullPath {
filePath = fsutil.Name(filePath)
}

ss = append(ss, fmt.Sprintf("%s:%d", filePath, info.Line))
}
return ss
}

// refers from stretchr/testify/assert
type labeledText struct {
label string
message string
}

func formatLabeledTexts(lts []labeledText) string {
labelWidth := 0
elemSize := len(lts)
for _, lt := range lts {
labelWidth = mathutil.MaxInt(len(lt.label), labelWidth)
}

var sb strings.Builder
for i, lt := range lts {
label := lt.label
if EnableColor {
label = color.Green.Sprint(label)
}

sb.WriteString(" " + label + strutil.Repeat(" ", labelWidth-len(lt.label)) + ": ")
formatMessage(lt.message, labelWidth, &sb)
if i+1 != elemSize {
sb.WriteByte('\n')
}
}
return sb.String()
}

func formatMessage(message string, labelWidth int, buf common.StringWriteStringer) string {
for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ {
// skip add prefix for first line.
if i != 0 {
// +3: is len of ": "
_, _ = buf.WriteString("\n " + strings.Repeat(" ", labelWidth+3))
}
_, _ = buf.WriteString(scanner.Text())
}

return buf.String()
}
83 changes: 83 additions & 0 deletions testutil/assert/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ import (
"bufio"
"fmt"
"reflect"
"runtime"
"strings"
"time"

"github.com/gookit/color"
"github.com/gookit/goutil/common"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil"
)

func checkEqualArgs(expected, actual any) error {
Expand Down Expand Up @@ -76,3 +84,78 @@ func formatTplAndArgs(fmtAndArgs ...any) string {
}
return fmt.Sprint(fmtAndArgs...)
}

func callerInfos() []string {
num := 3
ss := make([]string, 0, num)
cs := sysutil.CallersInfos(4, num, func(file string, fc *runtime.Func) bool {
// This is a huge edge case, but it will panic if this is the case
if file == "<autogenerated>" {
return false
}

fcName := fc.Name()
if fcName == "testing.tRunner" || strings.Contains(fcName, "goutil/testutil/assert") {
return false
}

// eg: runtime.goexit
if strings.HasPrefix(fcName, "runtime.") {
return false
}
return true
})

// cwd := sysutil.Workdir()
for _, info := range cs {
filePath := info.File
if !ShowFullPath {
filePath = fsutil.Name(filePath)
}

ss = append(ss, fmt.Sprintf("%s:%d", filePath, info.Line))
}
return ss
}

// refers from stretchr/testify/assert
type labeledText struct {
label string
message string
}

func formatLabeledTexts(lts []labeledText) string {
labelWidth := 0
elemSize := len(lts)
for _, lt := range lts {
labelWidth = mathutil.MaxInt(len(lt.label), labelWidth)
}

var sb strings.Builder
for i, lt := range lts {
label := lt.label
if EnableColor {
label = color.Green.Sprint(label)
}

sb.WriteString(" " + label + strutil.Repeat(" ", labelWidth-len(lt.label)) + ": ")
formatMessage(lt.message, labelWidth, &sb)
if i+1 != elemSize {
sb.WriteByte('\n')
}
}
return sb.String()
}

func formatMessage(message string, labelWidth int, buf common.StringWriteStringer) string {
for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ {
// skip add prefix for first line.
if i != 0 {
// +3: is len of ": "
_, _ = buf.WriteString("\n " + strings.Repeat(" ", labelWidth+3))
}
_, _ = buf.WriteString(scanner.Text())
}

return buf.String()
}

0 comments on commit 233578f

Please sign in to comment.