(t *testing.T)
| 28 | } |
| 29 | |
| 30 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…