-
Notifications
You must be signed in to change notification settings - Fork 4
/
assert.go
91 lines (71 loc) · 1.9 KB
/
assert.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
package assert
import (
"io/ioutil"
"runtime"
"strings"
"testing"
_ "github.com/ToQoz/gopwt/translatedassert" // for go get github.com/ToQoz/gopwt/assert
)
type testingInterface interface {
Error(args ...interface{})
Skip(args ...interface{})
}
// OK assert given bool is true.
// This will be translate to `translatedassert.OK` by gopwt.
func OK(t *testing.T, ok bool, messages ...string) {
_ok(t, ok, callerLine(1), messages...)
}
func _ok(t testingInterface, ok bool, callerLine string, messages ...string) {
if ok {
return
}
msg := `[FAIL Assertion] ` + callerLine + `
Please call gopwt.Empower() in your TestMain(t *testing.M). It give you power.
If you need more information, see http://github.com/ToQoz/gopwt
`
if len(messages) > 0 {
msg += "\nAssertionMessage:\n"
for _, m := range messages {
msg += "\t- " + m
}
}
t.Error(msg)
}
// Require assert given bool is true.
// The difference from OK is Require calls t.Skip after t.Error on fail.
// This will be translate to `translatedassert.Require` by gopwt.
func Require(t *testing.T, ok bool, messages ...string) {
_require(t, ok, callerLine(1), messages...)
}
func _require(t testingInterface, ok bool, callerLine string, messages ...string) {
if ok {
return
}
msg := `[FAIL Assertion] ` + callerLine + `
Please call gopwt.Empower() in your TestMain(t *testing.M). It give you power.
If you need more information, see http://github.com/ToQoz/gopwt
`
if len(messages) > 0 {
msg += "\nAssertionMessage:\n"
for _, m := range messages {
msg += "\t- " + m
}
}
t.Error(msg)
t.Skip("skip by gopwt/assert.Require")
}
func callerLine(skip int) string {
_, file, lnum, ok := runtime.Caller(skip + 1)
if !ok {
return ""
}
b, err := ioutil.ReadFile(file)
if err != nil {
return ""
}
s := strings.Split(string(b), "\n")
if len(s) <= lnum-1 {
return ""
}
return strings.Trim(strings.Trim(s[lnum-1], " \n\t"), "\r\n")
}