This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
82 lines (68 loc) · 1.81 KB
/
context_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
78
79
80
81
82
package golf_test
import (
"fmt"
"testing"
"github.com/fhofherr/golf"
"github.com/fhofherr/golf/internal/golferr"
"github.com/stretchr/testify/assert"
)
func TestWith_NilLogger(t *testing.T) {
golf.With(nil, "key", "value")
}
func TestWith_NoWithMethod(t *testing.T) {
logger := golf.NewMockLogger(t)
logger.On(
"Log",
fmt.Sprintf("%s: %T does not implement With", golferr.MsgUnsupported, logger),
).Return()
actual := golf.With(logger, "key", "value")
assert.Same(t, logger, actual)
logger.AssertExpectations(t)
}
func TestWith_CallWithMethod(t *testing.T) {
type testCase struct {
name string
prepare func(t *testing.T, tt *testCase)
assert func(t *testing.T, tt *testCase)
args []interface{}
// Set during run
wither *golf.MockWither
actual golf.Logger
}
tests := []testCase{
{
name: "With returns invalid value",
args: []interface{}{"key", "value"},
prepare: func(t *testing.T, tt *testCase) {
tt.wither.On("With", tt.args...).Return(struct{}{})
tt.wither.On(
"Log", fmt.Sprintf("%s: (%T).With did not return a Logger", golferr.MsgError, tt.wither),
).Return(struct{}{})
},
assert: func(t *testing.T, tt *testCase) {
assert.Same(t, tt.wither, tt.actual)
},
},
{
name: "With returns correct value",
args: []interface{}{"another key", "another value"},
prepare: func(t *testing.T, tt *testCase) {
logger := golf.NewMockLogger(t)
tt.wither.On("With", tt.args...).Return(logger)
tt.assert = func(t *testing.T, tt *testCase) {
assert.Same(t, tt.actual, logger)
}
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tt.wither = golf.NewMockWither(t)
tt.prepare(t, &tt)
tt.actual = golf.With(tt.wither, tt.args...)
tt.assert(t, &tt)
tt.wither.AssertExpectations(t)
})
}
}