-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
173 lines (147 loc) · 4.63 KB
/
helpers.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package actually
// helper functions
import (
"testing"
"github.com/bayashi/actually/witness"
"github.com/bayashi/actually/witness/obj"
"github.com/yassinebenaid/godump"
)
func failNowPtr(v bool) *bool {
return &v
}
// `FailNow` turns a flag on to stop further test execution immediately if one test fails.
// NOTE that FailNow method should be called after `Got` or `Expect`.
/*
actually.Got(something).FailNow().Nil(t) // Fail now for only this test
*/
func (a *testingA) FailNow() *testingA {
a.failNow = failNowPtr(true)
return a
}
// FailNowOn function turns an ENV flag on to stop further test execution immediately if one test fails.
// This switch is enabled within the test. Not only during function.
/*
func Test(t *testing.T) {
actually.FailNowOn(t)
actually.Got(something).Nil(t) // Fail Now
actually.Got(something).Expect(something).Same(t) // Fail Now
}
*/
// Warning: Do not use FailNowOn along with t.Parallel.
func FailNowOn(t *testing.T) {
t.Setenv(envKey_FailNow, envKey_FailNow)
}
// FailNotNowOn function turns an ENV flag off to stop further test execution immediately if one test fails.
// If you want to turn the ENV flag on, then you should call `FailNowOn`.
/*
func Test(t *testing.T) {
// turn on to fail right now
actually.FailNowOn(t)
actually.Got(something).Nil(t) // Fail Now
actually.Got(something).Expect(something).Same(t) // Fail Now
// turn off
actually.FailNotNowOn(t)
actually.Got(something).Nil(t) // NOT Fail Now
actually.Got(something).Expect(something).Same(t) // NOT Fail Now
// Fail Now by FailNow() in the chain
actually.Got(something).FailNow().Nil(t) // Fail Now
// Again, turn on to fail right now
actually.FailNowOn(t)
actually.Got(something).Nil(t) // Fail Now
}
*/
// This switch is enabled within the test. Not only during function.
func FailNotNowOn(t *testing.T) {
t.Setenv(envKey_FailNow, "")
}
// FailNotNow turns a flag off, so that even if the test fails, execution does not stop immediately.
/*
It behaves this way by default. If you want the opposite behavior, call `FailNow` method.
NOTE that FailNotNow method should be called after `Got` or `Expect`.
*/
// Deprecated: Anyone uses? This method will be removed in the near future.
func (a *testingA) FailNotNow() *testingA {
a.failNow = failNowPtr(false)
return a
}
// X turns on a flag to show test values as raw in a fail report.
func (a *testingA) X() *testingA {
a.showRawData = true
return a
}
// Skip provides shorthand to skip further tests within the same function for `-short` option.
/*
func Test(t *testing.T) {
Got(1).NotNil(t) // Run
Skip(t)
Got(2).NotNil(t) // Skip
Got(3).NotNil(t) // Skip Also
}
*/
func Skip(t *testing.T, skipReasons ...any) {
if testing.Short() {
t.Skip(skipReasons...)
}
}
// Set test name spefically.
/*
You can also set a test name on assertion methods.
actually.Got(a).Expect(b).Same(t, "Test Name")
*/
func (a *testingA) Name(n string) *testingA {
a.name = n
return a
}
// Fi doesn NOT return `*testingA`, returns whether a test failed instead.
// If even there is only one test fails in a one chain to test, then it will be `true` anyway.
/*
func Test(t *testing.T) {
love := true
// Fi returns `true` if either `NotNil(t)`, `True(t)` or 'Same(t)' failed
if res := a.Got(love).NotNil(t).True(t).Expect(true).Same(t).Fi(); !res {
// your own some action on fail
}
}
*/
func (a *testingA) Fi() bool {
return a.failed
}
// Diff is a helper function to get a diff string of 2 objects for debugging
func Diff(a any, b any) string {
return witness.Diff(a, b)
}
// Dump is a helper function to get a dumped string of an object for debugging
func Dump(a any) string {
obj.DUMPER = func(d any) string {
var dumper godump.Dumper
return dumper.Sprint(d)
}
return witness.Dump(a)
}
// Debug is a helper function to show debug info on fail
func (a *testingA) Debug(label string, info ...any) *testingA {
a.debugInfo.mutex.Lock()
defer a.debugInfo.mutex.Unlock()
a.debugInfo.debugInfo = append(a.debugInfo.debugInfo, map[string][]any{label: info})
return a
}
// Fail is to show decorated fail report. (Actual shortcut to witness.Fail)
/*
if g != e {
actually.Fail(t, "Not same", g, e)
}
*/
func Fail(t *testing.T, reason string, got any, expect ...any) {
t.Helper()
witness.Fail(t, reason, got, expect...)
}
// FailNow is to show decorated fail report by t.Fatal. (Actual shortcut to witness.FailNow)
/*
if g != e {
actually.FailNow(t, "Not same", g, e)
}
*/
func FailNow(t *testing.T, reason string, got any, expect ...any) {
t.Helper()
witness.FailNow(t, reason, got, expect...)
}