/
context_test.go
198 lines (158 loc) · 3.94 KB
/
context_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2013 Masterminds
// This package provides the execution context for a Cookoo request.
package cookoo
import (
"bytes"
"log"
"reflect"
"regexp"
"runtime"
"testing"
)
// An example datasource as can add to our store.
type ExampleDatasource struct {
name string
}
// A simple equal function.
func equal(t *testing.T, a interface{}, b interface{}) {
result := reflect.DeepEqual(a, b)
if !result {
_, file, line, _ := runtime.Caller(1)
t.Errorf("Failed equals in %s:%d", file, line)
}
}
func TestDatasource(t *testing.T) {
foo := new(ExampleDatasource)
foo.name = "bar"
cxt := NewContext()
cxt.AddDatasource("foo", foo)
foo2 := cxt.Datasource("foo").(*ExampleDatasource)
equal(t, foo, foo2)
equal(t, "bar", foo2.name)
cxt.RemoveDatasource("foo")
equal(t, nil, cxt.Datasource("foo"))
}
func TestAddGet(t *testing.T) {
cxt := NewContext()
cxt.Put("test1", 42)
cxt.Put("test2", "Geronimo!")
cxt.Put("test3", func() string { return "Hello" })
// Test Get
equal(t, 42, cxt.Get("test1", nil))
equal(t, "Geronimo!", cxt.Get("test2", nil))
// Test has
val, ok := cxt.Has("test1")
if !ok {
t.Error("! Failed to get 'test1'")
}
equal(t, 42, cxt.Get("test1", nil))
_, ok = cxt.Has("test999")
if ok {
t.Error("! Unexpected result for 'test999'")
}
val, ok = cxt.Has("test3")
fn := val.(func() string)
if ok {
equal(t, "Hello", fn())
} else {
t.Error("! Expected a function.")
}
m := cxt.AsMap()
if m["test1"] != 42 {
t.Error("! Error retrieving context as a map.")
}
}
type LameStruct struct {
stuff []string
}
func TestCopy(t *testing.T) {
lame := new(LameStruct)
lame.stuff = []string{"O", "Hai"}
c := NewContext()
c.Put("a", lame)
c.Put("b", "This is the song that never ends")
foo := new(ExampleDatasource)
foo.name = "bar"
c.AddDatasource("foo", foo)
c2 := c.Copy()
c.Put("c", 1234)
if c.Len() != 3 {
t.Error("! Canary failed. c should be 3")
}
if c2.Len() != 2 {
t.Error("! c2 should be 2.")
}
c.Put("b", "FOO")
if c2.Get("b", nil) == "FOO" {
t.Error("! b should not have changed in C2.")
}
lame.stuff[1] = "Noes"
v1 := c2.Get("a", nil).(*LameStruct)
if v1.stuff[1] != "Noes" {
t.Error("! Expected shallow copy of array. Got ", v1)
}
d2 := new(ExampleDatasource)
d2.name = "bar"
c.AddDatasource("d2", d2)
_, found := c2.HasDatasource("d2")
if found == true {
t.Error("! Datasource inadvertently copied from one context to another.")
}
}
func TestLogging(t *testing.T) {
logger := new(bytes.Buffer)
c := NewContext()
c.AddLogger("test", logger)
foo, found := c.Logger("test")
if found == false {
t.Error("! Logger not found.")
}
if foo != logger {
t.Error("! Loggers do not match.")
}
logger2 := new(bytes.Buffer)
c.AddLogger("test2", logger2)
c.(*ExecutionContext).SkipLogPrefix("tinker", "bell")
log.Print("a test")
line := logger.String()
line = line[0 : len(line)-1]
pattern := "a test$"
matched, err := regexp.MatchString(pattern, line)
if err != nil {
t.Fatal("! Regex Pattern did not compile:", err)
}
if !matched {
t.Errorf("! Message was not logged to first test logger: %q", line)
}
logger2.Reset()
c.Log("foo", "this is a test")
line = logger2.String()
line = line[0 : len(line)-1]
pattern = "^foo.*this is a test$"
matched, err = regexp.MatchString(pattern, line)
if err != nil {
t.Fatal("! Regex Pattern did not compile:", err)
}
if !matched {
t.Errorf("! Log to second logger did not happen correctly: %q", line)
}
logger.Reset()
c.Logf("tinker", "ignore")
c.Logf("bar", "foo %d baz", 2)
c.Log("bell", "ignore")
line = logger.String()
line = line[0 : len(line)-1]
pattern = "^bar.*foo 2 baz$"
matched, err = regexp.MatchString(pattern, line)
if err != nil {
t.Fatal("! pattern did not compile:", err)
}
if !matched {
t.Errorf("! Logf to first logger did now happen correctly: %q", line)
}
c.RemoveLogger("test")
_, found = c.Logger("test")
if found == true {
t.Error("! Logger found but should have been removed.")
}
}