-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper_test.go
61 lines (51 loc) · 1.35 KB
/
helper_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
package venom
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
// TestLogger has no-op ReadLog WriteLog methods
type TestLogger struct{}
func (tl *TestLogger) LogWrite(level ConfigLevel, key string, val interface{}) {}
func (tl *TestLogger) LogRead(key string, val interface{}, bl bool) {}
// kv is a test struct containing a (k)ey and a (v)alue
type kv struct {
k string
v interface{}
}
// lkv is a test struct containing a config (l)evel, a (k)ey, and a (v)alue
type lkv struct {
l ConfigLevel
k string
v interface{}
}
func assertEqualErrors(t *testing.T, expect, actual error) {
var msg string
if actual != nil {
msg = actual.Error()
}
assert.Equal(t, expect, actual, msg)
}
// redirectStdout is explicitly for TestNewLoggable test to pipe the contents
// sent to os.Stdout when implementing using the default logger.
func redirectStdout(test struct {
tc string
f func() *Venom
log bool
kv kv
}) string {
// make pipe to stdout and defer resetting
realStdout := os.Stdout
defer func() { os.Stdout = realStdout }()
r, fakeStdout, _ := os.Pipe()
os.Stdout = fakeStdout
// run test case with fake stdout capture
l := test.f()
l.SetDefault(test.kv.k, test.kv.v)
// close up pipe, return string, exec defer
fakeStdout.Close()
newOutBytes, _ := ioutil.ReadAll(r)
r.Close()
return string(newOutBytes)
}