-
Notifications
You must be signed in to change notification settings - Fork 0
/
testingx.go
62 lines (50 loc) · 1.43 KB
/
testingx.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
// Package testingx implements utilities for tests.
package testingx
import (
"math"
"regexp"
)
// EqualErrors compares errors lhs and rhs for equality.
func EqualErrors(lhs, rhs error) bool {
if lhs == rhs {
return true
}
if lhs != nil && rhs != nil {
if lhs.Error() == rhs.Error() {
return true
}
}
return false
}
// EqualError returns true if error err is non-nil and the error string matches
// string errstr.
func EqualError(err error, errstr string) bool {
return err != nil && err.Error() == errstr
}
// MatchError returns true if error err is non-nil and the error string matches
// regular expression re. It will panic if re cannot compiled.
func MatchError(err error, re string) bool {
return MatchErrorRegexp(err, regexp.MustCompile(re))
}
// MatchErrorRegexp returns true if error err is non-nil and the error string
// matches regular expression re. It will panic if re is nil.
func MatchErrorRegexp(err error, re *regexp.Regexp) bool {
if err == nil {
return false
}
return re.MatchString(err.Error())
}
// Panics returns true if function fn panics.
func Panics(fn func()) bool { return Recover(fn) != nil }
// Recover calls fn and returns the error value passed to panic.
func Recover(fn func()) (v interface{}) {
defer func() {
v = recover()
}()
fn()
return
}
// InDelta returns true if floats lhs and rhs are equal within delta.
func InDelta(lhs, rhs, delta float64) bool {
return math.Abs(lhs-rhs) < delta
}