forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.go
63 lines (51 loc) · 1.24 KB
/
assert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Package assert Provides commonly asserts functions for help write Go testing.
//
// inspired the package: github.com/stretchr/testify/assert
package assert
import (
"strings"
"github.com/gookit/color"
"github.com/gookit/goutil/internal/comfunc"
)
// TestingT is an interface wrapper around *testing.T
type TestingT interface {
Helper()
Name() string
Error(args ...any)
}
//
// -------------------- render error --------------------
//
var (
// ShowFullPath on show error trace
ShowFullPath = true
// EnableColor on show error trace
EnableColor = true
)
// DisableColor render
func DisableColor() {
EnableColor = false
}
// HideFullPath render
func HideFullPath() {
ShowFullPath = false
}
// fail reports a failure through
func fail(t TestingT, failMsg string, fmtAndArgs []any) bool {
t.Helper()
tName := t.Name()
if EnableColor {
tName = color.Red.Sprint(tName)
}
labeledTexts := []labeledText{
{"Test Name", tName},
{"Error Pos", strings.Join(callerInfos(), "\n")},
{"Error Msg", failMsg},
}
// user custom message
if userMsg := comfunc.FormatWithArgs(fmtAndArgs); len(userMsg) > 0 {
labeledTexts = append(labeledTexts, labeledText{"User Msg", userMsg})
}
t.Error("\n" + formatLabeledTexts(labeledTexts))
return false
}