You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tea/examples/incr/tea_test.go

83 lines
2.2 KiB
Go

4 years ago
// +build !convey,!std
package incr
import (
"github.com/jordanorelli/tea"
"testing"
)
// testInt is a Test that does nothing. It always passes. It acts as a
// container for a value to be made available to future tests.
4 years ago
type testInt struct {
4 years ago
// the "save" struct tag instructs tea to save this field for future tests.
4 years ago
X int `tea:"save"`
}
// Run satisfies the tea.Test interface so that testInt may be used as a
// tea.Test
4 years ago
func (test *testInt) Run(t *testing.T) {
t.Logf("saving to future tests X = %d", test.X)
}
4 years ago
4 years ago
// testIncr increments an integer and checks that the result of incrementing
// the integer is an expected value.
4 years ago
type testIncr struct {
4 years ago
// the "load" struct tag instructs tea to load the value of this field from
// previous tests in this run. Like before, we also use a "save" tag.
X int `tea:"load,save"`
4 years ago
expect int
}
// Run satisfies the tea.Test interface
4 years ago
func (test *testIncr) Run(t *testing.T) {
4 years ago
t.Logf("loaded from parent tests X = %d", test.X)
4 years ago
test.X++
if test.X != test.expect {
t.Errorf("expected X to be %d, is %d instead", test.expect, test.X)
}
4 years ago
t.Logf("saving to future tests X = %d", test.X)
4 years ago
}
func TestOnce(t *testing.T) {
4 years ago
// we use testInt with X set to 1 as our starting test.
4 years ago
root := tea.New(&testInt{X: 1})
4 years ago
// after that test passes, we want to run a test of type testIncr.
4 years ago
root.Child(&testIncr{expect: 2})
4 years ago
4 years ago
tea.Run(t, root)
}
func TestTwice(t *testing.T) {
4 years ago
// same setup as in TestOnce
4 years ago
root := tea.New(&testInt{X: 1})
4 years ago
// just like before, we want to run a testIncr test after the root test
// passes.
4 years ago
root.Child(&testIncr{expect: 2})
4 years ago
// This testIncr and the other testIncr are siblings, since we called Child
// from the same node.
4 years ago
root.Child(&testIncr{expect: 2})
4 years ago
tea.Run(t, root)
}
func TestTwiceSeries(t *testing.T) {
// we use testInt with X set to 1 as our starting test.
root := tea.New(&testInt{X: 1})
// by now this should look familiar. The difference here is that we save
// the newly created node in the tree.
two := root.Child(&testIncr{expect: 2})
// adding a child to two here
two.Child(&testIncr{expect: 3})
// but of course, we can still add new children to the root.
root.Child(&testIncr{expect: 2})
4 years ago
tea.Run(t, root)
}