-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_logger_test.go
55 lines (53 loc) · 1.3 KB
/
go_logger_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
package logus
import (
"context"
"fmt"
"log"
"strings"
"testing"
)
func Test_goLogger_Log(t *testing.T) {
tests := []struct {
name string
ctx context.Context
arg LogEntry
expected string
}{
{
name: "debug_nil_context",
ctx: nil,
arg: LogEntry{Severity: SeverityDebug, MessageFormat: "debugging message"},
expected: "DEBUG: debugging message",
},
{
name: "info_with_component",
ctx: context.Background(),
arg: LogEntry{Severity: SeverityInfo, Component: "UnitTest", MessageFormat: "informational message"},
expected: "INFO: Component=UnitTest: informational message"},
}
defer func() {
logPrintf = log.Printf
}()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var logPrintsCount int
var logString string
// Setup mocks
logPrintf = func(format string, v ...interface{}) {
logPrintsCount++
logString = fmt.Sprintf(format, v...)
}
s := NewStandardGoLogger()
err := s.Log(tt.ctx, tt.arg)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if logPrintsCount != 1 {
t.Errorf("expected 1 log print, got %d", logPrintsCount)
}
if !strings.Contains(logString, tt.expected) {
t.Errorf("expected log message to contain %q, got %q", tt.expected, logString)
}
})
}
}