From 6ef7afe907bbd3f080418f08aae6e576af4397ed Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 25 Jul 2020 17:32:13 +0000 Subject: [PATCH] fix skip recursion --- tea/tree.go | 48 ++++++++++++++++++++++++------------------------ tea_test.go | 6 +++++- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/tea/tree.go b/tea/tree.go index c9eddf0..87c162d 100644 --- a/tea/tree.go +++ b/tea/tree.go @@ -1,59 +1,52 @@ package tea import ( + // "reflect" "testing" ) // Run runs a tree of tests, starting from its root. func Run(t *testing.T, tree *Tree) { t.Run(tree.name, func(t *testing.T) { - var setup []Test + setup(t, tree) - for root := tree; root.parent != nil; root = root.parent { - setup = append(setup, root.parent.Test) - } - - for i, j := 0, len(setup)-1; i < j; i, j = i+1, j-1 { - setup[i], setup[j] = setup[j], setup[i] - } - - for _, test := range setup { - test.Run(t) - } - - tree.Test.Run(t) + tree.test.Run(t) - if t.Failed() || t.Skipped() { - for _, child := range tree.children { + for _, child := range tree.children { + if t.Failed() || t.Skipped() { skip(t, child) + } else { + Run(t, child) } - return - } - - for _, child := range tree.children { - Run(t, child) } }) } +func setup(t *testing.T, tree *Tree) { + if tree.parent != nil { + setup(t, tree.parent) + tree.parent.test.Run(t) + } +} + func skip(t *testing.T, tree *Tree) { t.Run(tree.name, func(t *testing.T) { - t.Skip("tea skipped: dependency failed") for _, child := range tree.children { skip(t, child) } + t.Skip("tea skipped: dependency failed") }) } func New(test Test) *Tree { return &Tree{ - Test: test, + test: test, name: parseName(test), } } type Tree struct { - Test + test Test name string parent *Tree children []*Tree @@ -66,6 +59,13 @@ func (t *Tree) Child(test Test) *Tree { return child } +// func clone(t Test) Test { +// T := reflect.TypeOf(t) +// switch T.Kind() { +// case reflect.Struct: +// } +// } + func parseName(test Test) string { if s, ok := test.(interface{ String() string }); ok { return s.String() diff --git a/tea_test.go b/tea_test.go index 0eaa6bf..79c2ea0 100644 --- a/tea_test.go +++ b/tea_test.go @@ -15,6 +15,9 @@ type testThingSetup struct { func (test *testThingSetup) Run(t *testing.T) { t.Logf("[%s] running testThingSetup", t.Name()) + if test.thing != nil { + // t.Fatal("should be nil") + } test.thing = new(Thing) } @@ -55,7 +58,8 @@ func TestThing(t *testing.T) { { bob := root.Child(&setKey{key: "b ob", value: "banana"}) bob.Child(&setKey{key: "car-el", value: "cherry"}) - bob.Child(&setKey{key: "dave", value: "durian"}) + dave := bob.Child(&setKey{key: "dave", value: "durian"}) + dave.Child(&setKey{key: "evan", value: "elderberry"}) } {