Merge pull request #41 from 178inaba/cmd_follow_test

Cmd follow test
This commit is contained in:
mattn 2017-04-24 09:47:59 +09:00 committed by GitHub
commit 2ae2a90336
2 changed files with 45 additions and 5 deletions

View File

@ -14,8 +14,17 @@ func TestCmdFollow(t *testing.T) {
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path { switch r.URL.Path {
case "/api/v1/accounts/search": case "/api/v1/accounts/search":
fmt.Fprintln(w, `[{"id": 123}]`) q := r.URL.Query().Get("q")
return 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": case "/api/v1/accounts/123/follow":
fmt.Fprintln(w, `{"id": 123}`) fmt.Fprintln(w, `{"id": 123}`)
ok = true ok = true
@ -25,7 +34,34 @@ func TestCmdFollow(t *testing.T) {
return return
}, },
func(app *cli.App) { 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 { if !ok {

View File

@ -9,7 +9,7 @@ import (
"github.com/urfave/cli" "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) ts := httptest.NewServer(h)
defer ts.Close() defer ts.Close()
@ -31,6 +31,10 @@ func testWithServer(h http.HandlerFunc, testFunc func(*cli.App)) string {
Server: "https://example.com", Server: "https://example.com",
}, },
} }
testFunc(app)
for _, f := range testFuncs {
f(app)
}
return buf.String() return buf.String()
} }