/
args.go
160 lines (134 loc) · 3.29 KB
/
args.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package bintest
import (
"fmt"
"regexp"
"strings"
)
func ArgumentsFromStrings(s []string) Arguments {
args := make([]interface{}, len(s))
for idx, v := range s {
args[idx] = v
}
return args
}
type ArgumentsMatchResult struct {
IsMatch bool
MatchCount int
Explanation string
}
type Arguments []interface{}
func (a Arguments) Match(x ...string) (result ArgumentsMatchResult) {
for i, expected := range a {
var formatArgumentMismatch = func(formatter string, args ...interface{}) string {
return fmt.Sprintf("Argument #%d doesn't match: %s", i+1, fmt.Sprintf(formatter, args...))
}
if len(x) <= i {
result.Explanation = formatArgumentMismatch("Expected %q, but missing an argument", expected)
return
}
var actual = x[i]
if matcher, ok := expected.(Matcher); ok {
if match, message := matcher.Match(actual); !match {
result.Explanation = formatArgumentMismatch(message)
return
}
} else if s, ok := expected.(string); ok && s != actual {
idx := findCommonPrefix([]rune(s), []rune(actual))
if idx == 0 {
result.Explanation = formatArgumentMismatch(
"Expected %q, got %q", shorten(s), shorten(actual))
} else {
result.Explanation = formatArgumentMismatch(
"Differs at character %d, expected %q, got %q", idx+1,
shorten(s[idx:]), shorten(actual[idx:]))
}
return
}
result.MatchCount++
}
if len(x) > len(a) {
result.Explanation = fmt.Sprintf("Argument #%d doesn't match: Unexpected extra argument", len(a))
return
}
result.IsMatch = true
return
}
const (
shortenLength = 10
)
func shorten(s string) string {
if len(s) > shortenLength {
return s[:shortenLength] + "…"
}
return s
}
func findCommonPrefix(s1, s2 []rune) int {
var maxLength = len(s1)
if len(s2) < maxLength {
maxLength = len(s2)
}
for i := 0; i < maxLength; i++ {
if s1[i] != s2[i] {
return i
}
}
return maxLength
}
func (a Arguments) String() string {
return FormatInterfaces(a)
}
type Matcher interface {
fmt.Stringer
Match(s string) (bool, string)
}
type MatcherFunc struct {
f func(s string) (bool, string)
str string
}
func (mf MatcherFunc) Match(s string) (bool, string) {
return mf.f(s)
}
func (mf MatcherFunc) String() string {
return mf.str
}
func MatchAny() Matcher {
return MatcherFunc{
f: func(s string) (bool, string) { return true, "" },
str: "bintest.MatchAny()",
}
}
func MatchPattern(pattern string) Matcher {
re := regexp.MustCompile(pattern)
return MatcherFunc{
f: func(s string) (bool, string) {
if !re.MatchString(s) {
return false, "Didn't match pattern " + pattern
}
return true, ""
},
str: fmt.Sprintf("bintest.MatchPattern(%q)", pattern),
}
}
// FormatStrings formats a slice of strings as quoted comma-separated arguments
func FormatStrings(a []string) string {
var s = make([]string, len(a))
for idx := range a {
s[idx] = fmt.Sprintf("%q", a[idx])
}
return strings.Join(s, ", ")
}
// FormatInterfaces formats a slice of interface{} as quoted comma-separated arguments
func FormatInterfaces(a []interface{}) string {
var s = make([]string, len(a))
for idx := range a {
switch t := a[idx].(type) {
case string:
s[idx] = fmt.Sprintf("%q", t)
case fmt.Stringer:
s[idx] = fmt.Sprintf("%s", t.String())
default:
s[idx] = fmt.Sprintf("%v", t)
}
}
return strings.Join(s, ", ")
}