MCPcopy
hub / github.com/charmbracelet/log / TestCallerFormatter

Function TestCallerFormatter

options_test.go:30–75  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28}
29
30func TestCallerFormatter(t *testing.T) {
31 var buf bytes.Buffer
32 l := NewWithOptions(colorprofile.NewWriter(&buf, os.Environ()), Options{ReportCaller: true})
33 frames := l.frames(0)
34 frame, _ := frames.Next()
35 file, line, fn := frame.File, frame.Line, frame.Function
36 hi := func() { l.Info("hi") }
37 cases := []struct {
38 name string
39 expected string
40 format CallerFormatter
41 }{
42 {
43 name: "short caller formatter",
44 expected: fmt.Sprintf("INFO <log/options_test.go:%d> hi\n", line+3),
45 format: ShortCallerFormatter,
46 },
47 {
48 name: "long caller formatter",
49 expected: fmt.Sprintf("INFO <%s:%d> hi\n", file, line+3),
50 format: LongCallerFormatter,
51 },
52 {
53 name: "foo caller formatter",
54 expected: "INFO <foo> hi\n",
55 format: func(file string, line int, fn string) string {
56 return "foo"
57 },
58 },
59 {
60 name: "custom caller formatter",
61 expected: fmt.Sprintf("INFO <%s:%d:%s.func1> hi\n", file, line+3, fn),
62 format: func(file string, line int, fn string) string {
63 return fmt.Sprintf("%s:%d:%s", file, line, fn)
64 },
65 },
66 }
67 for _, c := range cases {
68 t.Run(c.name, func(t *testing.T) {
69 buf.Reset()
70 l.callerFormatter = c.format
71 hi()
72 require.Equal(t, c.expected, buf.String())
73 })
74 }
75}

Callers

nothing calls this directly

Calls 4

NewWithOptionsFunction · 0.85
framesMethod · 0.80
InfoMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…