Skip to content

Commit

Permalink
Implemented several code style change to linter suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
yookoala committed Jun 2, 2024
1 parent b3561ac commit bbded4f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions v2/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (c *Case) Expect(exp Expectation) *Case {
// and return the result
func (c Case) Do() (resp Response, err error) {
if c.Request == nil {
return nil, fmt.Errorf("Request is nil")
return nil, fmt.Errorf("case.Request is nil")
}
if c.Context == nil {
c.Context = context.Background()
}
if c.Handler == nil {
return nil, fmt.Errorf("Handler is nil")
return nil, fmt.Errorf("case.Handler is nil")
}

// do the request
Expand Down
4 changes: 2 additions & 2 deletions v2/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestCase_WithContext(t *testing.T) {
}

if want, have := true, expHasContext; want != have {
err = fmt.Errorf("expected %#v, got %#v", want, have)
t.Errorf("expected %#v, got %#v", want, have)
}

// test the response *JSON
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestCase_Describe(t *testing.T) {
}

desc := restit.Describe(str, fn)
desc.Do(nil, nil)
desc.Do(context.Background(), nil)
if want, have := str, desc.Desc(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
} else if want, have := true, fnRun; want != have {
Expand Down
5 changes: 1 addition & 4 deletions v2/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ func (ctx *contextError) Len() int {

// Less implements sort.Interface
func (ctx *contextError) Less(i, j int) bool {
if (*ctx)[i].weight < (*ctx)[j].weight {
return true
}
return false
return (*ctx)[i].weight < (*ctx)[j].weight
}

// Swap implements sort.Interface
Expand Down
4 changes: 1 addition & 3 deletions v2/expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func LengthIs(name string, n int) Expectation {
}

list := proto.Get(name)
if err != nil {
return
} else if want, have := lzjson.TypeArray, list.Type(); want != have {
if want, have := lzjson.TypeArray, list.Type(); want != have {
ctxErr := NewContextError("expected %#v to be type %s, got %s",
name, want, have)
ctxErr.Prepend("ref", "response."+name)
Expand Down
20 changes: 13 additions & 7 deletions v2/expectation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package restit_test

import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
Expand All @@ -11,6 +12,7 @@ import (

"github.com/go-restit/lzjson"
"github.com/go-restit/restit/v2"
"golang.org/x/net/context"
)

func init() {
Expand All @@ -25,10 +27,11 @@ func TestStatusCodeIs(t *testing.T) {
},
}

if err := restit.StatusCodeIs(statusCode).Do(nil, resp); err != nil {
emptyCtx := context.Background()
if err := restit.StatusCodeIs(statusCode).Do(emptyCtx, resp); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := restit.StatusCodeIs(statusCode-1).Do(nil, resp); err == nil {
if err := restit.StatusCodeIs(statusCode-1).Do(emptyCtx, resp); err == nil {
t.Errorf("expected to trigger error but didn't")
} else if want, have := fmt.Sprintf("expected %d, got %d", statusCode-1, statusCode), err.Error(); want != have {
t.Errorf("expected %#v, got %#v", want, have)
Expand All @@ -43,10 +46,11 @@ func TestLengthIs(t *testing.T) {
},
})

if err := restit.LengthIs("hello", 2).Do(nil, resp); err != nil {
emptyCtx := context.Background()
if err := restit.LengthIs("hello", 2).Do(emptyCtx, resp); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := restit.LengthIs("hello", 1).Do(nil, resp); err == nil {
if err := restit.LengthIs("hello", 1).Do(emptyCtx, resp); err == nil {
t.Errorf("should trigger error but didn't")
} else if want, have := `expected "hello" to be length 1, got 2`, err.Error(); want != have {
t.Errorf("\nexpected: %s\ngot: %s", want, have)
Expand All @@ -57,10 +61,12 @@ func TestNthTest(t *testing.T) {

resp := restit.CacheResponse(restit.HTTPResponse{
RawResponse: &http.Response{
Body: ioutil.NopCloser(strings.NewReader(`{"hello": ["hello 1", "hello 2"]}`)),
Body: io.NopCloser(strings.NewReader(`{"hello": ["hello 1", "hello 2"]}`)),
},
})

emptyCtx := context.Background()

test1Run := false
test1 := restit.Nth(0).Of("hello").Is(restit.DescribeJSON("hello 1", func(node lzjson.Node) (err error) {
if want, have := "hello 1", node.String(); want != have {
Expand All @@ -69,7 +75,7 @@ func TestNthTest(t *testing.T) {
test1Run = true
return
}))
if err := test1.Do(nil, resp); err != nil {
if err := test1.Do(emptyCtx, resp); err != nil {
t.Errorf("unexpected test1 error: %s", err)
}
if test1Run == false {
Expand All @@ -84,7 +90,7 @@ func TestNthTest(t *testing.T) {
test2Run = true
return
}))
if err := test2.Do(nil, resp); err == nil {
if err := test2.Do(emptyCtx, resp); err == nil {
t.Errorf("test2 should trigger error but didn't")
} else if want, have := `failed "hello 1" (expected "hello 1", got "hello 2")`, err.Error(); want != have {
t.Errorf("\nexpected: %s\ngot: %s", want, have)
Expand Down

0 comments on commit bbded4f

Please sign in to comment.