From 766d7449e8a0b6a62957ab9aa7ab3c84d95f9ac1 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Fri, 20 Sep 2024 19:56:28 +0200 Subject: [PATCH] walk: refactor test to table test --- walk/walk_test.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/walk/walk_test.go b/walk/walk_test.go index 70441cd..0fb02b3 100644 --- a/walk/walk_test.go +++ b/walk/walk_test.go @@ -1,27 +1,35 @@ package walk import ( + "reflect" "testing" ) func TestWalk(t *testing.T) { - t.Run("walk function test", func(t *testing.T) { - expected := "Chris" - var got []string + cases := []struct { + Name string + Input interface{} + ExpectedCalls []string + }{ + { + "struct with one string field", + struct { + Name string + }{"Chris"}, + []string{"Chris"}, + }, + } - x := struct { - Name string - }{expected} + for _, test := range cases { + t.Run(test.Name, func(t *testing.T) { + var got []string + Walk(test.Input, func(input string) { + got = append(got, input) + }) - Walk(x, func(input string) { - got = append(got, input) + if !reflect.DeepEqual(got, test.ExpectedCalls) { + t.Errorf("got %v want %v", got, test.ExpectedCalls) + } }) - - if len(got) != 1 { - t.Errorf("wrong number of function calls, got %d want %d", len(got), 1) - } - if got[0] != expected { - t.Errorf("got %q want %q", got[0], expected) - } - }) + } }