-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_expectation.go
67 lines (59 loc) · 2.12 KB
/
log_expectation.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
package testing_utils
import (
"fmt"
"strings"
)
type LogExpectation struct {
LineCount int
Lines []ExpectedLine
}
func (l *LogExpectation) MeetsExpectation(testingLogger *TestingLogger, includeDebugLines bool) error {
lines := testingLogger.Handler.Lines
if !includeDebugLines {
filteredLines := []*testingLogLine{}
for _, line := range lines {
if !line.IsDebug {
filteredLines = append(filteredLines, line)
}
}
lines = filteredLines
}
if len(lines) != l.LineCount {
return fmt.Errorf(
"TestingLogger does not have expected line count of %d (count was %d), lines (including debug lines = %t) were: %s",
l.LineCount,
len(lines),
includeDebugLines,
strings.Join(testingLogger.FullStrings(includeDebugLines), "\n"))
}
for _, el := range l.Lines {
line := lines[el.Index]
if el.Debug && !line.IsDebug {
return fmt.Errorf("TestingLogger line Index=%d failed expectation to be Debug level (D=%t,I=%t,W=%t,E=%t)", el.Index, line.IsDebug, line.IsInfo, line.IsWarning, line.IsError)
}
if el.Info && !line.IsInfo {
return fmt.Errorf("TestingLogger line Index=%d failed expectation to be Info level (D=%t,I=%t,W=%t,E=%t)", el.Index, line.IsDebug, line.IsInfo, line.IsWarning, line.IsError)
}
if el.Warning && !line.IsWarning {
return fmt.Errorf("TestingLogger line Index=%d failed expectation to be Warning level (D=%t,I=%t,W=%t,E=%t)", el.Index, line.IsDebug, line.IsInfo, line.IsWarning, line.IsError)
}
if el.Error && !line.IsError {
return fmt.Errorf("TestingLogger line Index=%d failed expectation to be Error level (D=%t,I=%t,W=%t,E=%t)", el.Index, line.IsDebug, line.IsInfo, line.IsWarning, line.IsError)
}
for _, subStr := range el.RequiredSubstrings {
lineText := line.String()
if !strings.Contains(lineText, subStr) {
return fmt.Errorf("TestingLogger line Index=%d did not contain sub string '%s'. The actual line content was: '%s'", el.Index, subStr, lineText)
}
}
}
return nil
}
type ExpectedLine struct {
Index int
RequiredSubstrings []string
Error bool
Warning bool
Info bool
Debug bool
}