clone supplied test values when running setup

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

@ -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()) }

@ -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 {

@ -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)
}

@ -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)
}

Loading…
Cancel
Save