generated from skeptycal/gorepotemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_info_test.go
95 lines (82 loc) · 3.34 KB
/
test_info_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
83
84
85
86
87
88
89
90
91
92
93
94
95
package errorlogger
import (
"testing"
)
type errorloggerTestArgs struct {
enabled bool
msg string
wrap error
fn func(args ...interface{})
logger *Logger
}
// internal tests directly on private structs
var (
errorLoggerTestStruct = newTestStruct(true, "", nil, nil, nil)
wrapTestStruct = newTestStruct(true, "", fakeSysCallError, nil, nil)
messageTestStruct = newTestStruct(true, "fake test message", nil, nil, nil)
privateStructTests = []struct {
name string
e *errorLogger
}{
{"errorLoggerTestStruct", errorLoggerTestStruct},
{"wrapTestStruct", wrapTestStruct},
{"messageTestStruct", messageTestStruct},
}
// errorloggerTests provide a set of instantiated errorloggers
// used for tests.
// input uses type interface{} in order to allow testing with
// a variety of types that may or may not implement ErrorLogger.
//
// If ErrorLogger is not implemented, wantErr bool should be
// set to true.
errorloggerTests = []struct {
name string
args errorloggerTestArgs
want ErrorLogger
wantErr bool
}{
// control
{"global ErrorLogger", errorloggerTestArgs{}, testDefaultLogger, false},
// Check for false positive and false negative errors
// Test New() should pass and nil should fail
{"New()", errorloggerTestArgs{}, New(), false},
{"nil", errorloggerTestArgs{}, nil, true},
// NewWithOptions() is also tested here
{"NewWithOptions(false, nil, nil, nil) (should pass)", errorloggerTestArgs{}, NewWithOptions(false, "", nil, nil, nil), false},
{"NewWithOptions(true, nil, nil, nil)", errorloggerTestArgs{}, NewWithOptions(true, "", nil, nil, nil), false},
{"NewWithOptions(false, nil, nil, nil)", errorloggerTestArgs{}, NewWithOptions(false, "", nil, nil, nil), false},
{"NewWithOptions(true, nil, nil, string)", errorloggerTestArgs{}, NewWithOptions(true, "", nil, nil, nil), false},
{"NewWithOptions(true, nil, nil, integer)", errorloggerTestArgs{}, NewWithOptions(true, "", nil, nil, nil), false},
{"NewWithOptions(all defaults ...)", errorloggerTestArgs{}, NewWithOptions(true, "", defaultLogFunc, defaultErrWrap, defaultlogger), false},
{"NewWithOptions(false, DefaultLogFunc, nil)", errorloggerTestArgs{}, NewWithOptions(true, "", defaultLogFunc, nil, nil), false},
// Various tests using private struct
{"logrus logger in errorLogger (not public)", errorloggerTestArgs{}, &errorLogger{Logger: &Logger{}}, false},
{"default ErrorLogger with nil wrapper (not public)", errorloggerTestArgs{}, &errorLogger{wrap: nil}, false},
// Do not need a check for this in the constructor since errorLogger is not exported
// But something to be aware of ...
// {"ErrorLogger with nil logger (should fail)", &errorLogger{Logger: nil}, true},
}
)
func Test_newTestStruct(t *testing.T) {
for _, tt := range errorloggerTests {
t.Run(tt.name, func(t *testing.T) {
got := newTestStruct(tt.args.enabled, tt.args.msg, tt.args.wrap, tt.args.fn, tt.args.logger)
var want ErrorLogger
if tt.want == nil {
if !tt.wantErr {
t.Logf("newTestStruct() logger cannot be nil: %v(%T)", tt.want, tt.want)
t.FailNow()
// } else {
// want = New()
}
}
// TODO check test logic here ...
want = New()
g := got.errFunc(errFake)
w := want.Err(errFake)
if w.Error() != g.Error() {
t.Errorf("newTestStruct() error string not equal = %v, want %v", g, w)
}
})
}
}