/
matcher_test.go
93 lines (76 loc) · 2.75 KB
/
matcher_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
package quiz
import (
"path/filepath"
"runtime"
"testing"
)
func testTrue(t *testing.T, ex bool) {
if !ex {
_, file, line, _ := runtime.Caller(1)
t.Logf("Failed at %s:%v\n", filepath.Base(file), line)
t.Fail()
}
}
func expectPositive(target interface{}) *Matcher {
return PositiveMatcher(newSpyTestHarness(), target)
}
func expectNegative(target interface{}) *Matcher {
return NegativeMatcher(newSpyTestHarness(), target)
}
func TestEqualSucceeds(t *testing.T) {
testTrue(t, expectPositive("foo").Equal("foo").failure == false)
testTrue(t, expectNegative("foo").Equal("bar").failure == false)
}
func TestEqualFails(t *testing.T) {
failingMatch := expectPositive("foo").Equal("bar")
testTrue(t, failingMatch.failure == true)
testTrue(t, failingMatch.message == "Expected foo to equal bar.")
}
func TestBeTrueSucceeds(t *testing.T) {
testTrue(t, expectPositive(true).BeTrue().failure == false)
testTrue(t, expectNegative(false).BeTrue().failure == false)
}
func TestBeTrueFails(t *testing.T) {
failingMatch := expectPositive(false).BeTrue()
testTrue(t, failingMatch.failure == true)
testTrue(t, failingMatch.message == "Expected false to be true.")
}
func TestBeFalseSucceeds(t *testing.T) {
testTrue(t, expectPositive(false).BeFalse().failure == false)
testTrue(t, expectNegative(false).BeFalse().failure == true)
}
func TestBeFalseFails(t *testing.T) {
failingMatch := expectPositive(true).BeFalse()
testTrue(t, failingMatch.failure == true)
testTrue(t, failingMatch.message == "Expected true to be false.")
}
func TestBeGreaterThanSucceeds(t *testing.T) {
testTrue(t, expectPositive(1).BeGreaterThan(0).failure == false)
testTrue(t, expectNegative(1).BeGreaterThan(0).failure == true)
}
func TestBeGreaterThanFails(t *testing.T) {
failingMatch := expectPositive(0).BeGreaterThan(1)
testTrue(t, failingMatch.failure == true)
testTrue(t, failingMatch.message == "Expected 0 to be greater than 1.")
}
func TestBeLessThanSucceeds(t *testing.T) {
testTrue(t, expectPositive(0).BeLessThan(1).failure == false)
testTrue(t, expectNegative(0).BeLessThan(1).failure == true)
}
func TestBeLessThanFails(t *testing.T) {
failingMatch := expectPositive(1).BeLessThan(0)
testTrue(t, failingMatch.failure == true)
testTrue(t, failingMatch.message == "Expected 1 to be less than 0.")
}
func TestContainSucceeds(t *testing.T) {
testTrue(t, expectPositive("a").Contain("a").failure == false)
testTrue(t, expectNegative("a").Contain("a").failure == true)
}
func TestContainFails(t *testing.T) {
failingMatch := expectPositive("a").Contain("b")
testTrue(t, failingMatch.failure == true)
testTrue(t, failingMatch.message == "Expected a to contain b.")
}
func TestContainWorksWithSlices(t *testing.T) {
testTrue(t, expectPositive([]string{"a"}).Contain("a").failure == false)
}