-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers_say.go
72 lines (66 loc) · 1.92 KB
/
matchers_say.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
package justest
import (
"bytes"
"fmt"
"reflect"
"regexp"
)
var (
sayValueExtractor ValueExtractor
)
func init() {
sayValueExtractor = NewValueExtractor(ExtractorUnsupported)
sayValueExtractor[reflect.Chan] = NewChannelExtractor(sayValueExtractor, true)
sayValueExtractor[reflect.Func] = NewFuncExtractor(sayValueExtractor, true)
sayValueExtractor[reflect.Pointer] = func(t T, v any) (any, bool) {
GetHelper(t).Helper()
if bufferPointer, ok := v.(*bytes.Buffer); ok {
return bufferPointer.String(), true
} else if stringPointer, ok := v.(*string); ok {
return *stringPointer, true
} else if ba, ok := v.(*[]byte); ok {
return string(*ba), true
} else {
t.Fatalf("Unsupported type '%T' for Say matcher: %+v", v, v)
panic("unreachable")
}
}
sayValueExtractor[reflect.Slice] = func(t T, v any) (any, bool) {
GetHelper(t).Helper()
if b, ok := v.([]byte); ok {
return string(b), true
} else {
t.Fatalf("Unsupported type '%T' for Say matcher: %+v", v, v)
panic("unreachable")
}
}
sayValueExtractor[reflect.String] = ExtractSameValue
}
//go:noinline
func Say[Type string | *regexp.Regexp](expectation Type) Matcher {
switch e := any(expectation).(type) {
case string:
re := regexp.MustCompile(e)
return MatcherFunc(func(t T, actuals ...any) {
GetHelper(t).Helper()
for _, actual := range actuals {
v := sayValueExtractor.MustExtractValue(t, actual)
if !re.Match([]byte(v.(string))) {
t.Fatalf("Expected actual value to match '%s', but it does not: %s", re, v)
}
}
})
case *regexp.Regexp:
return MatcherFunc(func(t T, actuals ...any) {
GetHelper(t).Helper()
for _, actual := range actuals {
v := sayValueExtractor.MustExtractValue(t, actual)
if !e.Match([]byte(v.(string))) {
t.Fatalf("Expected actual value to match '%s', but it does not: %s", e, v)
}
}
})
default:
panic(fmt.Sprintf("unsupported type for Say matcher: %T", expectation))
}
}