|
|
|
@ -8,9 +8,6 @@ import (
|
|
|
|
|
"github.com/jordanorelli/tea"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// testStartServer is a test that checks that the server can start. If this
|
|
|
|
|
// test passes, we retain a reference to the server for future tests to
|
|
|
|
|
// utilize.
|
|
|
|
|
type testStartServer struct {
|
|
|
|
|
Server *httptest.Server `tea:"save"`
|
|
|
|
|
}
|
|
|
|
@ -26,16 +23,14 @@ func (test *testStartServer) After(t *testing.T) {
|
|
|
|
|
test.Server.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testHits sends a request to a hitcount server created in a previous test,
|
|
|
|
|
// checking that the number of hits returned matches what we expect.
|
|
|
|
|
type testHits struct {
|
|
|
|
|
type testRequest struct {
|
|
|
|
|
Server *httptest.Server `tea:"load"`
|
|
|
|
|
|
|
|
|
|
path string
|
|
|
|
|
hits int
|
|
|
|
|
expect int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (test *testHits) Run(t *testing.T) {
|
|
|
|
|
func (test *testRequest) Run(t *testing.T) {
|
|
|
|
|
client := test.Server.Client()
|
|
|
|
|
|
|
|
|
|
res, err := client.Get(test.Server.URL + test.path)
|
|
|
|
@ -49,39 +44,50 @@ func (test *testHits) Run(t *testing.T) {
|
|
|
|
|
t.Fatalf("response at %s was not json: %v", test.path, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if body.Hits != test.hits {
|
|
|
|
|
t.Errorf("expected a count of %d hits but saw %d instead", test.hits, body.Hits)
|
|
|
|
|
if body.Hits != test.expect {
|
|
|
|
|
t.Errorf("expected a count of %d but saw %d", test.expect, body.Hits)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestServer(t *testing.T) {
|
|
|
|
|
// start with a root node that creates our test server
|
|
|
|
|
root := tea.New(&testStartServer{})
|
|
|
|
|
|
|
|
|
|
// add a child node: this test is run if the root test passes. If the root
|
|
|
|
|
// test is failed, this test and all of its descendents are logged as
|
|
|
|
|
// skipped.
|
|
|
|
|
one := root.Child(&testHits{path: "/alice", hits: 1})
|
|
|
|
|
type list []testRequest
|
|
|
|
|
|
|
|
|
|
// the effects of the first test create the initial state for the second test.
|
|
|
|
|
two := one.Child(&testHits{path: "/alice", hits: 2})
|
|
|
|
|
|
|
|
|
|
// since we have never visited /bob, we know that bob should only have one hit.
|
|
|
|
|
two.Child(&testHits{path: "/bob", hits: 1})
|
|
|
|
|
runSeries := func(node *tea.Tree, tests list) *tea.Tree {
|
|
|
|
|
for i := 0; i < len(tests); i++ {
|
|
|
|
|
node = node.Child(&tests[i])
|
|
|
|
|
}
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// but we could also run the exact same test off of the root, like so:
|
|
|
|
|
root.Child(&testHits{path: "/bob", hits: 1})
|
|
|
|
|
runSiblings := func(node *tea.Tree, tests list) {
|
|
|
|
|
for i := 0; i < len(tests); i++ {
|
|
|
|
|
node.Child(&tests[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// since tests are values in tea, we can re-use the exact same test from
|
|
|
|
|
// different initial states by saving the test as a variable.
|
|
|
|
|
bob := &testHits{path: "/bob", hits: 1}
|
|
|
|
|
root := tea.New(&testStartServer{})
|
|
|
|
|
|
|
|
|
|
// these two executions of the same test value are operating on different
|
|
|
|
|
// program states. Since they are not in the same sequence, they have no
|
|
|
|
|
// effect on one another, even though they're utilizing the same test
|
|
|
|
|
// value.
|
|
|
|
|
two.Child(bob)
|
|
|
|
|
root.Child(bob)
|
|
|
|
|
runSeries(root, list{
|
|
|
|
|
{path: "/users/alice", expect: 1},
|
|
|
|
|
{path: "/users/alice", expect: 2},
|
|
|
|
|
{path: "/users/alice", expect: 3},
|
|
|
|
|
{path: "/users/alice", expect: 4},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
runSeries(root, list{
|
|
|
|
|
{path: "/users/bob", expect: 1},
|
|
|
|
|
{path: "/users/alice", expect: 1},
|
|
|
|
|
{path: "/users/alice", expect: 2},
|
|
|
|
|
{path: "/users/alice", expect: 3},
|
|
|
|
|
{path: "/users/bob", expect: 2},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
runSiblings(root, list{
|
|
|
|
|
{path: "/users/alice", expect: 1},
|
|
|
|
|
{path: "/users/alice", expect: 1},
|
|
|
|
|
{path: "/users/alice", expect: 1},
|
|
|
|
|
{path: "/users/alice", expect: 1},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
tea.Run(t, root)
|
|
|
|
|
}
|
|
|
|
|