From 2853ecfb38062d106464cd99a0e23742371f3708 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 25 Jul 2020 19:45:06 +0000 Subject: [PATCH] clone supplied test values when running setup --- tea/test.go | 15 ++++++++++++++- tea/tree.go | 18 +++++++++--------- tea_test.go | 28 +++++++++++----------------- thing.go | 3 +++ 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/tea/test.go b/tea/test.go index bc18655..2c197a5 100644 --- a/tea/test.go +++ b/tea/test.go @@ -1,7 +1,20 @@ package tea -import "testing" +import ( + "fmt" + "testing" +) type Test interface { Run(*testing.T) } + +func fail(t string, args ...interface{}) Test { + return failure{cause: fmt.Errorf(t, args...)} +} + +type failure struct { + cause error +} + +func (f failure) Run(t *testing.T) { t.Error(f.cause.Error()) } diff --git a/tea/tree.go b/tea/tree.go index 87c162d..052e508 100644 --- a/tea/tree.go +++ b/tea/tree.go @@ -1,7 +1,7 @@ package tea import ( - // "reflect" + "reflect" "testing" ) @@ -10,7 +10,7 @@ func Run(t *testing.T, tree *Tree) { t.Run(tree.name, func(t *testing.T) { setup(t, tree) - tree.test.Run(t) + clone(tree.test).Run(t) for _, child := range tree.children { if t.Failed() || t.Skipped() { @@ -25,7 +25,7 @@ func Run(t *testing.T, tree *Tree) { func setup(t *testing.T, tree *Tree) { if tree.parent != nil { setup(t, tree.parent) - tree.parent.test.Run(t) + clone(tree.parent.test).Run(t) } } @@ -59,12 +59,12 @@ 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 clone(t Test) Test { + srcV := reflect.ValueOf(t).Elem() + destV := reflect.New(srcV.Type()) + destV.Elem().Set(srcV) + return destV.Interface().(Test) +} func parseName(test Test) string { if s, ok := test.(interface{ String() string }); ok { diff --git a/tea_test.go b/tea_test.go index 79c2ea0..8882288 100644 --- a/tea_test.go +++ b/tea_test.go @@ -16,7 +16,7 @@ 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") + t.Fatal("should be nil") } test.thing = new(Thing) } @@ -49,24 +49,18 @@ func (test *setKey) Run(t *testing.T) { func TestThing(t *testing.T) { root := tea.New(new(testThingSetup)) - { - root.Child(&setKey{key: "alice", value: "apple"}) - root.Child(&setKey{key: "bob", value: "banana"}) - root.Child(&setKey{key: "carol", value: "cherry"}) - } + root.Child(&setKey{key: "alice", value: "apple"}) + root.Child(&setKey{key: "bob", value: "banana"}) + root.Child(&setKey{key: "carol", value: "cherry"}) - { - bob := root.Child(&setKey{key: "b ob", value: "banana"}) - bob.Child(&setKey{key: "car-el", value: "cherry"}) - dave := bob.Child(&setKey{key: "dave", value: "durian"}) - dave.Child(&setKey{key: "evan", value: "elderberry"}) - } + bob := root.Child(&setKey{key: "b ob", value: "banana"}) + bob.Child(&setKey{key: "car-el", value: "cherry"}) + dave := bob.Child(&setKey{key: "dave", value: "durian"}) + dave.Child(&setKey{key: "evan", value: "elderberry"}) - { - root.Child(&setKey{key: "al ice", bad: true}) - root.Child(&setKey{key: " alice", bad: true}) - root.Child(&setKey{key: "alice ", bad: true}) - } + root.Child(&setKey{key: "al ice", bad: true}) + root.Child(&setKey{key: " alice", bad: true}) + root.Child(&setKey{key: "alice ", bad: true}) tea.Run(t, root) } diff --git a/thing.go b/thing.go index d87fe4c..e3b63d4 100644 --- a/thing.go +++ b/thing.go @@ -12,6 +12,9 @@ var ( ) func validateKey(key string) error { + if strings.TrimSpace(key) == "" { + return fmt.Errorf("%w: key cannot be empty string", ErrInvalidKey) + } if strings.Count(key, " ") > 0 { return fmt.Errorf("%w: key cannot contain spaces", ErrInvalidKey) }