-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmock.go
111 lines (98 loc) · 2.65 KB
/
mock.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package servicetest
import (
"fmt"
"sync"
)
// Tape holds a record of function call stimuli and each function call's
// response. Use Tape to help build a mock framework by first creating a
// new Tape, then SetResponses to define the mock responses and then
// Record each function invocation. Play returns the function invocations
// for verification in a test.
type Tape struct {
sync.Mutex
stimuli []interface{}
responses []interface{}
}
// Record stores a new function invocation in a Tape and returns the
// response for that function interface.
func (t *Tape) Record(call interface{}) interface{} {
t.Lock()
defer t.Unlock()
t.stimuli = append(t.stimuli, call)
if len(t.responses) < 1 {
// Returning an error at this point will likely cause the mock
// using the tape to panic when it tries to cast the response
// to the desired type.
// Panic'ing here at least makes the issue more
// apparent.
// TODO(caprita): Don't panic.
panic(fmt.Errorf("Record(%#v) had no response", call))
}
resp := t.responses[0]
t.responses = t.responses[1:]
return resp
}
// SetResponses updates the Tape's associated responses.
func (t *Tape) SetResponses(responses ...interface{}) {
t.Lock()
defer t.Unlock()
t.responses = make([]interface{}, len(responses))
copy(t.responses, responses)
}
// Rewind resets the tape to the beginning so that it could be used again
// for further tests.
func (t *Tape) Rewind() {
t.Lock()
defer t.Unlock()
t.stimuli = make([]interface{}, 0)
t.responses = make([]interface{}, 0)
}
// Play returns the function call stimuli recorded to this Tape.
func (t *Tape) Play() []interface{} {
t.Lock()
defer t.Unlock()
resp := make([]interface{}, len(t.stimuli))
copy(resp, t.stimuli)
return resp
}
// NewTape creates a new Tape.
func NewTape() *Tape {
t := new(Tape)
t.Rewind()
return t
}
// TapeMap provides multiple tapes for different strings. Use TapeMap to
// record separate Tapes for each suffix in a service.
type TapeMap struct {
sync.Mutex
tapes map[string]*Tape
}
// NewTapeMap creates a new empty TapeMap.
func NewTapeMap() *TapeMap {
tm := &TapeMap{
tapes: make(map[string]*Tape),
}
return tm
}
// ForSuffix returns the Tape for suffix s.
func (tm *TapeMap) ForSuffix(s string) *Tape {
tm.Lock()
defer tm.Unlock()
t, ok := tm.tapes[s]
if !ok {
t = new(Tape)
tm.tapes[s] = t
}
return t
}
// Rewind rewinds all of the Tapes in the TapeMap.
func (tm *TapeMap) Rewind() {
tm.Lock()
defer tm.Unlock()
for _, t := range tm.tapes {
t.Rewind()
}
}