From 989bb7746d806f879e3059ec587b7b4338d101cf Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 29 Jul 2020 16:01:31 +0000 Subject: [PATCH] an example --- examples/incr/README.md | 21 ++++++++++++++++ examples/incr/convey_test.go | 46 ++++++++++++++++++++++++++++++++++++ examples/incr/std_test.go | 37 +++++++++++++++++++++++++++++ examples/incr/tea_test.go | 39 ++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 examples/incr/README.md create mode 100644 examples/incr/convey_test.go create mode 100644 examples/incr/std_test.go create mode 100644 examples/incr/tea_test.go diff --git a/examples/incr/README.md b/examples/incr/README.md new file mode 100644 index 0000000..8297a6a --- /dev/null +++ b/examples/incr/README.md @@ -0,0 +1,21 @@ +increment test += + +This is an example of an increment test. This is the example used in the +Goconvey docs, so I use it as an example to show how patterns that people have +seen before may be expressed in tea. + +The same tests are written using different testing tools. Build flags are used +to control which of the testing frameworks is being used. The reason we use +build flags here is because some of the examples are not expressible in some +of the other testing frameworks; those examples exist to show how something +might unexpectedly fail in one framework but work in tea. + +Files beginning with `std_` are written using only the standard library +testing framework on its own. Files beginning with `convey_` are written using +Goconvey, a BDD framework. Files beginning with `tea_` are written using tea. + +- `go test -tags std` runs the standard library tests +- `go test -tags convey` runs the Goconvey tests +- `go test` runs the tea tests. This is the default since we want to give + examples of how to use tea; the tea tests should be runnable with simply `go test` diff --git a/examples/incr/convey_test.go b/examples/incr/convey_test.go new file mode 100644 index 0000000..6682cd1 --- /dev/null +++ b/examples/incr/convey_test.go @@ -0,0 +1,46 @@ +// +build convey + +package incr + +import ( + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestOnce(t *testing.T) { + Convey("Given some integer with a starting value", t, func() { + x := 1 + + Convey("When the integer is incremented", func() { + x++ + + Convey("The value should be greater by one", func() { + So(x, ShouldEqual, 2) + }) + }) + }) +} + +func TestTwice(t *testing.T) { + Convey("Given some integer with a starting value", t, func() { + x := 1 + + Convey("When the integer is incremented", func() { + x++ + + Convey("The value should be greater by one", func() { + So(x, ShouldEqual, 2) + }) + }) + + // the name has to be updated, if you try to use the name twice, Convey + // explodes. + Convey("When the integer is incremented again", func() { + x++ + + Convey("The value should be greater by one", func() { + So(x, ShouldEqual, 2) + }) + }) + }) +} diff --git a/examples/incr/std_test.go b/examples/incr/std_test.go new file mode 100644 index 0000000..4ce0a97 --- /dev/null +++ b/examples/incr/std_test.go @@ -0,0 +1,37 @@ +// +build std + +package incr + +import ( + "testing" +) + +func TestOnce(t *testing.T) { + x := 1 + + t.Run("increment", func(t *testing.T) { + x++ + if x != 2 { + t.Errorf("expected x to be 2, is %d instead", x) + } + }) +} + +func TestTwice(t *testing.T) { + x := 1 + + t.Run("increment", func(t *testing.T) { + x++ + if x != 2 { + t.Errorf("expected x to be 2, is %d instead", x) + } + }) + + // this fails, because both subtests close over the same x. + t.Run("increment", func(t *testing.T) { + x++ + if x != 2 { + t.Errorf("expected x to be 2, is %d instead", x) + } + }) +} diff --git a/examples/incr/tea_test.go b/examples/incr/tea_test.go new file mode 100644 index 0000000..03125ef --- /dev/null +++ b/examples/incr/tea_test.go @@ -0,0 +1,39 @@ +// +build !convey,!std + +package incr + +import ( + "github.com/jordanorelli/tea" + "testing" +) + +type testInt struct { + X int `tea:"save"` +} + +func (testInt) Run(t *testing.T) {} + +type testIncr struct { + X int `tea:"load"` + expect int +} + +func (test *testIncr) Run(t *testing.T) { + test.X++ + if test.X != test.expect { + t.Errorf("expected X to be %d, is %d instead", test.expect, test.X) + } +} + +func TestOnce(t *testing.T) { + root := tea.New(&testInt{X: 1}) + root.Child(&testIncr{expect: 2}) + tea.Run(t, root) +} + +func TestTwice(t *testing.T) { + root := tea.New(&testInt{X: 1}) + root.Child(&testIncr{expect: 2}) + root.Child(&testIncr{expect: 2}) + tea.Run(t, root) +}