-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhaslength.go
89 lines (78 loc) · 2.95 KB
/
haslength.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
package has
import (
"fmt"
"github.com/corbym/gocrest"
)
const description = "value with length %v"
// Length can be called with arrays
// Returns a matcher that matches if the length matches the given criteria
func Length[T any](expected int) *gocrest.Matcher[[]T] {
matcher := new(gocrest.Matcher[[]T])
matcher.Describe = fmt.Sprintf(description, expected)
matcher.Matches = func(actual []T) bool {
lenOfActual := len(actual)
matcher.Actual = fmt.Sprintf("length was %d", lenOfActual)
return lenOfActual == expected
}
return matcher
}
// StringLength can be called with strings
// Returns a matcher that matches if the length matches the given criteria
func StringLength(expected int) *gocrest.Matcher[string] {
matcher := new(gocrest.Matcher[string])
matcher.Describe = fmt.Sprintf(description, expected)
matcher.Matches = func(actual string) bool {
lenOfActual := len(actual)
matcher.Actual = fmt.Sprintf("length was %d", lenOfActual)
return lenOfActual == expected
}
return matcher
}
// MapLength can be called with maps
// Returns a matcher that matches if the length matches the given criteria
func MapLength[K comparable, V any](expected int) *gocrest.Matcher[map[K]V] {
matcher := new(gocrest.Matcher[map[K]V])
matcher.Describe = fmt.Sprintf(description, expected)
matcher.Matches = func(actual map[K]V) bool {
lenOfActual := len(actual)
matcher.Actual = fmt.Sprintf("length was %d", lenOfActual)
return lenOfActual == expected
}
return matcher
}
// LengthMatching can be called with arrays
// Returns a matcher that matches if the length matches matcher passed in
func LengthMatching[A any](expected *gocrest.Matcher[int]) *gocrest.Matcher[[]A] {
matcher := new(gocrest.Matcher[[]A])
matcher.Describe = fmt.Sprintf(description, expected)
matcher.Matches = func(actual []A) bool {
lenOfActual := len(actual)
matcher.Actual = fmt.Sprintf("length was %d", lenOfActual)
return expected.Matches(lenOfActual)
}
return matcher
}
// StringLengthMatching can be called with arrays or strings
// Returns a matcher that matches if the length matches matcher passed in
func StringLengthMatching(expected *gocrest.Matcher[int]) *gocrest.Matcher[string] {
matcher := new(gocrest.Matcher[string])
matcher.Describe = fmt.Sprintf(description, expected)
matcher.Matches = func(actual string) bool {
lenOfActual := len(actual)
matcher.Actual = fmt.Sprintf("length was %d", lenOfActual)
return expected.Matches(lenOfActual)
}
return matcher
}
// MapLengthMatching can be called with maps
// Returns a matcher that matches if the length matches the given matcher
func MapLengthMatching[K comparable, V any](expected *gocrest.Matcher[int]) *gocrest.Matcher[map[K]V] {
matcher := new(gocrest.Matcher[map[K]V])
matcher.Describe = fmt.Sprintf(description, expected)
matcher.Matches = func(actual map[K]V) bool {
lenOfActual := len(actual)
matcher.Actual = fmt.Sprintf("length was %d", lenOfActual)
return expected.Matches(lenOfActual)
}
return matcher
}