-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchers_be_empty.go
36 lines (32 loc) · 994 Bytes
/
matchers_be_empty.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
package justest
import (
"reflect"
)
var (
emptyValueExtractor ValueExtractor
lengthExtractor Extractor = func(t T, v any) (any, bool) {
GetHelper(t).Helper()
return reflect.ValueOf(v).Len(), true
}
)
func init() {
emptyValueExtractor = NewValueExtractor(ExtractorUnsupported)
emptyValueExtractor[reflect.Array] = lengthExtractor
emptyValueExtractor[reflect.Chan] = lengthExtractor
emptyValueExtractor[reflect.Map] = lengthExtractor
emptyValueExtractor[reflect.Pointer] = NewPointerExtractor(emptyValueExtractor, true)
emptyValueExtractor[reflect.Slice] = lengthExtractor
emptyValueExtractor[reflect.String] = lengthExtractor
}
//go:noinline
func BeEmpty() Matcher {
return MatcherFunc(func(t T, actuals ...any) {
GetHelper(t).Helper()
for _, actual := range actuals {
length := emptyValueExtractor.MustExtractValue(t, actual).(int)
if length != 0 {
t.Fatalf("Expected '%+v' to be empty, but it is not (has a length of %d)", actual, length)
}
}
})
}