From 849990d37b8574a612c56acbb679e2aa0a336cc0 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Thu, 30 Jul 2020 14:23:03 +0000 Subject: [PATCH] a bit like a table-driven test huh --- examples/echo-server/main.go | 36 +++++++++++++ examples/echo-server/server.go | 40 +++++++++++++++ examples/echo-server/server_test.go | 80 +++++++++++++++++++++++++++++ examples/go.mod | 2 +- examples/go.sum | 2 + 5 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 examples/echo-server/main.go create mode 100644 examples/echo-server/server.go create mode 100644 examples/echo-server/server_test.go diff --git a/examples/echo-server/main.go b/examples/echo-server/main.go new file mode 100644 index 0000000..5271592 --- /dev/null +++ b/examples/echo-server/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "net" + "net/http" + "os" +) + +func main() { + addr := "127.0.0.1:54321" + + switch len(os.Args) { + case 0: + fmt.Fprintln(os.Stderr, "how did you even get here") + os.Exit(1) + case 1: + break + case 2: + addr = os.Args[1] + case 3: + addr = fmt.Sprintf("%s:%s", os.Args[1], os.Args[2]) + default: + fmt.Fprintln(os.Stderr, "too many args") + os.Exit(1) + } + + lis, err := net.Listen("tcp", addr) + if err != nil { + fmt.Fprintf(os.Stderr, "error: %s\n", err.Error()) + os.Exit(1) + } + + fmt.Printf("listening on: %s\n", addr) + http.Serve(lis, &server{counters: make(map[string]int)}) +} diff --git a/examples/echo-server/server.go b/examples/echo-server/server.go new file mode 100644 index 0000000..1d86705 --- /dev/null +++ b/examples/echo-server/server.go @@ -0,0 +1,40 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "sync" +) + +type response struct { + OK bool `json:"ok"` + Hits int `json:"hits"` +} + +type server struct { + sync.Mutex + counters map[string]int +} + +func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + hits := s.hit(r.URL.Path) + + fmt.Printf("% 8d %s\n", hits, r.URL.Path) + + w.Header().Add("Content-Type", "application/json") + json.NewEncoder(w).Encode(response{OK: true, Hits: hits}) +} + +func (s *server) hit(path string) int { + s.Lock() + defer s.Unlock() + + if s.counters == nil { + s.counters = map[string]int{path: 1} + return 1 + } + + s.counters[path]++ + return s.counters[path] +} diff --git a/examples/echo-server/server_test.go b/examples/echo-server/server_test.go new file mode 100644 index 0000000..354293a --- /dev/null +++ b/examples/echo-server/server_test.go @@ -0,0 +1,80 @@ +package main + +import ( + "encoding/json" + "net/http/httptest" + "testing" + + "github.com/jordanorelli/tea" +) + +type testStartServer struct { + Server *httptest.Server `tea:"save"` +} + +func (test *testStartServer) Run(t *testing.T) { + handler := new(server) + test.Server = httptest.NewServer(handler) + t.Logf("started a test server on: %s", test.Server.URL) +} + +func (test *testStartServer) After(t *testing.T) { + t.Logf("closing a test server on: %s", test.Server.URL) + test.Server.Close() +} + +type testRequest struct { + Server *httptest.Server `tea:"load"` + + path string + expect int +} + +func (test *testRequest) Run(t *testing.T) { + client := test.Server.Client() + + res, err := client.Get(test.Server.URL + test.path) + if err != nil { + t.Fatalf("request to %s failed: %v", test.path, err) + } + defer res.Body.Close() + + var body response + if err := json.NewDecoder(res.Body).Decode(&body); err != nil { + t.Fatalf("response at %s was not json: %v", test.path, err) + } + + if body.Hits != test.expect { + t.Errorf("expected a count of %d but saw %d", test.expect, body.Hits) + } +} + +func TestServer(t *testing.T) { + type series []testRequest + + runSeries := func(node *tea.Tree, tests series) *tea.Tree { + for i, _ := range tests { + node = node.Child(&tests[i]) + } + return node + } + + root := tea.New(&testStartServer{}) + + runSeries(root, series{ + {path: "/users/alice", expect: 1}, + {path: "/users/alice", expect: 2}, + {path: "/users/alice", expect: 3}, + {path: "/users/alice", expect: 4}, + }) + + runSeries(root, series{ + {path: "/users/alice", expect: 1}, + {path: "/users/bob", expect: 1}, + {path: "/users/alice", expect: 2}, + {path: "/users/alice", expect: 3}, + {path: "/users/bob", expect: 2}, + }) + + tea.Run(t, root) +} diff --git a/examples/go.mod b/examples/go.mod index ab2c86f..d6c5da0 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -3,6 +3,6 @@ module github.com/jordanorelli/tea/examples go 1.14 require ( - github.com/jordanorelli/tea v0.0.3 + github.com/jordanorelli/tea v0.0.5-0.20200730012141-ef32839cd424 github.com/smartystreets/goconvey v1.6.4 ) diff --git a/examples/go.sum b/examples/go.sum index c2d123b..58c4dad 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,6 +1,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/jordanorelli/tea v0.0.3 h1:6FNc5NE3O+2RDgK9nq3x8hZ0b5w5bFcz94VG67jbRbo= github.com/jordanorelli/tea v0.0.3/go.mod h1:mnnRKfuTTk8d+3rcOC6TIiBOLVG8RQitOtFtCfQo8Bw= +github.com/jordanorelli/tea v0.0.5-0.20200730012141-ef32839cd424 h1:8ZD+nW5GiHLU0vs/uLzvklxZNwRvHgCQSEMDnv4yLWY= +github.com/jordanorelli/tea v0.0.5-0.20200730012141-ef32839cd424/go.mod h1:mnnRKfuTTk8d+3rcOC6TIiBOLVG8RQitOtFtCfQo8Bw= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=