• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fogfish / it / 9052871904

12 May 2024 04:50PM UTC coverage: 82.405% (-2.9%) from 85.337%
9052871904

push

github

web-flow
(fix) do not panic if MUST condition fail (#16)

* (fix) do not panic if MUST condition fail
* Update README and code formatting

0 of 1 new or added line in 1 file covered. (0.0%)

9 existing lines in 1 file now uncovered.

281 of 341 relevant lines covered (82.4%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.85
/it.go
1
//
2
// Copyright (C) 2019 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/fogfish/it
7
//
8

9
package it
10

11
import (
12
        "errors"
13
        "fmt"
14
        "testing"
15
)
16

17
//
18
// Imperatives
19
//
20

21
// Check type is constructor of imperative testing expressions.
22
//
23
//        it.Then(t).Should( ... )
24
type Check struct {
25
        t *testing.T // Note: intentionally hidden from clients
26
}
27

28
// Then creates assertion expression, it takes a reference to
29
// standard *testing.T interface to setup the evaluation context.
30
func Then(t *testing.T) *Check { return &Check{t} }
1✔
31

32
// Ok alias to Then
33
func Ok(t *testing.T) *Check { return &Check{t} }
1✔
34

35
// Must is imperative keyword.
36
// The assert definition is an absolute requirement.
37
// It terminates execution of tests if assert is failed.
38
func (check *Check) Must(errs ...error) *Check {
1✔
39
        check.t.Helper()
1✔
40

1✔
41
        for _, err := range errs {
2✔
42
                if err != nil {
1✔
UNCOV
43
                        var e interface{ Passed() bool }
×
UNCOV
44
                        ok := errors.As(err, &e)
×
UNCOV
45
                        output := check.fatalf
×
UNCOV
46

×
UNCOV
47
                        if ok && e.Passed() {
×
48
                                output = check.noticef
×
49
                        }
×
UNCOV
50
                        output("must %s", err)
×
51
                }
52
        }
53

54
        return check
1✔
55
}
56

57
// MustNot is imperative keyword.
58
// The definition is an absolute prohibition
59
// It terminates execution of tests if assert is not failed.
60
func (check *Check) MustNot(errs ...error) *Check {
×
61
        check.t.Helper()
×
62

×
63
        for _, err := range errs {
×
64
                if err != nil {
×
65
                        var e interface{ Passed() bool }
×
66
                        ok := errors.As(err, &e)
×
67
                        output := check.fatalf
×
68

×
69
                        if !(ok && e.Passed()) {
×
70
                                output = check.noticef
×
71
                        }
×
72
                        output("must not %s", err)
×
73
                }
74
        }
75

76
        return check
×
77
}
78

79
// Should is imperative keyword.
80
// The assert definition is a strongly recommended requirement.
81
// However it's violation do not block continuation of testing.
82
// The test fails if assert is failed.
83
func (check *Check) Should(errs ...error) *Check {
1✔
84
        check.t.Helper()
1✔
85

1✔
86
        for _, err := range errs {
2✔
87
                if err != nil {
2✔
88
                        var e interface{ Passed() bool }
1✔
89
                        ok := errors.As(err, &e)
1✔
90
                        output := check.errorf
1✔
91

1✔
92
                        if ok && e.Passed() {
2✔
93
                                output = check.noticef
1✔
94
                        }
1✔
95
                        output("should %s", err)
1✔
96
                }
97
        }
98

99
        return check
1✔
100
}
101

102
// ShouldNot is imperative keyword.
103
// The assert definition is a strongly recommended prohibition.
104
// However it's violation do not block continuation of testing.
105
// The test fails if assert is not failed.
106
func (check *Check) ShouldNot(errs ...error) *Check {
1✔
107
        check.t.Helper()
1✔
108

1✔
109
        for _, err := range errs {
2✔
110
                if err != nil {
2✔
111
                        var e interface{ Passed() bool }
1✔
112
                        ok := errors.As(err, &e)
1✔
113
                        output := check.errorf
1✔
114

1✔
115
                        if !(ok && e.Passed()) {
2✔
116
                                output = check.noticef
1✔
117
                        }
1✔
118
                        output("should not %s", err)
1✔
119
                }
120
        }
121

122
        return check
1✔
123
}
124

125
// May is an optional imperative constrain.
126
// Its violation do not impact on testing flow in anyway.
127
// The informative warning message is produced to console if assert is failed.
128
// Error message will be printed only if the test fails or the -test.v
129
func (check *Check) May(errs ...error) *Check {
1✔
130
        check.t.Helper()
1✔
131

1✔
132
        for _, err := range errs {
2✔
133
                if err != nil {
2✔
134
                        var e interface{ Passed() bool }
1✔
135
                        ok := errors.As(err, &e)
1✔
136
                        output := check.warningf
1✔
137

1✔
138
                        if ok && e.Passed() {
1✔
139
                                output = check.noticef
×
140
                        }
×
141
                        output("may %s", err)
1✔
142
                }
143
        }
144

145
        return check
1✔
146
}
147

148
// MayNot is an optional imperative constrain.
149
// Its violation do not impact on testing flow in anyway.
150
// The informative warning message is produced to console if assert is not failed.
151
// Error message will be printed only if the test fails or the -test.v
152
func (check *Check) MayNot(errs ...error) *Check {
×
153
        check.t.Helper()
×
154

×
155
        for _, err := range errs {
×
156
                if err != nil {
×
157
                        var e interface{ Passed() bool }
×
158
                        ok := errors.As(err, &e)
×
159
                        output := check.warningf
×
160

×
161
                        if !(ok && e.Passed()) {
×
162
                                output = check.noticef
×
163
                        }
×
164
                        output("may not %s", err)
×
165
                }
166
        }
167

168
        return check
×
169
}
170

171
// Skip is imperative keyword
172
// It ignores results of assert
173
func (check *Check) Skip(errs ...error) *Check {
×
174
        check.t.Helper()
×
175
        for _, err := range errs {
×
176
                check.debugf("skip %s", err)
×
177
        }
×
178
        return check
×
179
}
180

UNCOV
181
func (check *Check) fatalf(msg string, args ...any) {
×
UNCOV
182
        check.t.Helper()
×
NEW
183
        check.t.Fatalf("\033[31m%s\033[0m", fmt.Sprintf(msg, args...))
×
UNCOV
184
}
×
185

186
func (check *Check) errorf(msg string, args ...any) {
1✔
187
        check.t.Helper()
1✔
188
        check.t.Errorf("\033[31m%s\033[0m", fmt.Sprintf(msg, args...))
1✔
189
}
1✔
190

191
func (check *Check) warningf(msg string, args ...any) {
1✔
192
        check.t.Helper()
1✔
193
        check.t.Logf("\033[33m%s\033[0m", fmt.Sprintf(msg, args...))
1✔
194
}
1✔
195

196
func (check *Check) noticef(msg string, args ...any) {
1✔
197
        check.t.Helper()
1✔
198
        check.t.Logf("\033[32m%s\033[0m", fmt.Sprintf(msg, args...))
1✔
199
}
1✔
200

201
func (check *Check) debugf(msg string, args ...any) {
×
202
        check.t.Helper()
×
203
        check.t.Logf("%s", fmt.Sprintf(msg, args...))
×
204
}
×
205

206
// ok labels assert with success
207
type ok struct{ err error }
208

209
func passed(err error) *ok       { return &ok{err} }
1✔
210
func (e *ok) Error() string      { return e.err.Error() }
1✔
211
func (e *ok) As(target any) bool { return errors.As(e.err, target) }
×
212
func (e *ok) Unwarp() error      { return e.err }
×
213
func (e *ok) Passed() bool       { return true }
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc