/
new_matchers_test.go
226 lines (182 loc) · 6.21 KB
/
new_matchers_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright © 2009-2010 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
package gospec
import (
"container/list"
"fmt"
"math"
"testing"
"os"
)
func Test__Placeholders_in_error_messages_are_replaced_with_variables(t *testing.T) {
m := Errorf("%v x %v", 1, 2)
assertEquals("1 x 2", m.String(), t)
}
func Test__Positive_expectation_failures_are_reported_with_the_positive_message(t *testing.T) {
log := new(spyErrorLogger)
m := newMatcherAdapter(nil, log)
m.Expect(1, dummyMatcher, 1)
log.ShouldHaveNoErrors(t)
m.Expect(1, dummyMatcher, 2)
log.ShouldHaveTheError("Positive failure: 1, 2", t)
}
func Test__Negative_expectation_failures_are_reported_with_the_negative_message(t *testing.T) {
log := new(spyErrorLogger)
m := newMatcherAdapter(nil, log)
m.Expect(1, Not(dummyMatcher), 2)
log.ShouldHaveNoErrors(t)
m.Expect(1, Not(dummyMatcher), 1)
log.ShouldHaveTheError("Negative failure: 1, 1", t)
}
func Test__Errors_in_expectations_are_reported_with_the_error_message(t *testing.T) {
log := new(spyErrorLogger)
m := newMatcherAdapter(nil, log)
m.Expect(666, dummyMatcher, 1)
log.ShouldHaveTheError("Error: 666", t)
}
func dummyMatcher(actual interface{}, expected interface{}) (ok bool, pos os.Error, neg os.Error, err os.Error) {
if actual.(int) == 666 {
err = Errorf("Error: %v", actual)
return
}
ok = actual == expected
pos = Errorf("Positive failure: %v, %v", actual, expected)
neg = Errorf("Negative failure: %v, %v", actual, expected)
return
}
// "Equals"
func Test__Equals_matcher_on_strings(t *testing.T) {
assertExpectation(t, "apple", Equals, "apple").Passes()
assertExpectation(t, "apple", Equals, "orange").Fails().
WithMessage(
"Expected 'orange' but was 'apple'",
"Did not expect 'orange' but was 'apple'")
}
func Test__Equals_matcher_on_ints(t *testing.T) {
assertExpectation(t, 42, Equals, 42).Passes()
assertExpectation(t, 42, Equals, 999).Fails()
}
func Test__Equals_matcher_on_structs(t *testing.T) {
assertExpectation(t, DummyStruct{42, 1}, Equals, DummyStruct{42, 2}).Passes()
assertExpectation(t, DummyStruct{42, 1}, Equals, DummyStruct{999, 2}).Fails()
}
func Test__Equals_matcher_on_struct_pointers(t *testing.T) {
assertExpectation(t, &DummyStruct{42, 1}, Equals, &DummyStruct{42, 2}).Passes()
assertExpectation(t, &DummyStruct{42, 1}, Equals, &DummyStruct{999, 2}).Fails()
}
type DummyStruct struct {
value int
ignoredValue int
}
func (this DummyStruct) Equals(other interface{}) bool {
switch that := other.(type) {
case DummyStruct:
return this.equals(&that)
case *DummyStruct:
return this.equals(that)
}
return false
}
func (this *DummyStruct) equals(that *DummyStruct) bool {
return this.value == that.value
}
func (this DummyStruct) String() string {
return fmt.Sprintf("DummyStruct%v", this.value)
}
// "Satisfy"
func Test__Satisfy_matcher(t *testing.T) {
value := 42
assertExpectation(t, value, Satisfies, value < 100).Passes()
assertExpectation(t, value, Satisfies, value > 100).Fails().
WithMessage(
"Criteria not satisfied by '42'",
"Criteria not satisfied by '42'")
}
// "IsWithin"
func Test__IsWithin_matcher(t *testing.T) {
value := float64(3.141)
pi := float64(math.Pi)
assertExpectation(t, value, IsWithin(0.001), pi).Passes()
assertExpectation(t, value, IsWithin(0.0001), pi).Fails().
WithMessage(
"Expected '3.141592653589793' ± 0.0001 but was '3.141'",
"Did not expect '3.141592653589793' ± 0.0001 but was '3.141'")
}
func Test__IsWithin_matcher_cannot_compare_ints(t *testing.T) {
value := int(3)
pi := float64(math.Pi)
assertExpectation(t, value, IsWithin(0.001), pi).GivesError("Expected a float, but was '3' of type 'int'")
assertExpectation(t, pi, IsWithin(0.001), value).GivesError("Expected a float, but was '3' of type 'int'")
}
// "Contains"
func Test__Contains_matcher_on_arrays(t *testing.T) {
values := [...]string{"one", "two", "three"}
assertExpectation(t, values, Contains, "one").Passes()
assertExpectation(t, values, Contains, "two").Passes()
assertExpectation(t, values, Contains, "three").Passes()
assertExpectation(t, values, Contains, "four").Fails().
WithMessage(
"Expected 'four' to be in '[one two three]' but it was not",
"Did not expect 'four' to be in '[one two three]' but it was")
}
func Test__Contains_matcher_on_iterators(t *testing.T) {
values := list.New()
values.PushBack("one")
values.PushBack("two")
values.PushBack("three")
assertExpectation(t, values.Iter(), Contains, "one").Passes()
assertExpectation(t, values.Iter(), Contains, "two").Passes()
assertExpectation(t, values.Iter(), Contains, "three").Passes()
assertExpectation(t, values.Iter(), Contains, "four").Fails().
WithMessage(
"Expected 'four' to be in '[one two three]' but it was not",
"Did not expect 'four' to be in '[one two three]' but it was")
}
func Test__Contains_matcher_cannot_iterate_noniterables(t *testing.T) {
assertExpectation(t, "one two three", Contains, "one").
GivesError("Unknown type 'string', not iterable: one two three")
}
// Test utilities
func assertExpectation(t *testing.T, actual interface{}, matcher Matcher, expected interface{}) *matchAssert {
ok, pos, neg, err := matcher(actual, expected)
return &matchAssert{ok, pos, neg, err, t}
}
type matchAssert struct {
ok bool
pos os.Error
neg os.Error
err os.Error
t *testing.T
}
func (this *matchAssert) Passes() *matchAssert {
if this.err != nil {
this.t.Error("expected to pass, but had an error")
}
if !this.ok {
this.t.Error("expected to pass, but failed")
}
return this
}
func (this *matchAssert) Fails() *matchAssert {
if this.err != nil {
this.t.Error("expected to fail, but had an error")
}
if this.ok {
this.t.Error("expected to fail, but passed")
}
return this
}
func (this *matchAssert) WithMessage(expectedPos string, expectedNeg string) *matchAssert {
assertEquals(expectedPos, this.pos.String(), this.t)
assertEquals(expectedNeg, this.neg.String(), this.t)
return this
}
func (this *matchAssert) GivesError(expectedErr string) *matchAssert {
if this.err == nil {
this.t.Error("expected have an error, but did not")
} else {
assertEquals(expectedErr, this.err.String(), this.t)
}
return this
}