-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock_test.go
90 lines (81 loc) · 1.96 KB
/
mock_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
package vermock_test
import (
"testing"
vermock "github.com/Versent/go-vermock"
)
func TestNew_identity(t *testing.T) {
t.Run("mockCache", func(t *testing.T) {
m1 := vermock.New[mockCache](t)
m2 := vermock.New[mockCache](t)
if m1 == m2 {
t.Error("expected different mocks")
}
})
t.Run("vermock.Delegates", func(t *testing.T) {
type T vermock.Delegates
m1 := vermock.New[T](t)
m2 := vermock.New[T](t)
if m1 == m2 {
t.Error("expected different mocks")
}
})
t.Run("zero-sized", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected panic")
} else if r != "vermock.New: zero-sized type used to construct more than one mock: *vermock_test.T" {
t.Error("unexpected panic:", r)
}
}()
type T struct{}
_ = vermock.New[T](t)
_ = vermock.New[T](t)
})
}
func TestNew_Expect(t *testing.T) {
called := false
var cache Cache = vermock.New(&testing.T{},
vermock.Expect[mockCache]("Put", func(_ testing.TB, key string, value any) error {
if key != "foo" && value != "bar" {
t.Error("unexpected arguments")
}
called = true
return nil
}),
vermock.Expect[mockCache]("Get", func(_ *testing.T, key string) (any, bool) {
if key != "foo" {
t.Error("unexpected arguments")
}
called = true
return "bar", true
}),
vermock.Expect[mockCache]("Delete", func(key string) {
if key != "foo" {
t.Error("unexpected arguments")
}
called = true
}),
ExpectDelete(func(_ testing.TB, key string) {
t.Error("this should not be called")
}),
)
called = false
if err := cache.Put("foo", "bar"); err != nil {
t.Error("unexpected error:", err)
}
if !called {
t.Error("expected call to Put delegate")
}
called = false
if result, ok := cache.Get("foo"); result != "bar" && ok {
t.Error("unexpected result")
}
if !called {
t.Error("expected call to Get delegate")
}
called = false
cache.Delete("foo")
if !called {
t.Error("expected call to Delete delegate")
}
}