-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchers_be_between.go
46 lines (37 loc) · 1.34 KB
/
matchers_be_between.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
package justest
import (
"reflect"
)
//go:noinline
func BeBetween(min, max any) Matcher {
if min == nil {
panic("expected a non-nil minimum value")
}
if max == nil {
panic("expected a non-nil maximum value")
}
return MatcherFunc(func(t T, actuals ...any) {
GetHelper(t).Helper()
for _, actual := range actuals {
v := NumericValueExtractor.MustExtractValue(t, actual)
actualValue := reflect.ValueOf(v)
minimumValue := reflect.ValueOf(min)
if actualValue.Type().Kind() != minimumValue.Type().Kind() {
t.Fatalf("Expected actual value to be of type '%T', but it is of type '%T'", min, v)
}
maximumValue := reflect.ValueOf(max)
if actualValue.Type().Kind() != maximumValue.Type().Kind() {
t.Fatalf("Expected actual value to be of type '%T', but it is of type '%T'", max, v)
}
cmpCompareFunctionValue := getNumericCompareFuncFor(t, v)
resultActualVsMin := cmpCompareFunctionValue.Call([]reflect.Value{actualValue, minimumValue})
if result := resultActualVsMin[0].Int(); result < 0 {
t.Fatalf("Expected actual value %v to be between %v and %v", v, min, max)
}
resultActualVsMax := cmpCompareFunctionValue.Call([]reflect.Value{actualValue, maximumValue})
if result := resultActualVsMax[0].Int(); result > 0 {
t.Fatalf("Expected actual value %v to be between %v and %v", v, min, max)
}
}
})
}