-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_logger.go
81 lines (66 loc) · 1.85 KB
/
mock_logger.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
package tools
import (
"errors"
"fmt"
)
// MockLogger provides a basic mock of the logger object.
type MockLogger struct {
calls []LoggerCall
}
// Call is a single call to a logger method. It has the method name and the arguments it was called with
type LoggerCall struct {
Method string
Args LoggerArgs
}
// Args are the list of arguments to a single logger method
type LoggerArgs struct {
Msg string
}
// Info is a mock info method
func (ml *MockLogger) Info(args ...interface{}) {
ml.call("Info", args...)
}
// Infof is a mock info method
func (ml *MockLogger) Infof(format string, args ...interface{}) {
ml.call("Info", fmt.Sprintf(format, args...))
}
// Info is a mock info method
func (ml *MockLogger) Error(args ...interface{}) {
ml.call("Error", args...)
}
// Errorf is a mock info method
func (ml *MockLogger) Errorf(format string, args ...interface{}) {
ml.call("Error", fmt.Sprintf(format, args...))
}
// Debug is a mock debug method
func (ml *MockLogger) Debug(args ...interface{}) {
ml.call("Debug", args...)
}
// Debugf is a mock debug method
func (ml *MockLogger) Debugf(format string, args ...interface{}) {
ml.call("Debug", fmt.Sprintf(format, args...))
}
// Warn is a mock debug method
func (ml *MockLogger) Warn(args ...interface{}) {
ml.call("Warn", args...)
}
// Warnf is a mock debug method
func (ml *MockLogger) Warnf(format string, args ...interface{}) {
ml.call("Warn", fmt.Sprintf(format, args...))
}
func (ml *MockLogger) Call() (c LoggerCall, err error) {
if len(ml.calls) == 0 {
return c, errors.New("No calls made")
}
return ml.calls[0], nil
}
func (ml *MockLogger) LastCall() *LoggerCall {
if len(ml.calls) == 0 {
return nil
}
return &ml.calls[len(ml.calls)-1]
}
func (ml *MockLogger) call(method string, args ...interface{}) {
msg := fmt.Sprint(args...)
ml.calls = append(ml.calls, LoggerCall{method, LoggerArgs{msg}})
}