-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter_test.go
More file actions
79 lines (69 loc) · 1.92 KB
/
formatter_test.go
File metadata and controls
79 lines (69 loc) · 1.92 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
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package logx_test
import (
"bytes"
"testing"
"time"
"go.osspkg.com/casecheck"
"go.osspkg.com/logx"
)
func TestUnit_FormatString_Encode(t *testing.T) {
tests := []struct {
name string
args *logx.Message
want []byte
wantErr bool
}{
{
name: "Case1",
args: &logx.Message{
Time: time.Now(),
Level: "INF",
Message: "Hello",
Ctx: []interface{}{
"err", "err\nmsg",
},
},
want: []byte("\"level\"=\"INF\"\t\"msg\"=\"Hello\"\t\"err\"=\"err\\nmsg\""),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var w bytes.Buffer
fo := logx.NewFormatString()
err := fo.Encode(&w, tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("Encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
got := w.Bytes()
if !bytes.Contains(got, tt.want) {
t.Errorf("Encode() got = %v, want %v", string(got), string(tt.want))
}
})
}
}
func TestUnit_debug(t *testing.T) {
t.SkipNow()
var w bytes.Buffer
fj := logx.NewFormatJSON()
fj.Encode(&w, &logx.Message{Ctx: []any{"a\"\na", "a\nb\n"}, Map: make(map[string]string)})
fj.Encode(&w, &logx.Message{Message: "a\"\nb\n"})
fj.Encode(&w, &logx.Message{})
fs := logx.NewFormatString()
fs.Encode(&w, &logx.Message{Ctx: []any{"a\"\na", "a\nb\n"}})
fs.Encode(&w, &logx.Message{Message: "a\"\nb\n"})
fs.Encode(&w, &logx.Message{})
result := string(w.Bytes())
wait := `{"time":"0001-01-01T00:00:00Z","level":"","msg":""}
{"time":"0001-01-01T00:00:00Z","level":"","msg":""}
{"time":"0001-01-01T00:00:00Z","level":"","msg":""}
"time"="0001-01-01T00:00:00Z" "level"="" "msg"="" "a\na"="a\nb\n"
"time"="0001-01-01T00:00:00Z" "level"="" "msg"="a\nb\n"
`
casecheck.Equal(t, wait, result)
}