-
Notifications
You must be signed in to change notification settings - Fork 6
/
log_test.go
73 lines (62 loc) · 1.53 KB
/
log_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
// Copyright 2022 The incite Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package incite
import (
"strings"
"testing"
"github.com/stretchr/testify/mock"
)
func TestNopLogger_Printf(t *testing.T) {
t.Run("Empty Format", func(t *testing.T) {
NopLogger.Printf("foo")
})
t.Run("One Argument", func(t *testing.T) {
NopLogger.Printf("hello, %q", "world")
})
t.Run("Two Arguments", func(t *testing.T) {
NopLogger.Printf("the two numbers are %d and %d", 1, 2)
})
}
type mockLogger struct {
mock.Mock
}
func newMockLogger(t *testing.T) *mockLogger {
m := &mockLogger{}
m.Test(t)
return m
}
func (m *mockLogger) Printf(format string, v ...interface{}) {
m.Called(format, v)
}
func (m *mockLogger) expectPrintf(format string, v ...interface{}) *mock.Call {
nPct := strings.Count(format, "%")
if len(v) > nPct {
panic("more arguments given than percent signs in the format string!")
}
w := make([]interface{}, len(v))
copy(w, v)
return m.On("Printf", format, mock.MatchedBy(func(x []interface{}) bool {
if len(x) != nPct {
return false
}
for i := range w {
if x[i] == w[i] || w[i] == mock.Anything {
continue
}
xsi, ok := x[i].(string)
if !ok {
return false
}
if w[i] == mock.AnythingOfType("string") {
continue
}
if wpi, ok := w[i].(stringPrefix); ok && len(xsi) >= len(wpi) && string(wpi) == xsi[0:len(wpi)] {
continue
}
return false
}
return true
})).Return()
}
type stringPrefix string