| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mlog |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/json" |
| 10 |
"io" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"gotest.tools/v3/assert" |
| 14 |
is "gotest.tools/v3/assert/cmp" |
| 15 |
) |
| 16 |
|
| 17 |
var jsonStringTests = map[string]string{ |
| 18 |
"generic": `test`, |
| 19 |
"quote": `"this"`, |
| 20 |
"r&n": "te\r\nst", |
| 21 |
"tab": "\t what", |
| 22 |
"weird chars": "\u2028 \u2029", |
| 23 |
"other weird chars": `"\u003c\u0026\u003e"`, |
| 24 |
"invalid utf8": "\xff\xff\xffhello", |
| 25 |
} |
| 26 |
|
| 27 |
func TestFormatWriterJSONEncodeStringMap(t *testing.T) { |
| 28 |
b := &bytes.Buffer{} |
| 29 |
for name, s := range jsonStringTests { |
| 30 |
e, err := json.Marshal(s) |
| 31 |
assert.Check(t, err, "%s: json marshal failed", name) |
| 32 |
|
| 33 |
b.Truncate(0) |
| 34 |
b.WriteByte('"') |
| 35 |
encodeStringJSON(b, s) |
| 36 |
b.WriteByte('"') |
| 37 |
assert.Check(t, is.Equal(string(e), b.String()), "%s: did not match expectation", name) |
| 38 |
} |
| 39 |
} |
| 40 |
func TestFormatWriterJSONAttrsNil(t *testing.T) { |
| 41 |
logger := New(io.Discard, 0) |
| 42 |
logWriter := &FormatWriterJSON{} |
| 43 |
attrs := make([]*Attr, 10) |
| 44 |
for i := 1; i <= 5; i++ { |
| 45 |
attrs = append(attrs, &Attr{randString(6, false), randString(10, false)}) |
| 46 |
} |
| 47 |
for i := 0; i < len(attrs); i++ { |
| 48 |
logWriter.EmitAttrs(logger, 0, "this is a test", attrs...) |
| 49 |
} |
| 50 |
} |