clone supplied test values when running setup

g-counter
Jordan Orelli 4 years ago
parent 6ef7afe907
commit 2853ecfb38

@ -1,7 +1,20 @@
package tea package tea
import "testing" import (
"fmt"
"testing"
)
type Test interface { type Test interface {
Run(*testing.T) 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()) }

@ -1,7 +1,7 @@
package tea package tea
import ( import (
// "reflect" "reflect"
"testing" "testing"
) )
@ -10,7 +10,7 @@ func Run(t *testing.T, tree *Tree) {
t.Run(tree.name, func(t *testing.T) { t.Run(tree.name, func(t *testing.T) {
setup(t, tree) setup(t, tree)
tree.test.Run(t) clone(tree.test).Run(t)
for _, child := range tree.children { for _, child := range tree.children {
if t.Failed() || t.Skipped() { if t.Failed() || t.Skipped() {
@ -25,7 +25,7 @@ func Run(t *testing.T, tree *Tree) {
func setup(t *testing.T, tree *Tree) { func setup(t *testing.T, tree *Tree) {
if tree.parent != nil { if tree.parent != nil {
setup(t, tree.parent) 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 return child
} }
// func clone(t Test) Test { func clone(t Test) Test {
// T := reflect.TypeOf(t) srcV := reflect.ValueOf(t).Elem()
// switch T.Kind() { destV := reflect.New(srcV.Type())
// case reflect.Struct: destV.Elem().Set(srcV)
// } return destV.Interface().(Test)
// } }
func parseName(test Test) string { func parseName(test Test) string {
if s, ok := test.(interface{ String() string }); ok { if s, ok := test.(interface{ String() string }); ok {

@ -16,7 +16,7 @@ type testThingSetup struct {
func (test *testThingSetup) Run(t *testing.T) { func (test *testThingSetup) Run(t *testing.T) {
t.Logf("[%s] running testThingSetup", t.Name()) t.Logf("[%s] running testThingSetup", t.Name())
if test.thing != nil { if test.thing != nil {
// t.Fatal("should be nil") t.Fatal("should be nil")
} }
test.thing = new(Thing) test.thing = new(Thing)
} }
@ -49,24 +49,18 @@ func (test *setKey) Run(t *testing.T) {
func TestThing(t *testing.T) { func TestThing(t *testing.T) {
root := tea.New(new(testThingSetup)) root := tea.New(new(testThingSetup))
{ root.Child(&setKey{key: "alice", value: "apple"})
root.Child(&setKey{key: "alice", value: "apple"}) root.Child(&setKey{key: "bob", value: "banana"})
root.Child(&setKey{key: "bob", value: "banana"}) root.Child(&setKey{key: "carol", value: "cherry"})
root.Child(&setKey{key: "carol", value: "cherry"})
}
{ bob := root.Child(&setKey{key: "b ob", value: "banana"})
bob := root.Child(&setKey{key: "b ob", value: "banana"}) bob.Child(&setKey{key: "car-el", value: "cherry"})
bob.Child(&setKey{key: "car-el", value: "cherry"}) dave := bob.Child(&setKey{key: "dave", value: "durian"})
dave := bob.Child(&setKey{key: "dave", value: "durian"}) dave.Child(&setKey{key: "evan", value: "elderberry"})
dave.Child(&setKey{key: "evan", value: "elderberry"})
}
{ root.Child(&setKey{key: "al ice", bad: true})
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: "alice ", bad: true})
root.Child(&setKey{key: "alice ", bad: true})
}
tea.Run(t, root) tea.Run(t, root)
} }

@ -12,6 +12,9 @@ var (
) )
func validateKey(key string) error { func validateKey(key string) error {
if strings.TrimSpace(key) == "" {
return fmt.Errorf("%w: key cannot be empty string", ErrInvalidKey)
}
if strings.Count(key, " ") > 0 { if strings.Count(key, " ") > 0 {
return fmt.Errorf("%w: key cannot contain spaces", ErrInvalidKey) return fmt.Errorf("%w: key cannot contain spaces", ErrInvalidKey)
} }

Loading…
Cancel
Save