-
Notifications
You must be signed in to change notification settings - Fork 1
/
expectation_test.go
116 lines (100 loc) · 3.68 KB
/
expectation_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
package bintest
import (
"reflect"
"testing"
"github.com/buildkite/bintest/v3/testutil"
)
func TestMatchExpectations(t *testing.T) {
var exp = ExpectationSet{
{name: "test", arguments: Arguments{"llamas", "rock"}, minCalls: 1, maxCalls: 5},
{name: "test", arguments: Arguments{"llamas", "are", "ok"}, minCalls: 1, maxCalls: 5},
{name: "blargh", arguments: Arguments{"alpacas", "too"}, minCalls: 1, maxCalls: 5},
}
match, err := exp.ForArguments("llamas", "are", "ok").Match()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(exp[1], match) {
t.Fatalf("Got unexpected %#v", match)
}
}
func TestMatchOverlappingExpectations(t *testing.T) {
var exp = ExpectationSet{
{name: "test", arguments: Arguments{"llamas", "rock"}, totalCalls: 1, minCalls: 1, maxCalls: 1},
{name: "blargh", arguments: Arguments{"alpacas", "too"}, minCalls: 1, maxCalls: 5},
{name: "test", arguments: Arguments{"llamas", "rock"}, minCalls: 1, maxCalls: 5},
}
match, err := exp.ForArguments("llamas", "rock").Match()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(exp[2], match) {
t.Fatalf("Got unexpected %#v", match)
}
}
func TestExplainExpectationMatch(t *testing.T) {
var exp = ExpectationSet{
{name: "test", arguments: Arguments{"llamas", "rock"}, totalCalls: 1, minCalls: 1, maxCalls: 1},
{name: "blargh", arguments: Arguments{"alpacas"}, minCalls: 1, maxCalls: 5},
{name: "test", arguments: Arguments{"llamas", "rock"}, minCalls: 1, maxCalls: 5},
}
actual := exp.ForArguments("llamas", "ro").ClosestMatch().Explain()
expected := `Argument #2 doesn't match: Differs at character 3, expected "ck", got ""`
if actual != expected {
t.Fatalf("Wrong explanation, got %s, wanted %s", actual, expected)
}
}
func TestCheckIndividualExpectations(t *testing.T) {
var match = []*Expectation{
{name: "test", arguments: Arguments{"llamas", "rock"}, totalCalls: 1, minCalls: 1, maxCalls: 1},
{name: "blargh", arguments: Arguments{"alpacas", "too"}, totalCalls: 1, minCalls: 1, maxCalls: 5},
{name: "test", arguments: Arguments{"llamas", "rock"}, totalCalls: 10, minCalls: 1, maxCalls: InfiniteTimes},
}
for _, exp := range match {
if !exp.Check(t) {
t.Fatalf("Expected %s to match", exp)
}
}
var notMatch = []*Expectation{
{name: "test", arguments: Arguments{"llamas", "rock"}, totalCalls: 0, minCalls: 1, maxCalls: 1},
{name: "blargh", arguments: Arguments{"alpacas", "too"}, totalCalls: 10, minCalls: 1, maxCalls: 5},
{name: "test", arguments: Arguments{"llamas", "rock"}, totalCalls: 0, minCalls: 1},
}
for _, exp := range notMatch {
if exp.Check(&testutil.TestingT{}) {
t.Fatalf("Expected %s to NOT match", exp)
}
}
}
func TestExpectionWithStdin(t *testing.T) {
testCases := []struct {
label string
stdin interface{}
readStdin []byte
expect bool
}{
{"string match", "hello", []byte("hello"), true},
{"string mismatch", "hello", []byte("world"), false},
{"MatchPattern match", MatchPattern("lo$"), []byte("hello"), true},
{"MatchPattern mismatch", MatchPattern("^lo"), []byte("hello"), false},
{"MatchAny", MatchAny(), []byte("hello"), true},
{"MatchAny nil readStdin", MatchAny(), nil, true},
{"string match nil readStdin", "nope", nil, false},
}
for _, tc := range testCases {
t.Run(tc.label, func(t *testing.T) {
exp := Expectation{stdin: tc.stdin, readStdin: tc.readStdin}
fakeT := &testutil.TestingT{}
if exp.Check(fakeT) != tc.expect {
t.Errorf("expected Check() to be %v for %s", tc.expect, tc.label)
}
expectedLogs := 0
if tc.expect == false {
expectedLogs = 1
}
if len(fakeT.Logs) != expectedLogs {
t.Errorf("expected %d errors when Check() is %v, got %d", expectedLogs, tc.expect, len(fakeT.Logs))
}
})
}
}