Skip to content

Commit

Permalink
test: refactor json.Marshaler and json.Unmarshaler stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
blgm committed May 21, 2020
1 parent ef62926 commit ff76c24
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
20 changes: 12 additions & 8 deletions jsonry_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import (
. "github.com/onsi/gomega"
)

type implementsJSONMarshaler struct{ value bool }
type implementsJSONMarshaler struct {
bytes []byte
err error
}

func (j implementsJSONMarshaler) MarshalJSON() ([]byte, error) {
if j.value {
return nil, errors.New("ouch")
}
return json.Marshal("hello")
func (i implementsJSONMarshaler) MarshalJSON() ([]byte, error) {
return i.bytes, i.err
}

type implementsJSONUnmarshaler struct {
hasBeenSet bool
}

func (j *implementsJSONMarshaler) UnmarshalJSON(input []byte) error {
func (i *implementsJSONUnmarshaler) UnmarshalJSON(input []byte) error {
if bytes.Equal(input, []byte(`"fail"`)) {
return errors.New("ouch")
}
j.value = true
i.hasBeenSet = true
return nil
}

Expand Down
11 changes: 8 additions & 3 deletions marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jsonry_test

import (
"errors"

"code.cloudfoundry.org/jsonry"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -168,9 +170,12 @@ var _ = Describe("Marshal", func() {
})

It("marshals a json.Marshaler", func() {
expectToMarshal(struct{ J implementsJSONMarshaler }{J: implementsJSONMarshaler{value: false}}, `{"J":"hello"}`)
expectToMarshal(struct{ J *implementsJSONMarshaler }{J: &implementsJSONMarshaler{value: false}}, `{"J":"hello"}`)
expectToFail(struct{ J implementsJSONMarshaler }{J: implementsJSONMarshaler{value: true}}, `error from MarshaJSON() call at field "J" (type "jsonry_test.implementsJSONMarshaler"): ouch`)
expectToMarshal(struct{ I implementsJSONMarshaler }{I: implementsJSONMarshaler{bytes: []byte(`"hello"`)}}, `{"I":"hello"}`)
expectToMarshal(struct{ I *implementsJSONMarshaler }{I: &implementsJSONMarshaler{bytes: []byte(`"hello"`)}}, `{"I":"hello"}`)
expectToMarshal(struct{ I *implementsJSONMarshaler }{I: (*implementsJSONMarshaler)(nil)}, `{"I":null}`)

expectToFail(struct{ I implementsJSONMarshaler }{I: implementsJSONMarshaler{err: errors.New("ouch")}}, `error from MarshaJSON() call at field "I" (type "jsonry_test.implementsJSONMarshaler"): ouch`)
expectToFail(struct{ I implementsJSONMarshaler }{I: implementsJSONMarshaler{}}, `error parsing MarshaJSON() output "" at field "I" (type "jsonry_test.implementsJSONMarshaler"): unexpected end of JSON input`)
})

It("marshals from named types and type aliases", func() {
Expand Down
8 changes: 4 additions & 4 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ var _ = Describe("Unmarshal", func() {
expectToFail(&s, `{}`, `maps must only have string keys for "int" at field "S" (type "map[int]string")`)
})

It("unmarshals a json.Marshaler field", func() {
var s struct{ S implementsJSONMarshaler }
It("unmarshals into json.Unmarshaler field", func() {
var s struct{ S implementsJSONUnmarshaler }
unmarshal(&s, `{"S":"ok"}`)
Expect(s.S).To(Equal(implementsJSONMarshaler{value: true}))
Expect(s.S).To(Equal(implementsJSONUnmarshaler{hasBeenSet: true}))

expectToFail(&s, `{"S":"fail"}`, `error from UnmarshalJSON() call at field "S" (type "jsonry_test.implementsJSONMarshaler"): ouch`)
expectToFail(&s, `{"S":"fail"}`, `error from UnmarshalJSON() call at field "S" (type "jsonry_test.implementsJSONUnmarshaler"): ouch`)
})

It("unmarshals into named types and type aliases", func() {
Expand Down

0 comments on commit ff76c24

Please sign in to comment.