-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocks_test.go
More file actions
117 lines (95 loc) · 3.3 KB
/
mocks_test.go
File metadata and controls
117 lines (95 loc) · 3.3 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xlog/blob/main/LICENSE.
package xlog_test
import (
"errors"
"io"
"sync/atomic"
"github.com/actforgood/xlog"
)
// Note: this file contains (internal) mocks needed in UTs.
// ErrWrite is a predefined error returned by WriteCallbackErr.
var ErrWrite = errors.New("intentionally triggered Writer error")
// Writer is a mock for io.Writer contract.
type MockWriter struct {
writeCallsCnt uint32
writeCallback func(p []byte) (n int, err error)
}
// Write mock logic.
func (mock *MockWriter) Write(p []byte) (int, error) {
atomic.AddUint32(&mock.writeCallsCnt, 1)
if mock.writeCallback != nil {
return mock.writeCallback(p)
}
return len(p), nil
}
// SetWriteCallback sets the callback to be executed inside Write.
// You can hook to control the returned value(s), to make assertions
// upon passed parameter(s).
func (mock *MockWriter) SetWriteCallback(
callback func(p []byte) (n int, err error),
) {
mock.writeCallback = callback
}
// WriteCallsCount returns the no. of times Write was called.
func (mock *MockWriter) WriteCallsCount() int {
return int(atomic.LoadUint32(&mock.writeCallsCnt))
}
// WriteCallbackErr is a predefined Write callback that returns
// an error.
func WriteCallbackErr(_ []byte) (int, error) {
return 0, ErrWrite
}
// ErrFormat is a predefined error returned by FormatCallbackErr.
var ErrFormat = errors.New("intentionally triggered Formatter error")
// Formatter is a mocked wrapper for a xlog.Formatter.
type MockFormatter struct {
formatCallsCnt uint32
formatCallback xlog.Formatter
}
// Format is the Formatter function.
func (mock *MockFormatter) Format(w io.Writer, keyValues []any) error {
atomic.AddUint32(&mock.formatCallsCnt, 1)
if mock.formatCallback != nil {
return mock.formatCallback(w, keyValues)
}
return nil
}
// SetFormatCallback sets the callback to be executed inside Format.
// You can hook to control the returned value(s), to make assertions
// upon passed parameters.
func (mock *MockFormatter) SetFormatCallback(callback xlog.Formatter) {
mock.formatCallback = callback
}
// FormatCallsCount returns the no. of times Format was called.
func (mock *MockFormatter) FormatCallsCount() int {
return int(atomic.LoadUint32(&mock.formatCallsCnt))
}
// FormatCallbackErr is a predefined MockFormatter callback that returns
// an error.
func FormatCallbackErr(_ io.Writer, _ []any) error {
return ErrFormat
}
// MockErrorHandler is a mocked wrapper for an xlog.ErrorHandler.
type MockErrorHandler struct {
handleCallsCnt uint32
handleCallback xlog.ErrorHandler
}
// Handle is the ErrorHandler function.
func (mock *MockErrorHandler) Handle(err error, keyValues []any) {
atomic.AddUint32(&mock.handleCallsCnt, 1)
if mock.handleCallback != nil {
mock.handleCallback(err, keyValues)
}
}
// SetHandleCallback sets the callback to be executed inside Handle.
// You can hook into to make assertions upon passed parameters.
func (mock *MockErrorHandler) SetHandleCallback(callback xlog.ErrorHandler) {
mock.handleCallback = callback
}
// HandleCallsCount returns the no. of times Handle was called.
func (mock *MockErrorHandler) HandleCallsCount() int {
return int(atomic.LoadUint32(&mock.handleCallsCnt))
}