-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasserting.go
More file actions
68 lines (50 loc) · 1.53 KB
/
asserting.go
File metadata and controls
68 lines (50 loc) · 1.53 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
// Package asserting provides macros to verify output, well-suited for use in `go test`
package asserting
import "github.com/thehungry-dev/asserting/rejection"
// TestController provides functionality to interrupt and log during a test execution. It's usually `testing.T`
type TestController interface {
Helper()
Errorf(format string, args ...interface{})
}
// Assert tests if the result is true
var Assert Assertion = Assertion(assertf)
// Assertion provides a function to assert results and recover from functions triggering panic
type Assertion func(TestController, bool, ...interface{})
// PanicMsg asserts that the provided function triggers panic with the provided message
func (assert Assertion) PanicMsg(t TestController, do func(), assertMsg func(interface{}) bool) {
t.Helper()
panicked := true
defer func() {
t.Helper()
err := recover()
assert(t, panicked, rejection.PanicExpected)
if panicked {
result := assertMsg(err)
assert(t, result, rejection.InvalidPanicMsg)
}
}()
do()
panicked = false
}
// Panic asserts that the provided function triggers panic
func (assert Assertion) Panic(t TestController, do func()) {
t.Helper()
assert.PanicMsg(t, do, any)
}
func any(_ interface{}) bool { return true }
func assertf(t TestController, result bool, msgArgs ...interface{}) {
t.Helper()
msg := rejection.FalseAssertion
var args []interface{}
switch len(msgArgs) {
case 0:
case 1:
msg = msgArgs[0].(string)
default:
msg = msgArgs[0].(string)
args = msgArgs[1:]
}
if !result {
t.Errorf(msg, args...)
}
}