-
Notifications
You must be signed in to change notification settings - Fork 11
/
format_test.go
39 lines (32 loc) · 1.23 KB
/
format_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
package log
import "testing"
func TestExtactDateTime(t *testing.T) {
format := `{"level": "info", "file": "log/main.go", "line":88, "log": "message"}`
dateFmt, timeFmt := ExtactDateTime(format)
if dateFmt != "" && timeFmt != "" {
t.FailNow()
}
format = `{"datetime": "2006-01-02 15:04:05.999999999", "level": "info", "file": "log/main.go", "line":88, "log": "message"}`
dateFmt, timeFmt = ExtactDateTime(format)
if dateFmt != "2006-01-02 15:04:05.999999999" && timeFmt != "" {
t.FailNow()
}
format = `{"date": "2006-01-02", "time": "15:04:05.999999999", "level": "info", "file": "log/main.go", "line":88, "log": "message"}`
dateFmt, timeFmt = ExtactDateTime(format)
if dateFmt != "2006-01-02" && timeFmt != "15:04:05.999999999" {
t.FailNow()
}
// 测试 日期模式不能重复出现在 format 中,不能判定是模式还是固定字符串
func() {
defer func() {
if err := recover(); err == nil {
t.Error("must panic, but not")
t.FailNow()
}
}()
// 有两个 2006 ,会出错
format = `{"date": "2006-01-02", "time": "15:04:05.999999999", "Tag": "2006" "level": "info", "file": "log/main.go", "line":88, "log": "message"}`
dateFmt, timeFmt = ExtactDateTime(format)
_, _ = dateFmt, timeFmt
}()
}