-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer_test.go
77 lines (71 loc) · 1.72 KB
/
writer_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
74
75
76
77
package tracer
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
func TestNewFileWriter(t *testing.T) {
t.Parallel()
is := assert.New(t)
is.NotNil(NewFileWriter(nil, nil), "it should return a valid FileWriter")
}
type mockWriter struct {
mock.Mock
}
func (mw mockWriter) Write(p []byte) (n int, err error) {
args := mw.Called(p)
if args[1] != nil {
return 0, args[1].(error)
}
return args[0].(int), nil
}
func TestFileWriter_Write(t *testing.T) {
t.Parallel()
t.Run("when is not possible to write the log", func(t *testing.T) {
t.Parallel()
is := assert.New(t)
called := 0
expected := Entry{
Message: "this is a test",
}
mw := mockWriter{}
mw.On("Write", []byte("dummy")).Return(0, errors.New("some-error")).Once()
subject := &FileWriter{
writer: mw,
formatter: func(entry Entry) string {
called++
is.Equal(expected, entry, "it should be the expected entry")
return "dummy"
},
}
is.NotPanics(func() {
subject.Write(expected)
}, "it should not panics")
is.Equal(1, called, "it should be called one time")
mw.AssertExpectations(t)
})
t.Run("when is possible to write the log", func(t *testing.T) {
t.Parallel()
is := assert.New(t)
called := 0
expected := Entry{
Message: "this is a test",
}
mw := mockWriter{}
mw.On("Write", []byte("dummy")).Return(15, nil).Once()
subject := &FileWriter{
writer: mw,
formatter: func(entry Entry) string {
called++
is.Equal(expected, entry, "it should be the expected entry")
return "dummy"
},
}
is.NotPanics(func() {
subject.Write(expected)
}, "it should not panics")
is.Equal(1, called, "it should be called one time")
mw.AssertExpectations(t)
})
}