1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package zapcore_test
21
22 import (
23 "testing"
24 "time"
25
26 "github.com/stretchr/testify/assert"
27
28 . "go.uber.org/zap/zapcore"
29 )
30
31 var testEntry = Entry{
32 LoggerName: "main",
33 Level: InfoLevel,
34 Message: `hello`,
35 Time: _epoch,
36 Stack: "fake-stack",
37 Caller: EntryCaller{Defined: true, File: "foo.go", Line: 42, Function: "foo.Foo"},
38 }
39
40 func TestConsoleEncodeEntry(t *testing.T) {
41 tests := []struct {
42 desc string
43 expected string
44 ent Entry
45 fields []Field
46 }{
47 {
48 desc: "info no fields",
49 expected: "2018-06-19T16:33:42Z\tinfo\tbob\tlob law\n",
50 ent: Entry{
51 Level: InfoLevel,
52 Time: time.Date(2018, 6, 19, 16, 33, 42, 99, time.UTC),
53 LoggerName: "bob",
54 Message: "lob law",
55 },
56 },
57 {
58 desc: "zero_time_omitted",
59 expected: "info\tname\tmessage\n",
60 ent: Entry{
61 Level: InfoLevel,
62 Time: time.Time{},
63 LoggerName: "name",
64 Message: "message",
65 },
66 },
67 }
68
69 cfg := testEncoderConfig()
70 cfg.EncodeTime = RFC3339TimeEncoder
71 enc := NewConsoleEncoder(cfg)
72
73 for _, tt := range tests {
74 t.Run(tt.desc, func(t *testing.T) {
75 buf, err := enc.EncodeEntry(tt.ent, tt.fields)
76 if assert.NoError(t, err, "Unexpected console encoding error.") {
77 assert.Equal(t, tt.expected, buf.String(), "Incorrect encoded entry.")
78 }
79 buf.Free()
80 })
81 }
82 }
83
84 func TestConsoleSeparator(t *testing.T) {
85 tests := []struct {
86 desc string
87 separator string
88 wantConsole string
89 }{
90 {
91 desc: "space console separator",
92 separator: " ",
93 wantConsole: "0 info main foo.go:42 foo.Foo hello\nfake-stack\n",
94 },
95 {
96 desc: "default console separator",
97 separator: "",
98 wantConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack\n",
99 },
100 {
101 desc: "tag console separator",
102 separator: "\t",
103 wantConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack\n",
104 },
105 {
106 desc: "dash console separator",
107 separator: "--",
108 wantConsole: "0--info--main--foo.go:42--foo.Foo--hello\nfake-stack\n",
109 },
110 }
111
112 for _, tt := range tests {
113 console := NewConsoleEncoder(encoderTestEncoderConfig(tt.separator))
114 t.Run(tt.desc, func(t *testing.T) {
115 entry := testEntry
116 consoleOut, err := console.EncodeEntry(entry, nil)
117 if !assert.NoError(t, err) {
118 return
119 }
120 assert.Equal(
121 t,
122 tt.wantConsole,
123 consoleOut.String(),
124 "Unexpected console output",
125 )
126 })
127
128 }
129 }
130
131 func encoderTestEncoderConfig(separator string) EncoderConfig {
132 testEncoder := testEncoderConfig()
133 testEncoder.ConsoleSeparator = separator
134 return testEncoder
135 }
136
View as plain text