-
Notifications
You must be signed in to change notification settings - Fork 0
/
logc_test.go
53 lines (49 loc) · 1.29 KB
/
logc_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
package main
import (
"bufio"
"strings"
"testing"
"time"
"fortio.org/log"
)
func TestGetAttributes(t *testing.T) {
for _, tc := range []struct {
in string
want string
}{
{`{"msg":"foo"}`, ""},
{`{"msg":"foo","attr1":"val\"with quote"}`, `: "attr1":"val\"with quote"`},
{`{"msg":"foo\\"}`, ``},
{`{"msg":"foo\\","a1":"v1"}`, `: "a1":"v1"`},
{`{"msg":"hello \"world\"","a1":"v\n1,"a2":"v2"}`, `: "a1":"v\n1,"a2":"v2"`},
} {
got := GetAttributes(tc.in)
if got != tc.want {
t.Errorf("GetAttributes(%s) = '%s', want '%s'", tc.in, got, tc.want)
}
}
}
func TestLevels(t *testing.T) {
var zeroTime time.Time
log.Config.ForceColor = true
log.SetColorMode()
left := log.Colors.DarkGray + "["
right := log.Colors.DarkGray + "]"
for _, tc := range []struct {
in string
want string
}{
{`{"level":"trace","msg":"foo"}`, left + log.Colors.Cyan + "VRB" + right + "> " +
log.Colors.Cyan + "foo" + log.Colors.Reset + "\n"},
{`{"level":"xyz","msg":"foo"}`, log.Colors.BrightRed + "?> " + log.Colors.Blue + "foo" + log.Colors.Reset + "\n"},
} {
buf := &strings.Builder{}
w := bufio.NewWriter(buf)
ProcessLogLine(w, &zeroTime, []byte(tc.in))
w.Flush()
got := buf.String()
if got != tc.want {
t.Errorf("LevelToColor(%s) = '%s', want '%s'", tc.in, got, tc.want)
}
}
}