Repository navigation
Expand file tree
/
Copy pathjson_test.go
More file actions
148 lines (125 loc) · 3.67 KB
/
Copy pathjson_test.go
File metadata and controls
148 lines (125 loc) · 3.67 KB
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
package logg
import (
"bytes"
stdjson "encoding/json"
"strings"
"testing"
)
func TestLogg_getJson(t *testing.T) {
js := newJson()
if js == nil {
t.Error("js object is nil")
t.FailNow()
}
if len(js.buf) == 0 {
t.Error("js buffer is empty, but must contain a opening bracket '{'")
}
}
func TestLogg_json_close(t *testing.T) {
t.Run("empty", func(t *testing.T) {
js := newJson()
js.close()
if string(js.buf) != `{}` {
t.Errorf("empty json must be {}, received: %s", string(js.buf))
}
})
t.Run("single field", func(t *testing.T) {
js := newJson()
js.addString("key", "value")
js.close()
if string(js.buf) != `{"key": "value"}` {
t.Errorf("received: %s", string(js.buf))
}
})
}
func TestLogg_json_put(t *testing.T) {
js := &json{
buf: make([]byte, 1<<16+1),
}
js.close()
js.put()
jsonFromPool := jsonPool.Get().(*json)
if len(jsonFromPool.buf) >= 1<<16 {
t.Error("json has a buf size maxSize+1, cannot be saved in sync.Pool")
}
}
func TestLogg_appendEscaped(t *testing.T) {
tests := map[string]struct {
in string
expected string
}{
"plain": {in: "hello world", expected: "hello world"},
"double quote": {in: `he said "hi"`, expected: `he said \"hi\"`},
"backslash": {in: `a\b`, expected: `a\\b`},
"newline": {in: "line1\nline2", expected: `line1\nline2`},
"carriage tab": {in: "a\r\tb", expected: `a\r\tb`},
"control char": {in: "a\x00b", expected: "a\\u0000b"},
"utf8 passthru": {in: "héllo", expected: "héllo"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := string(appendEscaped(nil, []byte(tc.in)))
if got != tc.expected {
t.Errorf("wrong escaping. Expected: %s, received: %s", tc.expected, got)
}
})
}
}
func TestLogg_buildJSON_valid(t *testing.T) {
m := newMessage(InfoLevel, 0, 0, Json, false)
out := m.build([]byte(`message with "quotes", a \backslash and
a newline`))
// trailing '\n' is added by build for the writer; trim it before parsing.
out = bytes.TrimRight(out, "\n")
var decoded map[string]string
if err := stdjson.Unmarshal(out, &decoded); err != nil {
t.Fatalf("JSON output is not valid: %v\noutput: %s", err, out)
}
if decoded["level"] != "INF" {
t.Errorf("wrong level field: %q", decoded["level"])
}
if !strings.Contains(decoded["message"], `"quotes"`) {
t.Errorf("message field lost its content: %q", decoded["message"])
}
}
func TestLogg_json_fields(t *testing.T) {
js := newJson()
js.addString("a", "1")
js.addBytes("b", []byte("2"))
js.addValue("n", 42)
js.addValue("flag", true)
js.close()
want := `{"a": "1", "b": "2", "n": 42, "flag": true}`
if string(js.buf) != want {
t.Errorf("wrong json. Expected: %s, received: %s", want, string(js.buf))
}
}
func TestLogg_appendJSONValue(t *testing.T) {
tests := map[string]struct {
in interface{}
expected string
}{
"nil": {in: nil, expected: "null"},
"string": {in: `a "b"`, expected: `"a \"b\""`},
"bytes": {in: []byte("xy"), expected: `"xy"`},
"int": {in: -7, expected: "-7"},
"uint": {in: uint(7), expected: "7"},
"float": {in: 1.5, expected: "1.5"},
"bool": {in: false, expected: "false"},
"error": {in: errSentinel, expected: `"boom"`},
"stringer": {in: stringerVal{}, expected: `"S"`},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := string(appendJSONValue(nil, tc.in))
if got != tc.expected {
t.Errorf("appendJSONValue(%v): expected %s, received %s", tc.in, tc.expected, got)
}
})
}
}
var errSentinel = stringError("boom")
type stringError string
func (e stringError) Error() string { return string(e) }
type stringerVal struct{}
func (stringerVal) String() string { return "S" }