diff --git a/comment_test.go b/comment_test.go index 5a06043..c61715a 100644 --- a/comment_test.go +++ b/comment_test.go @@ -4,9 +4,10 @@ import ( "fmt" "net/http" "net/url" - "reflect" "testing" "time" + + "github.com/stretchr/testify/assert" ) var expectedCommentSubmitOrEdit = &Comment{ @@ -19,15 +20,19 @@ var expectedCommentSubmitOrEdit = &Comment{ Author: "reddit_username", AuthorID: "t2_user1", AuthorFlairText: "Flair", + AuthorFlairID: "024b2b66-05ca-11e1-96f4-12313d096aae", Subreddit: "subreddit", SubredditNamePrefixed: "r/subreddit", SubredditID: "t5_test", + Likes: Bool(true), + Score: 1, Controversiality: 0, - Created: &Timestamp{time.Date(2020, 4, 28, 20, 9, 47, 0, time.UTC)}, + Created: &Timestamp{time.Date(2020, 4, 29, 0, 9, 47, 0, time.UTC)}, + Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, LinkID: "t3_link1", } @@ -39,7 +44,7 @@ func TestCommentServiceOp_Submit(t *testing.T) { commentBlob := readFileContents(t, "testdata/comment-submit-edit.json") mux.HandleFunc("/api/comment", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("api_type", "json") @@ -47,22 +52,16 @@ func TestCommentServiceOp_Submit(t *testing.T) { form.Set("parent", "t1_test") form.Set("text", "test comment") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, commentBlob) }) comment, _, err := client.Comment.Submit(ctx, "t1_test", "test comment") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := expectedCommentSubmitOrEdit, comment; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, expectedCommentSubmitOrEdit, comment) } func TestCommentServiceOp_Edit(t *testing.T) { @@ -72,7 +71,7 @@ func TestCommentServiceOp_Edit(t *testing.T) { commentBlob := readFileContents(t, "testdata/comment-submit-edit.json") mux.HandleFunc("/api/editusertext", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("api_type", "json") @@ -80,30 +79,19 @@ func TestCommentServiceOp_Edit(t *testing.T) { form.Set("thing_id", "t1_test") form.Set("text", "test comment") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, commentBlob) }) _, _, err := client.Comment.Edit(ctx, "t3_test", "test comment") - if err == nil { - t.Fatal("expected error, got nothing instead") - } - if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`) comment, _, err := client.Comment.Edit(ctx, "t1_test", "test comment") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := expectedCommentSubmitOrEdit, comment; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, expectedCommentSubmitOrEdit, comment) } func TestCommentServiceOp_Delete(t *testing.T) { @@ -111,35 +99,24 @@ func TestCommentServiceOp_Delete(t *testing.T) { defer teardown() mux.HandleFunc("/api/del", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "t1_test") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) _, err := client.Comment.Delete(ctx, "t3_test") - if err == nil { - t.Fatal("expected error, got nothing instead") - } - if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`) res, err := client.Comment.Delete(ctx, "t1_test") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := http.StatusOK, res.StatusCode; expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } func TestCommentServiceOp_Save(t *testing.T) { @@ -147,35 +124,24 @@ func TestCommentServiceOp_Save(t *testing.T) { defer teardown() mux.HandleFunc("/api/save", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "t1_test") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) _, err := client.Comment.Save(ctx, "t3_test") - if err == nil { - t.Fatal("expected error, got nothing instead") - } - if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`) res, err := client.Comment.Save(ctx, "t1_test") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := http.StatusOK, res.StatusCode; expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } func TestCommentServiceOp_Unsave(t *testing.T) { @@ -183,33 +149,22 @@ func TestCommentServiceOp_Unsave(t *testing.T) { defer teardown() mux.HandleFunc("/api/unsave", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "t1_test") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) _, err := client.Comment.Unsave(ctx, "t3_test") - if err == nil { - t.Fatal("expected error, got nothing instead") - } - if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`) res, err := client.Comment.Unsave(ctx, "t1_test") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := http.StatusOK, res.StatusCode; expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } diff --git a/flair_test.go b/flair_test.go index 70f1716..015de01 100644 --- a/flair_test.go +++ b/flair_test.go @@ -3,8 +3,9 @@ package geddit import ( "fmt" "net/http" - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) var expectedFlairs = []Flair{ @@ -46,18 +47,13 @@ func TestFlairServiceOp_GetFlairs(t *testing.T) { flairsBlob := readFileContents(t, "testdata/flairs.json") mux.HandleFunc("/r/subreddit/api/user_flair", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodGet) + assert.Equal(t, http.MethodGet, r.Method) fmt.Fprint(w, flairsBlob) }) flairs, _, err := client.Flair.GetFromSubreddit(ctx, "subreddit") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := expectedFlairs, flairs; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, expectedFlairs, flairs) } func TestFlairServiceOp_GetFlairsV2(t *testing.T) { @@ -67,16 +63,11 @@ func TestFlairServiceOp_GetFlairsV2(t *testing.T) { flairsV2Blob := readFileContents(t, "testdata/flairs-v2.json") mux.HandleFunc("/r/subreddit/api/user_flair_v2", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodGet) + assert.Equal(t, http.MethodGet, r.Method) fmt.Fprint(w, flairsV2Blob) }) flairs, _, err := client.Flair.GetFromSubredditV2(ctx, "subreddit") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := expectedFlairsV2, flairs; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, expectedFlairsV2, flairs) } diff --git a/geddit_test.go b/geddit_test.go index 9a1fd2f..36d76ee 100644 --- a/geddit_test.go +++ b/geddit_test.go @@ -10,6 +10,8 @@ import ( "os" "reflect" "testing" + + "github.com/stretchr/testify/assert" ) var ( @@ -47,12 +49,6 @@ func teardown() { server.Close() } -func testMethod(t *testing.T, r *http.Request, expected string) { - if expected != r.Method { - t.Fatalf("Request method = %v, expected %v", r.Method, expected) - } -} - func readFileContents(t *testing.T, filepath string) string { file, err := os.Open(filepath) if err != nil { @@ -102,9 +98,7 @@ func testClientDefaults(t *testing.T, c *Client) { func TestNewClient(t *testing.T) { c, err := NewClient(nil) - if err != nil { - t.Fatalf("got unexpected error: %+v", err) - } + assert.NoError(t, err) testClientDefaults(t, c) } @@ -114,11 +108,5 @@ func TestNewClient_Error(t *testing.T) { } _, err := NewClient(nil, errorOpt) - if err == nil { - t.Fatal("expected error, got nothing instead") - } - - if expect, actual := "foo", err.Error(); expect != actual { - t.Fatalf("got unexpected error\nexpect: %+v\nactual: %+v", Stringify(expect), Stringify(actual)) - } + assert.EqualError(t, err, "foo") } diff --git a/go.mod b/go.mod index 7d81e92..f12d092 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.14 require ( github.com/google/go-querystring v1.0.0 + github.com/stretchr/testify v1.5.1 golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d ) diff --git a/go.sum b/go.sum index bb9f2d6..8fb65c7 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,15 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= @@ -18,3 +25,6 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/link_test.go b/link_test.go index ecff639..9c101f9 100644 --- a/link_test.go +++ b/link_test.go @@ -4,8 +4,9 @@ import ( "fmt" "net/http" "net/url" - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) func TestLinkServiceOp_Hide(t *testing.T) { @@ -13,35 +14,24 @@ func TestLinkServiceOp_Hide(t *testing.T) { defer teardown() mux.HandleFunc("/api/hide", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "1,2,3") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) _, err := client.Link.Hide(ctx) - if err == nil { - t.Fatal("expected error, got nothing instead") - } - if expect, actual := `must provide at least 1 id`, err.Error(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.EqualError(t, err, "must provide at least 1 id") res, err := client.Link.Hide(ctx, "1", "2", "3") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := http.StatusOK, res.StatusCode; expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } func TestLinkServiceOp_Unhide(t *testing.T) { @@ -49,33 +39,22 @@ func TestLinkServiceOp_Unhide(t *testing.T) { defer teardown() mux.HandleFunc("/api/unhide", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "1,2,3") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) _, err := client.Link.Unhide(ctx) - if err == nil { - t.Fatal("expected error, got nothing instead") - } - if expect, actual := `must provide at least 1 id`, err.Error(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.EqualError(t, err, "must provide at least 1 id") res, err := client.Link.Unhide(ctx, "1", "2", "3") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := http.StatusOK, res.StatusCode; expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } diff --git a/vote_test.go b/vote_test.go index 3745f8f..2cf05c5 100644 --- a/vote_test.go +++ b/vote_test.go @@ -4,8 +4,9 @@ import ( "fmt" "net/http" "net/url" - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) func TestVoteServiceOp_Up(t *testing.T) { @@ -13,25 +14,23 @@ func TestVoteServiceOp_Up(t *testing.T) { defer teardown() mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "t3_test") form.Set("dir", fmt.Sprint(upvote)) form.Set("rank", "10") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) - _, err := client.Vote.Up(ctx, "t3_test") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } + res, err := client.Vote.Up(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } func TestVoteServiceOp_Down(t *testing.T) { @@ -39,25 +38,23 @@ func TestVoteServiceOp_Down(t *testing.T) { defer teardown() mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "t3_test") form.Set("dir", fmt.Sprint(downvote)) form.Set("rank", "10") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) - _, err := client.Vote.Down(ctx, "t3_test") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } + res, err := client.Vote.Down(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) } func TestVoteServiceOp_Remove(t *testing.T) { @@ -65,23 +62,21 @@ func TestVoteServiceOp_Remove(t *testing.T) { defer teardown() mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) + assert.Equal(t, http.MethodPost, r.Method) form := url.Values{} form.Set("id", "t3_test") form.Set("dir", fmt.Sprint(novote)) form.Set("rank", "10") - _ = r.ParseForm() - if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) fmt.Fprint(w, `{}`) }) - _, err := client.Vote.Remove(ctx, "t3_test") - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } + res, err := client.Vote.Remove(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) }