-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlogger_test.go
More file actions
56 lines (48 loc) · 1.26 KB
/
logger_test.go
File metadata and controls
56 lines (48 loc) · 1.26 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
package backlog
import (
"bytes"
"log"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestLogging(t *testing.T) {
buf := bytes.NewBufferString("")
logger := internalLog{logger: log.New(buf, "", 0|log.Lshortfile)}
logger.Println("test line 123")
assert.Equal(t, "logger_test.go:15: test line 123\n", buf.String())
buf.Truncate(0)
logger.Print("test line 123")
assert.Equal(t, "logger_test.go:18: test line 123\n", buf.String())
buf.Truncate(0)
logger.Printf("test line 123\n")
assert.Equal(t, "logger_test.go:21: test line 123\n", buf.String())
buf.Truncate(0)
if err := logger.Output(1, "test line 123\n"); err != nil {
log.Println(err)
}
assert.Equal(t, "logger_test.go:24: test line 123\n", buf.String())
buf.Truncate(0)
}
type mockLogger struct {
shouldError bool
}
func (m *mockLogger) Output(_ int, _ string) error {
if m.shouldError {
return errors.New("mock error")
}
return nil
}
func TestInternalLog_PrintlnError(t *testing.T) {
originalLogFatal := logFatal
defer func() {
logFatal = originalLogFatal
}()
var fatalCalled bool
logFatal = func(_ ...interface{}) {
fatalCalled = true
}
logger := internalLog{logger: &mockLogger{shouldError: true}}
logger.Println("test")
assert.True(t, fatalCalled)
}