-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matcher_test.go
68 lines (64 loc) · 1.81 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
package gotest
import (
"fmt"
tst "testing"
)
func TestFuncMatcherMatch(t *tst.T) {
c := Use(t)
var gaveContext TestContext = &fakeTestContext{}
var gotContext TestContext
var gotAssumption bool
var gotValue int
var descriptorArg int
var matcherCallCount int
var descriptorCallCount int
funcMatcher := FuncMatcher[int] {
Func: func(context TestContext, assumption bool, value int, descriptor Descriptor[int]) {
matcherCallCount++
gotContext = context
gotAssumption = assumption
gotValue = value
descriptor(1337)
},
}
funcMatcher.Match(
gaveContext,
true,
42,
func(describee int) string {
descriptorCallCount++
descriptorArg = describee
return fmt.Sprintf("describing %d", describee)
},
)
AssertThat(c, gotContext).Is(EqualTo(gaveContext))
AssertThat(c, gotAssumption).Is(EqualTo(true))
AssertThat(c, gotValue).Is(EqualTo(42))
AssertThat(c, descriptorArg).Is(EqualTo(1337))
AssertThat(c, matcherCallCount).Is(EqualTo(1))
AssertThat(c, descriptorCallCount).Is(EqualTo(1))
}
func TestFuncMatcherMatchWithNilCallback(t *tst.T) {
c := Use(t)
var gaveContext TestContext = &fakeTestContext{}
funcMatcher := FuncMatcher[int] {}
descriptor := func(int) string {
return ""
}
AssertThat(c, func() {
funcMatcher.Match(gaveContext, false, 42, descriptor)
}).Will(PanicWithValue("No matcher callback provided", WithTheString))
}
func TestDoDescribe(t *tst.T) {
c := Use(t)
AssertThat(c, func() {
doDescribe(0, nil)
}).Will(PanicWithValue("No descriptor provided; this indicates an incorrect usage of a matcher", WithTheString))
var descriptorCallCount int
descriptor := func(value int) string {
descriptorCallCount++
return fmt.Sprintf("the int %d", value)
}
AssertThat(c, doDescribe(42, descriptor)).Is(EqualTo("the int 42"))
AssertThat(c, descriptorCallCount).Is(EqualTo(1))
}