-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher_test.go
73 lines (62 loc) · 1.89 KB
/
matcher_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
package slogtest
import (
"fmt"
"log/slog"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMatcher(t *testing.T) {
t.Run("find message in logs", func(t *testing.T) {
matcher := NewMatcher(t).WithMsg("test")
defer matcher.Finish()
logger := slog.New(matcher.Handler())
logger.Info("test")
})
t.Run("find message reg exp in logs", func(t *testing.T) {
matcher := NewMatcher(t).WithMsgRegExp(regexp.MustCompile("test."))
defer matcher.Finish()
logger := slog.New(matcher.Handler())
logger.Info("test5")
})
t.Run("find no message reg exp in logs", func(t *testing.T) {
matcher := NewMatcher(t).WithNoMsgRegExp(regexp.MustCompile("^test.$"))
defer matcher.Finish()
logger := slog.New(matcher.Handler())
logger.Info("test52")
})
t.Run("find no message reg exp in logs fail", func(t *testing.T) {
mockT := &CollectT{}
matcher := NewMatcher(mockT).WithNoMsgRegExp(regexp.MustCompile("^test.$"))
defer matcher.Finish()
logger := slog.New(matcher.Handler())
logger.Info("test5")
require.Len(t, mockT.errors, 1)
assert.Equal(t, mockT.errors[0].Error(), "Expect \"test5\" to NOT match \"^test.$\"")
})
t.Run("find no message in logs", func(t *testing.T) {
matcher := NewMatcher(t).WithNoMsg("test")
defer matcher.Finish()
logger := slog.New(matcher.Handler())
logger.Info("test2")
})
t.Run("test immutability", func(t *testing.T) {
m := NewMatcher(t)
m.WithMsg("test")
assert.Nil(t, m.afterAssertF)
})
t.Run("test groups find message", func(t *testing.T) {
matcher := NewMatcher(t).WithMsg("test")
defer matcher.Finish()
logger := slog.New(matcher.Handler())
logger.WithGroup("group").Info("test")
})
}
type CollectT struct {
errors []error
}
// Errorf collects the error.
func (c *CollectT) Errorf(format string, args ...interface{}) {
c.errors = append(c.errors, fmt.Errorf(format, args...))
}