-
Notifications
You must be signed in to change notification settings - Fork 23
/
formatter_test.go
86 lines (73 loc) · 2.26 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
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
package logging
import (
"fmt"
"github.com/hhkbp2/testify/require"
"testing"
)
var (
testRecord = NewLogRecord(
"name",
LevelInfo,
"pathname",
"filename",
111,
"funcname",
"",
false,
[]interface{}{"message"})
)
func WithLineFeed(s string) string {
return s + "\n"
}
func TestFormat_Name(t *testing.T) {
formatter := NewStandardFormatter("%(name)s", "")
require.Equal(t,
WithLineFeed(testRecord.Name), formatter.Format(testRecord))
}
func TestFormat_LevelNo(t *testing.T) {
formatter := NewStandardFormatter("%(levelno)d", "")
levelNo := WithLineFeed(fmt.Sprintf("%d", testRecord.Level))
require.Equal(t, levelNo, formatter.Format(testRecord))
}
func TestFormat_LevelName(t *testing.T) {
formatter := NewStandardFormatter("%(levelname)s", "")
levelName := WithLineFeed(GetLevelName(testRecord.Level))
require.Equal(t, levelName, formatter.Format(testRecord))
}
func TestFormat_PathName(t *testing.T) {
formatter := NewStandardFormatter("%(pathname)s", "")
require.Equal(t,
WithLineFeed(testRecord.PathName), formatter.Format(testRecord))
}
func TestFormat_FileName(t *testing.T) {
formatter := NewStandardFormatter("%(filename)s", "")
require.Equal(t,
WithLineFeed(testRecord.FileName), formatter.Format(testRecord))
}
func TestFormat_LineNo(t *testing.T) {
formatter := NewStandardFormatter("%(lineno)d", "")
lineNo := WithLineFeed(fmt.Sprintf("%d", testRecord.LineNo))
require.Equal(t, lineNo, formatter.Format(testRecord))
}
func TestFormat_FuncName(t *testing.T) {
formatter := NewStandardFormatter("%(funcname)s", "")
require.Equal(t,
WithLineFeed(testRecord.FuncName), formatter.Format(testRecord))
}
func TestFormat_Created(t *testing.T) {
formatter := NewStandardFormatter("%(created)d", "")
created := WithLineFeed(
fmt.Sprintf("%d", testRecord.CreatedTime.UnixNano()))
require.Equal(t, created, formatter.Format(testRecord))
}
func TestFormat_AscTime(t *testing.T) {
formatter := NewStandardFormatter("%(asctime)s", defaultDateFormat)
testRecord.AscTime = formatter.FormatTime(testRecord)
require.Equal(t,
WithLineFeed(testRecord.AscTime), formatter.Format(testRecord))
}
func TestFormat_Message(t *testing.T) {
formatter := defaultFormatter
require.Equal(t,
WithLineFeed(testRecord.GetMessage()), formatter.Format(testRecord))
}