forked from sclevine/agouti
/
log_matcher.go
95 lines (78 loc) · 2.13 KB
/
log_matcher.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 internal
import (
"fmt"
"strings"
"github.com/onsi/gomega/format"
"github.com/sclevine/agouti"
)
type LogMatcher struct {
ExpectedMessages []string
Levels []string
Name string
Type string
}
func (m *LogMatcher) Match(actual interface{}) (success bool, err error) {
actualPage, ok := actual.(interface {
ReadAllLogs(logType string) ([]agouti.Log, error)
})
if !ok {
return false, fmt.Errorf("HaveLogged%s matcher requires a Page. Got:\n%s", firstToUpper(m.Name), format.Object(actual, 1))
}
logs, err := actualPage.ReadAllLogs(m.Type)
if err != nil {
return false, err
}
if len(m.ExpectedMessages) == 0 {
return m.anyLogsForLevels(logs), nil
}
for _, message := range m.ExpectedMessages {
if !m.messageInLogs(message, logs) {
return false, nil
}
}
return true, nil
}
func (m *LogMatcher) FailureMessage(actual interface{}) (message string) {
if len(m.ExpectedMessages) == 0 {
return booleanMessage(actual, fmt.Sprintf("to have logged %s logs", m.Name))
}
messages := strings.Join(m.ExpectedMessages, "\n"+tab)
return equalityMessage(actual, fmt.Sprintf("to have %s logs matching", m.Name), messages)
}
func (m *LogMatcher) NegatedFailureMessage(actual interface{}) (message string) {
if len(m.ExpectedMessages) == 0 {
return booleanMessage(actual, fmt.Sprintf("not to have logged %s logs", m.Name))
}
messages := strings.Join(m.ExpectedMessages, "\n"+tab)
return equalityMessage(actual, fmt.Sprintf("not to have %s logs matching", m.Name), messages)
}
func (m *LogMatcher) messageInLogs(message string, logs []agouti.Log) bool {
for _, log := range logs {
if m.logInLevels(log) && log.Message == message {
return true
}
}
return false
}
func (m *LogMatcher) anyLogsForLevels(logs []agouti.Log) bool {
for _, log := range logs {
if m.logInLevels(log) {
return true
}
}
return false
}
func (m *LogMatcher) logInLevels(log agouti.Log) bool {
for _, level := range m.Levels {
if level == log.Level {
return true
}
}
return false
}
func firstToUpper(s string) string {
if s == "" {
return ""
}
return strings.ToUpper(string(s[0])) + s[1:]
}