forked from charmbracelet/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
73 lines (69 loc) · 1.8 KB
/
options_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
package log
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func TestOptions(t *testing.T) {
opts := Options{
Level: ErrorLevel,
ReportCaller: true,
Fields: []interface{}{"foo", "bar"},
}
logger := NewWithOptions(io.Discard, opts)
require.Equal(t, ErrorLevel, logger.GetLevel())
require.True(t, logger.reportCaller)
require.False(t, logger.reportTimestamp)
require.Equal(t, []interface{}{"foo", "bar"}, logger.fields)
require.Equal(t, TextFormatter, logger.formatter)
require.Equal(t, DefaultTimeFormat, logger.timeFormat)
require.NotNil(t, logger.timeFunc)
}
func TestCallerFormatter(t *testing.T) {
var buf bytes.Buffer
l := NewWithOptions(&buf, Options{ReportCaller: true})
frames := l.frames(0)
frame, _ := frames.Next()
file, line, fn := frame.File, frame.Line, frame.Function
hi := func() { l.Info("hi") }
cases := []struct {
name string
expected string
format CallerFormatter
}{
{
name: "short caller formatter",
expected: fmt.Sprintf("INFO <log/options_test.go:%d> hi\n", line+3),
format: ShortCallerFormatter,
},
{
name: "long caller formatter",
expected: fmt.Sprintf("INFO <%s:%d> hi\n", file, line+3),
format: LongCallerFormatter,
},
{
name: "foo caller formatter",
expected: "INFO <foo> hi\n",
format: func(file string, line int, fn string) string {
return "foo"
},
},
{
name: "custom caller formatter",
expected: fmt.Sprintf("INFO <%s:%d:%s.func1> hi\n", file, line+3, fn),
format: func(file string, line int, fn string) string {
return fmt.Sprintf("%s:%d:%s", file, line, fn)
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
buf.Reset()
l.callerFormatter = c.format
hi()
require.Equal(t, c.expected, buf.String())
})
}
}