Skip to content

Commit

Permalink
Fix JSON tests #256
Browse files Browse the repository at this point in the history
- some go vet lints (redundant type)
  • Loading branch information
liamsi committed Mar 6, 2019
1 parent 2f188fe commit 9dc895f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module github.com/tendermint/go-amino
go 1.12

require (
github.com/davecgh/go-spew v1.1.0
github.com/golang/protobuf v1.1.0
github.com/davecgh/go-spew v1.1.1
github.com/golang/protobuf v1.3.0
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.3.0
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
)
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.3.0 h1:kbxbvI4Un1LUWKxufD+BiE6AEExYYgkQLQmLFqA1LFk=
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf h1:+RRA9JqSOZFfKrOeqr2z77+8R2RKyh8PG66dcu1V0ck=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
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/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
53 changes: 29 additions & 24 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,32 +173,33 @@ type innerFP struct {

func TestUnmarshalMap(t *testing.T) {
binBytes := []byte(`dontcare`)
jsonBytes := []byte(`{"2": 2}`)
obj := new(map[string]int)
cdc := amino.NewCodec()
// Binary doesn't support decoding to a map...
// TODO: move out binary tests from json_test.go ...
assert.Panics(t, func() {
err := cdc.UnmarshalBinaryLengthPrefixed(binBytes, &obj)
err := cdc.UnmarshalBinaryBare(binBytes, &obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})
assert.Panics(t, func() {
err := cdc.UnmarshalBinaryLengthPrefixed(binBytes, obj)
err := cdc.UnmarshalBinaryBare(binBytes, obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})
// ... nor encoding it.
assert.Panics(t, func() {
bz, err := cdc.MarshalBinaryLengthPrefixed(obj)
bz, err := cdc.MarshalBinaryBare(obj)
assert.Fail(t, "should have paniced but got bz: %X err: %v", bz, err)
})
// JSON doesn't support decoding to a map...
assert.Panics(t, func() {
err := cdc.UnmarshalJSON(jsonBytes, &obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})
assert.Panics(t, func() {
err := cdc.UnmarshalJSON(jsonBytes, obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})

invalidJSONMapBytes := []byte(`{"some_key": 2}`)
// we expect quoted values for javascript / JSON numbers:
err := cdc.UnmarshalJSON(invalidJSONMapBytes, &obj)
assert.Error(t, err)

validJSONMapBytes := []byte(`{"some_key": "2"}`)
err = cdc.UnmarshalJSON(validJSONMapBytes, obj)
assert.NoError(t, err)

// ... nor encoding it.
assert.Panics(t, func() {
bz, err := cdc.MarshalJSON(obj)
Expand All @@ -212,15 +213,18 @@ func TestUnmarshalFunc(t *testing.T) {
obj := func() {}
cdc := amino.NewCodec()
// Binary doesn't support decoding to a func...

err := cdc.UnmarshalBinaryLengthPrefixed(binBytes, &obj)
// on length prefixed we return an error:
assert.Error(t, err)

assert.Panics(t, func() {
err := cdc.UnmarshalBinaryLengthPrefixed(binBytes, &obj)
t.Log(err)
// TODO: this doesn't panic; update testify and other deps and
// fix test(s)
err := cdc.UnmarshalBinaryBare(binBytes, &obj)
// panics with "unknown field type Func"
assert.Fail(t, "should have paniced but got err: %v", err)
})
assert.Panics(t, func() {
err := cdc.UnmarshalBinaryLengthPrefixed(binBytes, obj)
err := cdc.UnmarshalBinaryBare(binBytes, obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})
// ... nor encoding it.
Expand All @@ -233,10 +237,11 @@ func TestUnmarshalFunc(t *testing.T) {
err := cdc.UnmarshalJSON(jsonBytes, &obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})
assert.Panics(t, func() {
err := cdc.UnmarshalJSON(jsonBytes, obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})

err = cdc.UnmarshalJSON(jsonBytes, obj)
// UnmarshalJSON expects a pointer
assert.Error(t, err)

// ... nor encoding it.
assert.Panics(t, func() {
bz, err := cdc.MarshalJSON(obj)
Expand Down Expand Up @@ -563,7 +568,7 @@ func TestMarshalJSONMap(t *testing.T) {
Map2nil: (map[string]SimpleStruct)(nil),
Map2empty: map[string]SimpleStruct{},

Map3: map[string]*SimpleStruct{"foo": &SimpleStruct{Foo: 1, Bar: []byte("bar")}},
Map3: map[string]*SimpleStruct{"foo": {Foo: 1, Bar: []byte("bar")}},
Map3nil: (map[string]*SimpleStruct)(nil),
Map3empty: map[string]*SimpleStruct{},

Expand All @@ -584,7 +589,7 @@ func TestMarshalJSONMap(t *testing.T) {
Map2nil: map[string]SimpleStruct{},
Map2empty: map[string]SimpleStruct{},

Map3: map[string]*SimpleStruct{"foo": &SimpleStruct{Foo: 1, Bar: []byte("bar")}},
Map3: map[string]*SimpleStruct{"foo": {Foo: 1, Bar: []byte("bar")}},
Map3nil: map[string]*SimpleStruct{},
Map3empty: map[string]*SimpleStruct{},

Expand Down

0 comments on commit 9dc895f

Please sign in to comment.