-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathit.go
More file actions
213 lines (175 loc) · 4.93 KB
/
it.go
File metadata and controls
213 lines (175 loc) · 4.93 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Copyright (C) 2019 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/it
//
package it
import (
"errors"
"fmt"
"testing"
)
//
// Imperatives
//
// Check type is constructor of imperative testing expressions.
//
// it.Then(t).Should( ... )
type Check struct {
t *testing.T // Note: intentionally hidden from clients
}
// Then creates assertion expression, it takes a reference to
// standard *testing.T interface to setup the evaluation context.
func Then(t *testing.T) *Check { return &Check{t} }
// Ok alias to Then
func Ok(t *testing.T) *Check { return &Check{t} }
// Must is imperative keyword.
// The assert definition is an absolute requirement.
// It terminates execution of tests if assert is failed.
func (check *Check) Must(errs ...error) *Check {
check.t.Helper()
for _, err := range errs {
if err != nil {
var e interface{ Passed() bool }
ok := errors.As(err, &e)
output := check.fatalf
if ok && e.Passed() {
output = check.noticef
}
output("must %s", err)
}
}
return check
}
// MustNot is imperative keyword.
// The definition is an absolute prohibition
// It terminates execution of tests if assert is not failed.
func (check *Check) MustNot(errs ...error) *Check {
check.t.Helper()
for _, err := range errs {
if err != nil {
var e interface{ Passed() bool }
ok := errors.As(err, &e)
output := check.fatalf
if !(ok && e.Passed()) {
output = check.noticef
}
output("must not %s", err)
}
}
return check
}
// Should is imperative keyword.
// The assert definition is a strongly recommended requirement.
// However it's violation do not block continuation of testing.
// The test fails if assert is failed.
func (check *Check) Should(errs ...error) *Check {
check.t.Helper()
for _, err := range errs {
if err != nil {
var e interface{ Passed() bool }
ok := errors.As(err, &e)
output := check.errorf
if ok && e.Passed() {
output = check.noticef
}
output("should %s", err)
}
}
return check
}
// ShouldNot is imperative keyword.
// The assert definition is a strongly recommended prohibition.
// However it's violation do not block continuation of testing.
// The test fails if assert is not failed.
func (check *Check) ShouldNot(errs ...error) *Check {
check.t.Helper()
for _, err := range errs {
if err != nil {
var e interface{ Passed() bool }
ok := errors.As(err, &e)
output := check.errorf
if !(ok && e.Passed()) {
output = check.noticef
}
output("should not %s", err)
}
}
return check
}
// May is an optional imperative constrain.
// Its violation do not impact on testing flow in anyway.
// The informative warning message is produced to console if assert is failed.
// Error message will be printed only if the test fails or the -test.v
func (check *Check) May(errs ...error) *Check {
check.t.Helper()
for _, err := range errs {
if err != nil {
var e interface{ Passed() bool }
ok := errors.As(err, &e)
output := check.warningf
if ok && e.Passed() {
output = check.noticef
}
output("may %s", err)
}
}
return check
}
// MayNot is an optional imperative constrain.
// Its violation do not impact on testing flow in anyway.
// The informative warning message is produced to console if assert is not failed.
// Error message will be printed only if the test fails or the -test.v
func (check *Check) MayNot(errs ...error) *Check {
check.t.Helper()
for _, err := range errs {
if err != nil {
var e interface{ Passed() bool }
ok := errors.As(err, &e)
output := check.warningf
if !(ok && e.Passed()) {
output = check.noticef
}
output("may not %s", err)
}
}
return check
}
// Skip is imperative keyword
// It ignores results of assert
func (check *Check) Skip(errs ...error) *Check {
check.t.Helper()
for _, err := range errs {
check.debugf("skip %s", err)
}
return check
}
func (check *Check) fatalf(msg string, args ...any) {
check.t.Helper()
check.t.Fatalf("\033[31m%s\033[0m", fmt.Sprintf(msg, args...))
}
func (check *Check) errorf(msg string, args ...any) {
check.t.Helper()
check.t.Errorf("\033[31m%s\033[0m", fmt.Sprintf(msg, args...))
}
func (check *Check) warningf(msg string, args ...any) {
check.t.Helper()
check.t.Logf("\033[33m%s\033[0m", fmt.Sprintf(msg, args...))
}
func (check *Check) noticef(msg string, args ...any) {
check.t.Helper()
check.t.Logf("\033[32m%s\033[0m", fmt.Sprintf(msg, args...))
}
func (check *Check) debugf(msg string, args ...any) {
check.t.Helper()
check.t.Logf("%s", fmt.Sprintf(msg, args...))
}
// ok labels assert with success
type ok struct{ err error }
func passed(err error) *ok { return &ok{err} }
func (e *ok) Error() string { return e.err.Error() }
func (e *ok) As(target any) bool { return errors.As(e.err, target) }
func (e *ok) Unwarp() error { return e.err }
func (e *ok) Passed() bool { return true }