From c15abf0398ed156e17ff8522ece834eda5159428 Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Mon, 4 Mar 2019 13:17:56 +1100 Subject: [PATCH] internal/dag: dry up tests with a method expression Signed-off-by: Dave Cheney --- internal/dag/builder_test.go | 50 +++++++++++------------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/internal/dag/builder_test.go b/internal/dag/builder_test.go index be629403b83..6d5bfe6b7ee 100644 --- a/internal/dag/builder_test.go +++ b/internal/dag/builder_test.go @@ -2762,11 +2762,7 @@ func TestDAGInsert(t *testing.T) { dag := b.Build() got := make(map[int]*Listener) - dag.Visit(func(v Vertex) { - if l, ok := v.(*Listener); ok { - got[l.Port] = l - } - }) + dag.Visit(listenerMap(got).Visit) want := make(map[int]*Listener) for _, v := range tc.want { @@ -2785,6 +2781,14 @@ func TestDAGInsert(t *testing.T) { } } +type listenerMap map[int]*Listener + +func (lm listenerMap) Visit(v Vertex) { + if l, ok := v.(*Listener); ok { + lm[l.Port] = l + } +} + func backend(name string, port intstr.IntOrString) *v1beta1.IngressBackend { return &v1beta1.IngressBackend{ ServiceName: name, @@ -2922,11 +2926,7 @@ func TestDAGIngressRouteCycle(t *testing.T) { dag := b.Build() got := make(map[int]*Listener) - dag.Visit(func(v Vertex) { - if l, ok := v.(*Listener); ok { - got[l.Port] = l - } - }) + dag.Visit(listenerMap(got).Visit) want := make(map[int]*Listener) want[80] = &Listener{ @@ -2972,11 +2972,7 @@ func TestDAGIngressRouteCycleSelfEdge(t *testing.T) { dag := b.Build() got := make(map[int]*Listener) - dag.Visit(func(v Vertex) { - if l, ok := v.(*Listener); ok { - got[l.Port] = l - } - }) + dag.Visit(listenerMap(got).Visit) want := make(map[int]*Listener) opts := []cmp.Option{ @@ -3012,11 +3008,7 @@ func TestDAGIngressRouteDelegatesToNonExistent(t *testing.T) { dag := b.Build() got := make(map[int]*Listener) - dag.Visit(func(v Vertex) { - if l, ok := v.(*Listener); ok { - got[l.Port] = l - } - }) + dag.Visit(listenerMap(got).Visit) want := make(map[int]*Listener) opts := []cmp.Option{ @@ -3067,11 +3059,7 @@ func TestDAGIngressRouteDelegatePrefixDoesntMatch(t *testing.T) { dag := b.Build() got := make(map[int]*Listener) - dag.Visit(func(v Vertex) { - if l, ok := v.(*Listener); ok { - got[l.Port] = l - } - }) + dag.Visit(listenerMap(got).Visit) want := make(map[int]*Listener) opts := []cmp.Option{ @@ -3239,11 +3227,7 @@ func TestDAGIngressRouteDelegatePrefixMatchesStringPrefixButNotPathPrefix(t *tes dag := b.Build() got := make(map[int]*Listener) - dag.Visit(func(v Vertex) { - if l, ok := v.(*Listener); ok { - got[l.Port] = l - } - }) + dag.Visit(listenerMap(got).Visit) want := make(map[int]*Listener) opts := []cmp.Option{ @@ -3824,11 +3808,7 @@ func TestDAGIngressRouteUniqueFQDNs(t *testing.T) { } dag := b.Build() got := make(map[int]*Listener) - dag.Visit(func(v Vertex) { - if l, ok := v.(*Listener); ok { - got[l.Port] = l - } - }) + dag.Visit(listenerMap(got).Visit) want := make(map[int]*Listener) for _, v := range tc.want {