-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoan_test.go
80 lines (66 loc) · 1.57 KB
/
koan_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
package koan
import (
"bytes"
"errors"
"fmt"
"github.com/TwiN/go-color"
"log"
"os"
"strings"
"testing"
)
var tl *Logger
func TestLogger_Info(t *testing.T) {
want := INFOCOLOR + "INFO: Test info message" + color.Reset
tl = createTestLogger()
got := captureOutput(func() {
tl.Info("Test info message")
})
assertEqual(t, got, want)
}
func TestLogger_Warn(t *testing.T) {
want := WARNCOLOR + "WARN: Test warning message" + color.Reset
tl = createTestLogger()
got := captureOutput(func() {
tl.Warn("Test warning message")
})
assertEqual(t, got, want)
}
func TestLogger_Debug(t *testing.T) {
want := DEBUGCOLOR + "DEBUG: Test debug message" + color.Reset
tl = createTestLogger()
tl.DEBUG = true
got := captureOutput(func() {
tl.Debug("Test debug message")
})
assertEqual(t, got, want)
}
func TestLogger_Error(t *testing.T) {
err := errors.New("test error message")
msg := fmt.Sprintf("%s (%v)", "ERROR: Test message", err)
want := ERRCOLOR + msg + color.Reset
tl = createTestLogger()
got := captureOutput(func() {
tl.Error("Test message", err)
})
assertEqual(t, got, want)
}
// test helpers
func createTestLogger() *Logger {
return &Logger{}
}
func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
output := strings.Split(buf.String(), " ")
// remove timestamp & newline
return strings.TrimSuffix(strings.Join(output[2:], " "), "\n")
}
func assertEqual(t *testing.T, got, want string) {
if got != want {
fmt.Printf("want: %q, got: %q", want, got)
t.Errorf("Fail wanted %s got %s", want, got)
}
}