-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathassert.go
More file actions
128 lines (106 loc) · 2.64 KB
/
Copy pathassert.go
File metadata and controls
128 lines (106 loc) · 2.64 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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/goutil/internal/comfunc"
"github.com/gookit/goutil/x/ccolor"
)
// 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
// FailFast fail fast, stop test when first error(will call testing.T.FailNow())
FailFast = false
)
// DisableColor render
func DisableColor() { EnableColor = false }
// HideFullPath render
func HideFullPath() { ShowFullPath = false }
// SetFailFast set fail fast
func SetFailFast(enable bool) { FailFast = enable }
// fail reports a failure through
func fail(t TestingT, failMsg string, fmtAndArgs []any) bool {
t.Helper()
tName := t.Name()
if EnableColor {
tName = ccolor.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))
// fail fast handle
if FailFast {
if fnr, ok := t.(failNower); ok {
fnr.FailNow()
}
}
return false
}
//
// -------------------- required --------------------
//
// Must assert that the given condition is true. alias of Require()
//
// If it's not, it calls t.FailNow() to terminate the test.
//
// Usage:
//
// assert.Must(t, assert.True(false))
func Must(t TestingT, condition bool, fmtAndArgs ...any) {
t.Helper()
if !condition {
FailNow(t, "Required", fmtAndArgs...)
}
}
// Require asserts that the given condition is true.
//
// If it's not, it calls t.FailNow() to terminate the test.
//
// Usage:
//
// assert.Require(t, assert.True(false))
func Require(t TestingT, condition bool, fmtAndArgs ...any) {
t.Helper()
if !condition {
FailNow(t, "Required", fmtAndArgs...)
}
}
//
// -------------------- fail --------------------
//
// Fail reports a failure through
func Fail(t TestingT, failMsg string, fmtAndArgs ...any) bool {
t.Helper()
return fail(t, failMsg, fmtAndArgs)
}
type failNower interface {
FailNow()
}
// FailNow fails test
func FailNow(t TestingT, failMsg string, fmtAndArgs ...any) bool {
t.Helper()
fail(t, failMsg, fmtAndArgs)
if fnr, ok := t.(failNower); ok {
fnr.FailNow()
}
return false
}