From 324c3981451082cf583bba589d18e5aec240930c Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Tue, 4 Aug 2020 00:42:28 -0400 Subject: [PATCH] Delete unused code, use assert package for tests Signed-off-by: Vartan Benohanian --- reddit.go | 24 ++++++++ reddit_options_test.go | 30 +++------- reddit_test.go | 9 +-- util.go | 126 ----------------------------------------- 4 files changed, 35 insertions(+), 154 deletions(-) delete mode 100644 util.go diff --git a/reddit.go b/reddit.go index 0dce0f8..e7f56c6 100644 --- a/reddit.go +++ b/reddit.go @@ -387,3 +387,27 @@ func addQuery(url string, query url.Values) string { } return url + "?" + query.Encode() } + +// String is a helper routine that allocates a new string value +// to store v and returns a pointer to it. +func String(v string) *string { + p := new(string) + *p = v + return p +} + +// Int is a helper routine that allocates a new int value +// to store v and returns a pointer to it. +func Int(v int) *int { + p := new(int) + *p = v + return p +} + +// Bool is a helper routine that allocates a new bool value +// to store v and returns a pointer to it. +func Bool(v bool) *bool { + p := new(bool) + *p = v + return p +} diff --git a/reddit_options_test.go b/reddit_options_test.go index 9c98f58..f3fc11d 100644 --- a/reddit_options_test.go +++ b/reddit_options_test.go @@ -2,8 +2,9 @@ package reddit import ( "os" - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) func TestFromEnv(t *testing.T) { @@ -20,9 +21,7 @@ func TestFromEnv(t *testing.T) { defer os.Unsetenv("GO_REDDIT_CLIENT_PASSWORD") c, err := NewClient(nil, FromEnv) - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } + assert.NoError(t, err) type values struct { id, secret, username, password string @@ -30,32 +29,19 @@ func TestFromEnv(t *testing.T) { expect := values{"id1", "secret1", "username1", "password1"} actual := values{c.ID, c.Secret, c.Username, c.Password} - - if !reflect.DeepEqual(expect, actual) { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.Equal(t, expect, actual) } func TestWithBaseURL(t *testing.T) { baseURL := "http://localhost:8080" c, err := NewClient(nil, WithBaseURL(baseURL)) - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := baseURL, c.BaseURL.String(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, baseURL, c.BaseURL.String()) } func TestWithTokenURL(t *testing.T) { tokenURL := "http://localhost:8080/api/v1/access_token" c, err := NewClient(nil, WithTokenURL(tokenURL)) - if err != nil { - t.Fatalf("got unexpected error: %v", err) - } - - if expect, actual := tokenURL, c.TokenURL.String(); expect != actual { - t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual)) - } + assert.NoError(t, err) + assert.Equal(t, tokenURL, c.TokenURL.String()) } diff --git a/reddit_test.go b/reddit_test.go index 6857e9c..2310a7a 100644 --- a/reddit_test.go +++ b/reddit_test.go @@ -81,16 +81,13 @@ func testClientServices(t *testing.T, c *Client) { cv := reflect.Indirect(cp) for _, s := range services { - if cv.FieldByName(s).IsNil() { - t.Fatalf("c.%s shouldn't be nil", s) - } + assert.Falsef(t, cv.FieldByName(s).IsNil(), "c.%s should not be nil", s) } } func testClientDefaultUserAgent(t *testing.T, c *Client) { - if expect, actual := fmt.Sprintf("golang:%s:v%s (by /u/)", libraryName, libraryVersion), c.userAgent; expect != actual { - t.Fatalf("got unexpected value\nexpect: %+v\nactual: %+v", Stringify(expect), Stringify(actual)) - } + expectedUserAgent := fmt.Sprintf("golang:%s:v%s (by /u/)", libraryName, libraryVersion) + assert.Equal(t, expectedUserAgent, c.userAgent) } func testClientDefaults(t *testing.T, c *Client) { diff --git a/util.go b/util.go deleted file mode 100644 index 9d0f850..0000000 --- a/util.go +++ /dev/null @@ -1,126 +0,0 @@ -package reddit - -import ( - "bytes" - "fmt" - "io" - "reflect" -) - -var timestampType = reflect.TypeOf(Timestamp{}) - -// String is a helper routine that allocates a new string value -// to store v and returns a pointer to it. -func String(v string) *string { - p := new(string) - *p = v - return p -} - -// Int is a helper routine that allocates a new int32 value -// to store v and returns a pointer to it, but unlike Int32 -// its argument value is an int. -func Int(v int) *int { - p := new(int) - *p = v - return p -} - -// Bool is a helper routine that allocates a new bool value -// to store v and returns a pointer to it. -func Bool(v bool) *bool { - p := new(bool) - *p = v - return p -} - -// StreamToString converts a reader to a string -func StreamToString(stream io.Reader) string { - buf := new(bytes.Buffer) - _, _ = buf.ReadFrom(stream) - return buf.String() -} - -// var timestampType = reflect.TypeOf(Timestamp{}) - -// Stringify attempts to create a string representation of types -func Stringify(message interface{}) string { - var buf bytes.Buffer - v := reflect.ValueOf(message) - stringifyValue(&buf, v) - return buf.String() -} - -// stringifyValue was graciously cargoculted from the goprotubuf library -func stringifyValue(w io.Writer, val reflect.Value) { - if val.Kind() == reflect.Ptr && val.IsNil() { - _, _ = w.Write([]byte("")) - return - } - - v := reflect.Indirect(val) - - switch v.Kind() { - case reflect.String: - fmt.Fprintf(w, `"%s"`, v) - case reflect.Slice: - stringifySlice(w, v) - return - case reflect.Struct: - stringifyStruct(w, v) - default: - if v.CanInterface() { - fmt.Fprint(w, v.Interface()) - } - } -} - -func stringifySlice(w io.Writer, v reflect.Value) { - _, _ = w.Write([]byte{'['}) - for i := 0; i < v.Len(); i++ { - if i > 0 { - _, _ = w.Write([]byte{' '}) - } - - stringifyValue(w, v.Index(i)) - } - - _, _ = w.Write([]byte{']'}) -} - -func stringifyStruct(w io.Writer, v reflect.Value) { - if v.Type().Name() != "" { - _, _ = w.Write([]byte(v.Type().String())) - } - - // special handling of Timestamp values - if v.Type() == timestampType { - fmt.Fprintf(w, "{%s}", v.Interface()) - return - } - - _, _ = w.Write([]byte{'{'}) - - var sep bool - for i := 0; i < v.NumField(); i++ { - fv := v.Field(i) - if fv.Kind() == reflect.Ptr && fv.IsNil() { - continue - } - if fv.Kind() == reflect.Slice && fv.IsNil() { - continue - } - - if sep { - _, _ = w.Write([]byte(", ")) - } else { - sep = true - } - - _, _ = w.Write([]byte(v.Type().Field(i).Name)) - _, _ = w.Write([]byte{':'}) - stringifyValue(w, fv) - } - - _, _ = w.Write([]byte{'}'}) -}