forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
80 lines (68 loc) · 1.56 KB
/
match.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
package mock
import (
"fmt"
"strings"
"github.com/redis/rueidis"
"go.uber.org/mock/gomock"
)
func Match(cmd ...string) gomock.Matcher {
return gomock.GotFormatterAdapter(
gomock.GotFormatterFunc(func(i any) string {
return format(i)
}),
&cmdMatcher{expect: cmd},
)
}
type cmdMatcher struct {
expect []string
}
func (c *cmdMatcher) Matches(x any) bool {
return gomock.Eq(commands(x)).Matches(c.expect)
}
func (c *cmdMatcher) String() string {
return fmt.Sprintf("redis command %v", c.expect)
}
func MatchFn(fn func(cmd []string) bool, description ...string) gomock.Matcher {
return gomock.GotFormatterAdapter(
gomock.GotFormatterFunc(func(i any) string {
return format(i)
}),
&fnMatcher{matcher: fn, description: description},
)
}
type fnMatcher struct {
matcher func(cmd []string) bool
description []string
}
func (c *fnMatcher) Matches(x any) bool {
if cmd, ok := commands(x).([]string); ok {
return c.matcher(cmd)
}
return false
}
func (c *fnMatcher) String() string {
return fmt.Sprintf("matches %v", strings.Join(c.description, " "))
}
func format(v any) string {
if _, ok := v.([]any); !ok {
v = []any{v}
}
sb := &strings.Builder{}
sb.WriteString("\n")
for i, c := range v.([]any) {
fmt.Fprintf(sb, "index %d redis command %v\n", i+1, commands(c))
}
return sb.String()
}
func commands(x any) any {
if cmd, ok := x.(rueidis.Completed); ok {
return cmd.Commands()
}
if cmd, ok := x.(rueidis.Cacheable); ok {
return cmd.Commands()
}
if cmd, ok := x.(rueidis.CacheableTTL); ok {
return cmd.Cmd.Commands()
}
return x
}