-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecs.go
123 lines (110 loc) · 3.01 KB
/
specs.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
package specs
import (
"regexp"
"testing"
)
// Specs is a wrapper for *testing.T
type Specs struct {
t *testing.T
}
// Spec is a struct used for creating table-driven tests.
//
// ExpectAll takes []Spec as input.
type Spec struct {
Expected interface{}
Actual interface{}
}
// New initializes the specs by storing a reference to the *testing.T.
func New(t *testing.T) *Specs {
return &Specs{t}
}
// Expect checks for equality between expected and actual.
func (s *Specs) Expect(expected, actual interface{}, msg ...string) {
if actual != expected {
if len(msg) > 0 {
s.t.Error(msg[0])
} else {
s.t.Errorf("expected: \"%+v\" but got: \"%+v\"", expected, actual)
}
}
}
// ExpectMatches checks if the string actual has a match in expected.
func (s *Specs) ExpectMatches(expected, actual string, msg ...string) {
r := regexp.MustCompile(expected)
if !r.MatchString(actual) {
s.t.Errorf("expected match: \"%+v\" but got no match: \"%+v\"", expected, actual)
}
}
// ExpectNotMatches checks if the string actual not has a match in expected.
func (s *Specs) ExpectNotMatches(expected, actual string, msg ...string) {
r := regexp.MustCompile(expected)
if r.MatchString(actual) {
s.t.Errorf("expected no match: \"%+v\" but got match: \"%+v\"", expected, actual)
}
}
// ExpectNot checks for inequality between expected and actual.
func (s *Specs) ExpectNot(expected, actual interface{}, msg ...string) {
if actual == expected {
if len(msg) > 0 {
s.t.Error(msg[0])
} else {
s.t.Errorf("expected: \"%+v\" not to be: \"%+v\"", expected, actual)
}
}
}
// ExpectNil checks that actual is nil.
func (s *Specs) ExpectNil(actual interface{}, msg ...string) {
if actual == nil {
return
}
if len(msg) > 0 {
s.t.Error(msg[0])
} else {
s.t.Errorf("expected \"%+v\" to be <nil>", actual)
}
}
// ExpectNilFatal checks that actual is nil, and calls t.Fatal if not.
func (s *Specs) ExpectNilFatal(actual interface{}, msg ...string) {
if actual == nil {
return
}
if len(msg) > 0 {
s.t.Fatal(msg[0])
} else {
s.t.Fatalf("expected \"%+v\" to be <nil>", actual)
}
}
// ExpectNotNil checks that actual is not nil.
func (s *Specs) ExpectNotNil(actual interface{}, msg ...string) {
if actual != nil {
return
}
if len(msg) > 0 {
s.t.Error(msg[0])
} else {
s.t.Errorf("expected \"%+v\" to be not <nil>", actual)
}
}
// ErrExpect checks if err is nil before comparing expected and acutal.
// It calls t.Fatal(err) and shortcircut the test if err is not nil.
func (s *Specs) ErrExpect(err, expected, actual interface{}, msg ...string) {
if err != nil {
if len(msg) > 0 {
s.t.Fatal(msg[0])
} else {
s.t.Fatal(err)
}
}
if actual != expected {
s.t.Errorf("expected: \"%+v\" but got: \"%+v\"", expected, actual)
}
}
// ExpectAll takes []Spec and runs through all the tests, checking if
// Spec.actual is equal to Spec.expected.
func (s *Specs) ExpectAll(tests []Spec) {
for _, t := range tests {
if t.Actual != t.Expected {
s.t.Errorf("expected: \"%+v\" but got: \"%+v\"", t.Expected, t.Actual)
}
}
}