Skip to content

Commit 2d373a2

Browse files
committed
Prefactor: extract matchers.BeFunc
1 parent 3fbf214 commit 2d373a2

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

pkg/sharding/key/key_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ limitations under the License.
1717
package key_test
1818

1919
import (
20-
"reflect"
21-
2220
. "github.com/onsi/ginkgo/v2"
2321
. "github.com/onsi/gomega"
24-
"github.com/onsi/gomega/gcustom"
25-
gomegatypes "github.com/onsi/gomega/types"
2622
appsv1 "k8s.io/api/apps/v1"
2723
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2824
"k8s.io/utils/ptr"
2925

3026
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
3127
. "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/key"
28+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/utils/test/matchers"
3229
)
3330

3431
var _ = Describe("#FuncForResource", func() {
@@ -76,7 +73,7 @@ var _ = Describe("#FuncForResource", func() {
7673
Group: "operator",
7774
Resource: "foo",
7875
}, controllerRing)).To(
79-
beFunc(ForObject),
76+
BeFunc(ForObject),
8077
)
8178
})
8279

@@ -85,15 +82,15 @@ var _ = Describe("#FuncForResource", func() {
8582
Group: "operator",
8683
Resource: "controlled",
8784
}, controllerRing)).To(
88-
beFunc(ForController),
85+
BeFunc(ForController),
8986
)
9087
})
9188

9289
It("should return ForObject if the resource is a main and controlled resource of the ring", func() {
9390
Expect(FuncForResource(metav1.GroupResource{
9491
Resource: "foo",
9592
}, controllerRing)).To(
96-
beFunc(ForObject),
93+
BeFunc(ForObject),
9794
)
9895
})
9996
})
@@ -177,9 +174,3 @@ var _ = Describe("#ForController", func() {
177174
Expect(ForController(obj)).To(Equal("operator/Foo/bar/foo"))
178175
})
179176
})
180-
181-
func beFunc(expected Func) gomegatypes.GomegaMatcher {
182-
return gcustom.MakeMatcher(func(actual Func) (bool, error) {
183-
return reflect.ValueOf(expected).Pointer() == reflect.ValueOf(actual).Pointer(), nil
184-
})
185-
}

pkg/utils/test/matchers/matchers.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ limitations under the License.
1717
package matchers
1818

1919
import (
20+
"fmt"
21+
"reflect"
22+
23+
"github.com/onsi/gomega/gcustom"
2024
gomegatypes "github.com/onsi/gomega/types"
2125
apierrors "k8s.io/apimachinery/pkg/api/errors"
2226
)
@@ -28,3 +32,22 @@ func BeNotFoundError() gomegatypes.GomegaMatcher {
2832
message: "NotFound",
2933
}
3034
}
35+
36+
// BeFunc is a matcher that returns true if expected and actual are the same func.
37+
func BeFunc(expected any) gomegatypes.GomegaMatcher {
38+
return gcustom.MakeMatcher(func(actual any) (bool, error) {
39+
var (
40+
valueExpected = reflect.ValueOf(expected)
41+
valueActual = reflect.ValueOf(actual)
42+
)
43+
44+
if valueExpected.Kind() != reflect.Func {
45+
return false, fmt.Errorf("expected should be a func, got %v", valueExpected.Kind())
46+
}
47+
if valueActual.Kind() != reflect.Func {
48+
return false, fmt.Errorf("actual should be a func, got %v", valueActual.Kind())
49+
}
50+
51+
return valueExpected.Pointer() == valueActual.Pointer(), nil
52+
})
53+
}

0 commit comments

Comments
 (0)