-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_test.go
52 lines (48 loc) · 1.1 KB
/
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
/*
* Copyright (c) 2024 Mikhail Knyazhev <markus621@yandex.ru>. 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/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))
}
})
}
}