From 6f6070b22ecc134392557735923a2a20fb9410c1 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 24 Apr 2017 01:13:08 +0900 Subject: [PATCH] Add cmd follow test --- cmd/mstdn/cmd_follow_test.go | 42 +++++++++++++++++++++++++++++++++--- cmd/mstdn/cmd_test.go | 8 +++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cmd/mstdn/cmd_follow_test.go b/cmd/mstdn/cmd_follow_test.go index ea74526..f4c053b 100644 --- a/cmd/mstdn/cmd_follow_test.go +++ b/cmd/mstdn/cmd_follow_test.go @@ -14,8 +14,17 @@ func TestCmdFollow(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/v1/accounts/search": - fmt.Fprintln(w, `[{"id": 123}]`) - return + q := r.URL.Query().Get("q") + if q == "mattn" { + fmt.Fprintln(w, `[{"id": 123}]`) + return + } else if q == "different_id" { + fmt.Fprintln(w, `[{"id": 1234567}]`) + return + } else if q == "empty" { + fmt.Fprintln(w, `[]`) + return + } case "/api/v1/accounts/123/follow": fmt.Fprintln(w, `{"id": 123}`) ok = true @@ -25,7 +34,34 @@ func TestCmdFollow(t *testing.T) { return }, func(app *cli.App) { - app.Run([]string{"mstdn", "follow", "mattn"}) + err := app.Run([]string{"mstdn", "follow", "mattn"}) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + }, + func(app *cli.App) { + err := app.Run([]string{"mstdn", "follow"}) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + }, + func(app *cli.App) { + err := app.Run([]string{"mstdn", "follow", "fail"}) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + }, + func(app *cli.App) { + err := app.Run([]string{"mstdn", "follow", "empty"}) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + }, + func(app *cli.App) { + err := app.Run([]string{"mstdn", "follow", "different_id"}) + if err == nil { + t.Fatalf("should not be fail: %v", err) + } }, ) if !ok { diff --git a/cmd/mstdn/cmd_test.go b/cmd/mstdn/cmd_test.go index eb38282..5bba072 100644 --- a/cmd/mstdn/cmd_test.go +++ b/cmd/mstdn/cmd_test.go @@ -9,7 +9,7 @@ import ( "github.com/urfave/cli" ) -func testWithServer(h http.HandlerFunc, testFunc func(*cli.App)) string { +func testWithServer(h http.HandlerFunc, testFuncs ...func(*cli.App)) string { ts := httptest.NewServer(h) defer ts.Close() @@ -31,6 +31,10 @@ func testWithServer(h http.HandlerFunc, testFunc func(*cli.App)) string { Server: "https://example.com", }, } - testFunc(app) + + for _, f := range testFuncs { + f(app) + } + return buf.String() }