env uses layers

g-counter
Jordan Orelli 4 years ago
parent decbb52f26
commit 1e1bbcd3d9

@ -0,0 +1,7 @@
/*
Package tea provides a tree structure for writing stateful tests, such that
child tests may utilize the side-effects of their ancestors.
*/
package tea

@ -6,8 +6,7 @@ import (
) )
type env struct { type env struct {
key string data map[string]interface{}
value interface{}
parent *env parent *env
} }
@ -17,7 +16,8 @@ func mkenv(test Test) *env {
} }
// save looks at the Test t and saves the values of its fields marked with a // save looks at the Test t and saves the values of its fields marked with a
// save tag // save tag. All of the fields for that tests are stored together as a data
// layer.
func (e *env) save(test Test) *env { func (e *env) save(test Test) *env {
V := reflect.ValueOf(test) V := reflect.ValueOf(test)
if V.Type().Kind() == reflect.Ptr { if V.Type().Kind() == reflect.Ptr {
@ -25,6 +25,7 @@ func (e *env) save(test Test) *env {
} }
T := V.Type() T := V.Type()
saved := make(map[string]interface{})
for i := 0; i < T.NumField(); i++ { for i := 0; i < T.NumField(); i++ {
f := T.Field(i) f := T.Field(i)
if !isSaveField(f) { if !isSaveField(f) {
@ -32,9 +33,11 @@ func (e *env) save(test Test) *env {
} }
fv := V.Field(i) fv := V.Field(i)
e = &env{ saved[f.Name] = fv.Interface()
key: f.Name, }
value: fv.Interface(), if len(saved) > 0 {
return &env{
data: saved,
parent: e, parent: e,
} }
} }
@ -55,15 +58,18 @@ func (e *env) load(dest Test) error {
set := false set := false
for e := e; e != nil; e = e.parent { for e := e; e != nil; e = e.parent {
if e.key == f.Name { v, ok := e.data[f.Name]
ev := reflect.ValueOf(e.value) if !ok {
continue
}
ev := reflect.ValueOf(v)
if ev.Type().AssignableTo(fv.Type()) { if ev.Type().AssignableTo(fv.Type()) {
set = true set = true
fv.Set(ev) fv.Set(ev)
break break
} }
} }
}
if !set { if !set {
return fmt.Errorf("failed to set required field: %q", f.Name) return fmt.Errorf("failed to set required field: %q", f.Name)

@ -41,12 +41,13 @@ func TestSave(t *testing.T) {
t.Fatalf("saw nil env when expecting a valid env") t.Fatalf("saw nil env when expecting a valid env")
} }
if e.key != "Foo" { foo, ok := e.data["Foo"]
t.Errorf("expected key %q but saw %q instead", "Foo", e.key) if !ok {
t.Errorf("expected field Foo to be saved but was not saved")
} }
if e.value != 5 { if foo != 5 {
t.Errorf("expected value %v but saw %v instead", 5, e.value) t.Errorf("expected value %v but saw %v instead", 5, foo)
} }
}) })

@ -1 +0,0 @@
package tea
Loading…
Cancel
Save