forked from visionmedia/go-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_formatter_test.go
112 lines (80 loc) · 3.27 KB
/
json_formatter_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
package debug
import (
"encoding/json"
"errors"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrorNotLost(t *testing.T) {
SetHasColors(false)
SetFormatter(&JSONFormatter{})
s := formatter.Format(*Debug("error_not_lost").WithField("error", errors.New("wild walrus")), "hi")
entry := make(map[string]interface{})
err := json.Unmarshal([]byte(s), &entry)
assert.Nil(t, err, "Unable to unmarshal formatted entry")
assert.Equal(t, entry["error"], "wild walrus")
assert.Equal(t, entry["msg"], "hi")
assert.NotEmpty(t, entry["delta"])
assert.NotEmpty(t, entry["time"])
assert.NotEmpty(t, entry["namespace"])
}
func TestErrorNotLostOnFieldNotNamedError(t *testing.T) {
SetHasColors(false)
SetFormatter(&JSONFormatter{})
s := formatter.Format(*Debug("mapped_field_error").WithField("omg", errors.New("wild walrus")), "hi")
entry := make(map[string]interface{})
err := json.Unmarshal([]byte(s), &entry)
assert.Nil(t, err, "Unable to unmarshal formatted entry")
assert.Equal(t, entry["omg"], "wild walrus")
assert.Equal(t, entry["msg"], "hi")
assert.NotEmpty(t, entry["delta"])
assert.NotEmpty(t, entry["time"])
assert.NotEmpty(t, entry["namespace"])
}
func TestFieldClashWithTime(t *testing.T) {
SetHasColors(false)
SetFormatter(&JSONFormatter{})
s := formatter.Format(*Debug("clash_time").WithField("time", "right now!"), "hi")
entry := make(map[string]interface{})
err := json.Unmarshal([]byte(s), &entry)
assert.Nil(t, err, "Unable to unmarshal formatted entry")
assert.NotEqual(t, entry["fields.time"], "right now!", "fields.time not set to original time field")
r := regexp.MustCompile(`\d\d:\d\d:\d\d\.(\d*)`)
ss := (entry["time"]).(string)
assert.Equal(t, len(r.FindStringSubmatch(ss)), 2, "time check")
}
func TestFieldClashWithMsg(t *testing.T) {
SetHasColors(false)
SetFormatter(&JSONFormatter{})
s := formatter.Format(*Debug("clash_msg").WithField("msg", errors.New("wild walrus")), "hi")
entry := make(map[string]interface{})
err := json.Unmarshal([]byte(s), &entry)
assert.Nil(t, err, "Unable to unmarshal formatted entry")
assert.Equal(t, entry["msg"], "hi")
}
func TestFieldClashWithNamespace(t *testing.T) {
SetHasColors(false)
SetFormatter(&JSONFormatter{})
s := formatter.Format(*Debug("clash_namespace").WithField("namespace", errors.New("wild walrus")), "hi")
entry := make(map[string]interface{})
err := json.Unmarshal([]byte(s), &entry)
assert.Nil(t, err, "Unable to unmarshal formatted entry")
assert.Equal(t, entry["namespace"], "clash_namespace", "namespace is correct")
}
func TestJSONEntryEndsWithNewline(t *testing.T) {
SetFormatter(&JSONFormatter{})
s := formatter.Format(*Debug("newline").WithField("dog", errors.New("wild walrus")), "hi")
entry := make(map[string]interface{})
err := json.Unmarshal([]byte(s), &entry)
assert.Nil(t, err, "Unable to unmarshal formatted entry")
assert.Equal(t, "\n", string(s[len(s)-1]), "Expected JSON log entry to end with a newline")
}
func TestJSONPretty(t *testing.T) {
HAS_TIME = false
SetHasColors(false)
SetFormatter(&JSONFormatter{PrettyPrint: true})
s := formatter.Format(*Debug("pretty").WithField("dog", "wild walrus"), "hi")
expected := "{\n \"dog\": \"wild walrus\",\n \"msg\": \"hi\",\n \"namespace\": \"pretty\"\n}\n"
assert.Equal(t, expected, s, "is pretty")
}