-
Notifications
You must be signed in to change notification settings - Fork 2
/
failmsg.go
93 lines (85 loc) · 2.39 KB
/
failmsg.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
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
package verify
// FailureMessage encapsulates a failure message
// that can by emitted using objects compatible
// with the testing.TB interface.
type FailureMessage string
// Assert calls t.Error if the failure message is not empty.
// Calling Error on *testing.T marks the the function as having failed
// and continues its execution.
// Returns true when the failure message is empty.
func (msg FailureMessage) Assert(t interface {
Error(args ...any)
}, args ...any,
) bool {
if msg == "" {
return true
}
if h, ok := t.(interface{ Helper() }); ok {
h.Helper()
}
t.Error(append(args, "\n"+string(msg))...)
return false
}
// Require calls t.Fatal if the failure message is not empty.
// Calling Fatal on *testing.T stops the test function execution.
// Returns true when the failure message is empty.
func (msg FailureMessage) Require(t interface {
Fatal(args ...any)
}, args ...any,
) bool {
if msg == "" {
return true
}
if h, ok := t.(interface{ Helper() }); ok {
h.Helper()
}
t.Fatal(append(args, "\n"+string(msg))...)
return false
}
// Assertf calls t.Errorf if the failure message is not empty.
// Calling Errorf on *testing.T marks the the function as having failed
// and continues its execution.
// Returns true when the failure message is empty
func (msg FailureMessage) Assertf(t interface {
Errorf(format string, args ...any)
}, format string, args ...any,
) bool {
if msg == "" {
return true
}
if h, ok := t.(interface{ Helper() }); ok {
h.Helper()
}
t.Errorf(format+"%s", append(args, "\n"+string(msg))...)
return false
}
// Requiref calls t.Fatalf if the failure message is not empty.
// Calling Fatalf on *testing.T stops the test function execution.
// Returns true when the failure message is empty.
func (msg FailureMessage) Requiref(t interface {
Fatalf(format string, args ...any)
}, format string, args ...any,
) bool {
if msg == "" {
return true
}
if h, ok := t.(interface{ Helper() }); ok {
h.Helper()
}
t.Fatalf(format+"%s", append(args, "\n"+string(msg))...)
return false
}
// Prefix adds prefix if the failure message is not empty.
func (msg FailureMessage) Prefix(s string) FailureMessage {
if msg == "" {
return ""
}
return FailureMessage(s) + msg
}
// Err returns the failure message as an error type, or nil if the message is empty.
func (msg FailureMessage) Err() *AssertionError {
if msg == "" {
return nil
}
return &AssertionError{Message: msg}
}