-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
53 lines (44 loc) · 1.05 KB
/
context.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
package testcase
import (
"sync"
"testing"
)
type context struct {
c interface{}
callbacks map[string]callback
}
func newContext(c interface{}, callbackList []callbackItem) *context {
callbacks := make(map[string]callback)
for _, callbackItem := range callbackList {
callbacks[callbackItem.ID] = callbackItem.Value
}
return &context{
c: c,
callbacks: callbacks,
}
}
func (c *context) C() interface{} { return c.c }
func (c *context) GetCallback(callbackID string) (callback, bool) {
callback, ok := c.callbacks[callbackID]
return callback, ok
}
var (
contextsLock sync.Mutex
contexts map[*testing.T]*context = make(map[*testing.T]*context)
)
func setContext(t *testing.T, context *context) {
contextsLock.Lock()
defer contextsLock.Unlock()
contexts[t] = context
}
func clearContext(t *testing.T) {
contextsLock.Lock()
defer contextsLock.Unlock()
delete(contexts, t)
}
func getContext(t *testing.T) (*context, bool) {
contextsLock.Lock()
defer contextsLock.Unlock()
context, ok := contexts[t]
return context, ok
}