Skip to content

Commit

Permalink
test(json): add test case for #63
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicbarnes committed Apr 29, 2021
1 parent 936b01c commit 8b1bdce
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,6 @@ func TestGithubIssue41(t *testing.T) {
"expected: ", expectedString,
)
}

}

func TestGithubIssue44(t *testing.T) {
Expand All @@ -1602,6 +1601,40 @@ func (r *rawJsonString) UnmarshalJSON(b []byte) error {
return nil
}

// See https://github.com/segmentio/encoding/issues/63
// In short, embedding a struct pointer resulted in an incorrect memory address
// as we were still looking to the parent struct to start and only using offsets
// which resulted in the wrong values being extracted.
func TestGithubIssue63(t *testing.T) {
expectedString := `{"my_field":"test","my_other_field":"testing","code":1}`

type MyStruct struct {
MyField string `json:"my_field,omitempty"`
MyOtherField string `json:"my_other_field"`
MyEmptyField string `json:"my_empty_field,omitempty"`
}

type MyStruct2 struct {
*MyStruct
Code int `json:"code"`
}

input := MyStruct2{
MyStruct: &MyStruct{
MyField: "test",
MyOtherField: "testing",
},
Code: 1,
}

if b, err := Marshal(input); err != nil {
t.Error(err)
} else if string(b) != expectedString {
t.Errorf("got: %s", string(b))
t.Errorf("expected: %s", expectedString)
}
}

func TestSetTrustRawMessage(t *testing.T) {
buf := &bytes.Buffer{}
enc := NewEncoder(buf)
Expand Down

0 comments on commit 8b1bdce

Please sign in to comment.