-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
78 lines (65 loc) · 1.59 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
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
package cogol
import (
"fmt"
"testing"
)
type Context struct {
storage Storage
logger Logger
test *test
t *testing.T
succeeded chan bool
failed chan string
killed bool
assertionKiller func(f *failure)
}
// context creates a new cgl.Context instance
func (cgl Cogol) context(test *test, logger Logger, storage Storage) *Context {
return &Context{
test: test,
storage: storage,
logger: logger,
succeeded: make(chan bool),
failed: make(chan string),
t: cgl.t,
killed: false,
assertionKiller: defaultKiller,
}
}
// Kill marks test as failed and stops it's processing
func (ctx *Context) Kill(message string) {
// Can not kill context more than once, so if ctx.Kill() has been called
// more than once, we can just ignore it since the test has failed anyway
if ctx.killed {
return
}
f := &failure{
ctx: ctx,
msg: message,
}
ctx.test.f = f
ctx.failed <- fmt.Sprintf("Killed '%v'", ctx.test.name)
ctx.killed = true
}
func (ctx *Context) Log() Logger {
return ctx.logger
}
func (ctx *Context) Storage() Storage {
return ctx.storage
}
type Storage interface {
Get(key string) interface{}
Set(key string, value interface{})
}
type defaultStorage struct {
data map[string]interface{}
}
func newDefaultStorage() *defaultStorage {
return &defaultStorage{map[string]interface{}{}}
}
func (s *defaultStorage) Get(key string) interface{} {
return s.data[key]
}
func (s *defaultStorage) Set(key string, value interface{}) {
s.data[key] = value
}