Skip to content

Commit

Permalink
Fix golint
Browse files Browse the repository at this point in the history
Add full example with mapstructure
  • Loading branch information
heralight committed Oct 16, 2017
1 parent 1992849 commit e6727ae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
16 changes: 15 additions & 1 deletion _examples/assert_json/json_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package assert_json

import (
"fmt"
"strings"
"testing"

"github.com/mitchellh/mapstructure"
baloo "gopkg.in/h2non/baloo.v2"
)

type UserAgent struct {
Value string `mapstructure:"user-agent"`
}

// test stores the HTTP testing client preconfigured
var test = baloo.New("http://httpbin.org")

Expand All @@ -27,7 +34,14 @@ func TestBalooJSONCustomAssertion(t *testing.T) {
Type("json").
JSON(`{"user-agent":"baloo/` + baloo.Version + `"}`).
VerifyJSON(func(data map[string]interface{}) error {
// check your json response here
var result UserAgent
err := mapstructure.Decode(data, &result)
if err != nil {
return err
}
if !strings.Contains(result.Value, "baloo") {
return fmt.Errorf("bad user-agent: %s, %s", result.Value, data)
}
return nil
}).
Done()
Expand Down
6 changes: 3 additions & 3 deletions assert/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"reflect"
)

// convert types take an int and return a string value.
type FnJsonVerify func(map[string]interface{}) error
// FnJSONVerify callable function that take a JSON map, test this data and can return an error.
type FnJSONVerify func(map[string]interface{}) error

func unmarshal(buf []byte) (map[string]interface{}, error) {
data := make(map[string]interface{})
Expand Down Expand Up @@ -116,7 +116,7 @@ func JSON(data interface{}) Func {

// VerifyJSON extract JSON in body and call fn with result
// write your own test on data
func VerifyJSON(fn FnJsonVerify) Func {
func VerifyJSON(fn FnJSONVerify) Func {
return func(res *http.Response, req *http.Request) error {
// Read and unmarshal response body as JSON
body, err := unmarshalBody(res)
Expand Down
2 changes: 1 addition & 1 deletion expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (e *Expect) JSON(data interface{}) *Expect {

// VerifyJSON asserts the response body with the given function
// write your own test on data
func (e *Expect) VerifyJSON(fn assert.FnJsonVerify) *Expect {
func (e *Expect) VerifyJSON(fn assert.FnJSONVerify) *Expect {
e.AssertFunc(assert.VerifyJSON(fn))
return e
}
Expand Down
8 changes: 4 additions & 4 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import (
)

var scanStackForFile = true
var logf_func = logf
var logfFunc = logf

const noStackOffset = 0

// Logf adds the actual file:line information to the log message
func Logf(t *testing.T, format string, args ...interface{}) {
logf_func(t, noStackOffset, "\n"+format, args...)
logfFunc(t, noStackOffset, "\n"+format, args...)
}

func logfatal(t *testing.T, format string, args ...interface{}) {
logf_func(t, noStackOffset, format, args...)
logfFunc(t, noStackOffset, format, args...)
t.FailNow()
}

Expand All @@ -34,7 +34,7 @@ func logerror(t *testing.T, args ...interface{}) {
}

func logerrorf(t *testing.T, format string, args ...interface{}) {
logf_func(t, noStackOffset, format, args...)
logfFunc(t, noStackOffset, format, args...)
t.Fail()
}

Expand Down

0 comments on commit e6727ae

Please sign in to comment.