forked from omigo/log
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstandard_test.go
More file actions
75 lines (63 loc) · 2.28 KB
/
standard_test.go
File metadata and controls
75 lines (63 loc) · 2.28 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
package log
import (
"runtime"
"strings"
"testing"
)
func TestCalculatePrefixLen(t *testing.T) {
format := `{"level": "info", "line": 88, "log": "message"}`
prefixLen := calculatePrefixLen(format, 1)
if prefixLen != -1 {
t.FailNow()
}
format = `{"level": "info", "file": "/go/src/github.com/gotips/log/examples/main.go", "line":88, "log": "message"}`
prefixLen = calculatePrefixLen(format, 1)
if prefixLen != 0 {
t.FailNow()
}
_, file, _, _ := runtime.Caller(0)
format = `{"level": "info", "file": "github.com/gotips/log/examples/main.go", "line":88, "log": "message"}`
prefixLen = calculatePrefixLen(format, 1)
if prefixLen != strings.Index(file, "/src/")+5 {
t.FailNow()
}
format = `{"level": "info", "file": "examples/main.go", "line":88, "log": "message"}`
prefixLen = calculatePrefixLen(format, 1)
if prefixLen != strings.Index(file, "standard_test.go") {
t.FailNow()
}
format = `{"level": "info", "file": "main.go", "line":88, "log": "message"}`
prefixLen = calculatePrefixLen(format, 1)
if prefixLen != strings.Index(file, "standard_test.go") {
t.FailNow()
}
}
func TestExtactDateTimeFormat(t *testing.T) {
format := `{"level": "info", "file": "log/main.go", "line":88, "log": "message"}`
dateFmt, timeFmt := extactDateTimeFormat(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 = extactDateTimeFormat(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 = extactDateTimeFormat(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 = extactDateTimeFormat(format)
}()
}