-
Notifications
You must be signed in to change notification settings - Fork 0
/
testingx_test.go
132 lines (111 loc) · 2.34 KB
/
testingx_test.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
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
package testingx
import (
"errors"
"fmt"
"math"
"regexp"
"testing"
)
func TestEqualErrors(t *testing.T) {
err := errors.New("error")
cases := []struct {
equal bool
lhs error
rhs error
}{
{true, err, err},
{true, errors.New("error"), errors.New("error")},
{false, errors.New("error"), fmt.Errorf("error rhs")},
}
for _, c := range cases {
got := EqualErrors(c.lhs, c.rhs)
if got != c.equal {
t.Errorf("EqualErrors(%#v, %#v) = %v, want: %v", c.lhs, c.rhs, got, c.equal)
}
}
}
func TestEqualError(t *testing.T) {
cases := []struct {
in error
want bool
}{
{errors.New("error"), true},
{nil, false},
}
for _, c := range cases {
const errstr = "error"
got := EqualError(c.in, errstr)
if got != c.want {
t.Errorf("EqualError(%#v, %q) = %v, want: %v", c.in, errstr, got, c.want)
}
}
}
func TestMatchError(t *testing.T) {
cases := []struct {
in error
want bool
}{
{errors.New("error"), true},
{nil, false},
}
for _, c := range cases {
const re = "^error$"
got := MatchError(c.in, re)
if got != c.want {
t.Errorf("MatchError(%#v, %q) = %v, want: %v", c.in, re, got, c.want)
}
}
}
func TestMatchError_panic(t *testing.T) {
paniced := Panics(func() {
MatchError(errors.New("error"), "\\")
})
if !paniced {
t.Error("MatchError did not panic when regular expression cannot be compiled")
}
}
func TestMatchErrorRegexp(t *testing.T) {
cases := []struct {
in error
want bool
}{
{errors.New("error"), true},
{nil, false},
}
re := regexp.MustCompile("^error$")
for _, c := range cases {
got := MatchErrorRegexp(c.in, re)
if got != c.want {
t.Errorf("MatchErrorRegexp(%#v, %v) = %v, want: %v", c.in, re, got, c.want)
}
}
}
func TestMatchErrorRegexp_panic(t *testing.T) {
paniced := Panics(func() {
MatchErrorRegexp(errors.New("error"), nil)
})
if !paniced {
t.Error("MatchErrorRegexp did not panic when regular expression is nil")
}
}
func TestPanics(t *testing.T) {
paniced := Panics(func() {
panic("trigger panic")
})
if !paniced {
t.Error("Panics did not panic")
}
}
func TestInDelta(t *testing.T) {
cases := []struct {
lhs, rhs, delta float64
}{
{math.Sqrt(3), 1.732, 1e-3},
{math.Pow(3, 1.2), 3.737192, 1e-6},
}
for _, c := range cases {
if !InDelta(c.lhs, c.rhs, c.delta) {
t.Errorf("%f != %f ± %f", c.lhs, c.rhs, c.delta)
}
}
}