From a3346f59ccfc3fc829863a08c94b0c97981a6281 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Fri, 31 Jul 2020 13:31:29 +0000 Subject: [PATCH] export a Passing type I keep writing these one-off when I actually use tea. It seems like it's worth including. It's especially useful for the root test, where you don't necessarily want to create a test, but want to create a starting environment. Exporting a type that always passes is preferrable to making an explicit environment type, since we can maintain our guarantees that our environment isn't an invalid value. --- env.go | 4 ++++ env_test.go | 22 +++++++++---------- examples/{echo-server => hit-counter}/main.go | 0 .../{echo-server => hit-counter}/server.go | 3 +++ .../server_test.go | 0 test.go | 12 +++++----- tree.go | 6 ++--- 7 files changed, 28 insertions(+), 19 deletions(-) rename examples/{echo-server => hit-counter}/main.go (100%) rename examples/{echo-server => hit-counter}/server.go (75%) rename examples/{echo-server => hit-counter}/server_test.go (100%) diff --git a/env.go b/env.go index aaf7262..0827069 100644 --- a/env.go +++ b/env.go @@ -25,6 +25,10 @@ func (e *env) save(test Test) *env { } T := V.Type() + if T.Kind() != reflect.Struct { + return e + } + saved := make(map[string]interface{}) for i := 0; i < T.NumField(); i++ { f := T.Field(i) diff --git a/env_test.go b/env_test.go index 11c184a..0000618 100644 --- a/env_test.go +++ b/env_test.go @@ -6,7 +6,7 @@ import ( func TestSave(t *testing.T) { t.Run("empty begets nil", func(t *testing.T) { - e := mkenv(new(empty)) + e := mkenv(Pass) if e != nil { t.Errorf("saw unexpected env value looking for nil: %v", e) } @@ -14,7 +14,7 @@ func TestSave(t *testing.T) { t.Run("unexported fields are ignored", func(t *testing.T) { type test struct { - empty + Passing foo int `tea:"save"` } @@ -25,7 +25,7 @@ func TestSave(t *testing.T) { t.Run("create an env from a test", func(t *testing.T) { test := struct { - empty + Passing Foo int `tea:"save"` }{ Foo: 5, @@ -48,7 +48,7 @@ func TestSave(t *testing.T) { t.Run("update an existing env", func(t *testing.T) { test := struct { - empty + Passing Foo int `tea:"save"` }{ Foo: 5, @@ -77,7 +77,7 @@ func TestLoad(t *testing.T) { } var test struct { - empty + Passing Foo int `tea:"load"` } @@ -95,7 +95,7 @@ func TestLoad(t *testing.T) { } var test struct { - empty + Passing Foo int `tea:"load"` } @@ -110,7 +110,7 @@ func TestLoad(t *testing.T) { } var test struct { - empty + Passing Foo int `tea:"load"` } test.Foo = 5 @@ -131,7 +131,7 @@ func TestMatch(t *testing.T) { } var test struct { - empty + Passing Name string `tea:"match"` Foo int `tea:"load"` } @@ -150,7 +150,7 @@ func TestMatch(t *testing.T) { } var test struct { - empty + Passing Name string `tea:"match"` Foo int `tea:"load"` } @@ -170,7 +170,7 @@ func TestMatch(t *testing.T) { } var test struct { - empty + Passing Name string `tea:"match"` Foo int `tea:"load"` } @@ -199,7 +199,7 @@ func TestMatch(t *testing.T) { } var test struct { - empty + Passing Name string `tea:"match"` Foo int `tea:"load"` } diff --git a/examples/echo-server/main.go b/examples/hit-counter/main.go similarity index 100% rename from examples/echo-server/main.go rename to examples/hit-counter/main.go diff --git a/examples/echo-server/server.go b/examples/hit-counter/server.go similarity index 75% rename from examples/echo-server/server.go rename to examples/hit-counter/server.go index 1d86705..fe2fc7e 100644 --- a/examples/echo-server/server.go +++ b/examples/hit-counter/server.go @@ -12,6 +12,9 @@ type response struct { Hits int `json:"hits"` } +// server implements an http hit-counter server. The hit-count server responds +// to GET requests with the number of responses it has seen for that path, +// inclusive of the request itself (i.e., starting at 1). type server struct { sync.Mutex counters map[string]int diff --git a/examples/echo-server/server_test.go b/examples/hit-counter/server_test.go similarity index 100% rename from examples/echo-server/server_test.go rename to examples/hit-counter/server_test.go diff --git a/test.go b/test.go index 58cfbde..7bd6c3c 100644 --- a/test.go +++ b/test.go @@ -33,12 +33,14 @@ func (f failure) Run(t *testing.T) { t.Error(f.cause.Error()) } -// empty is an empty test. It does nothing when run, it's just used as a -// sentinel value to create notes in the test graph and for ... testing the tea -// package itself. -type empty struct{} +// Pass is a Test value that always passes. +const Pass = Passing("test passed") -func (e empty) Run(t *testing.T) {} +// Passing is a Test type that always passes. Every value of the Passing type, +// including the zero value, is a test that will always pass. +type Passing string + +func (p Passing) Run(t *testing.T) {} // parseName parses the name for a given test func parseName(test Test) string { diff --git a/tree.go b/tree.go index 356d04b..4bafc79 100644 --- a/tree.go +++ b/tree.go @@ -53,10 +53,10 @@ func exec(t *testing.T, tree *Tree) ([]Test, *env) { history, e := exec(t, tree.parent) test := clone(tree.test) if err := e.load(test); err != nil { - // TODO: figure out how to handle load failures like this. - panic("load failure: " + err.Error()) + t.Errorf("test plan failed: %s", err) + } else { + test.Run(t) } - test.Run(t) return append([]Test{test}, history...), e.save(test) }