// main_test.go
package main
import (
"reflect"
"testing"
)
func TestMarshalNestedObject(t *testing.T) {
tests := []struct {
name string
want string
wantErr bool
}{
{
name: "expected",
want: `{"data":{"type":"","attributes":{"name":"foo"}}}
`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := MarshalNestedObject()
if (err != nil) != tt.wantErr {
t.Errorf("MarshalNestedObject() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalNestedObject() got = %v, want %v", got, tt.want)
}
})
}
}
func TestMarshalObject(t *testing.T) {
tests := []struct {
name string
want string
wantErr bool
}{
{
name: "expected",
want: `{"data":{"type":"object","attributes":{"color":"blue","enable":true,"nested-object":{"name":"foo"}}}}
`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := MarshalObject()
if (err != nil) != tt.wantErr {
t.Errorf("MarshalObject() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalObject() got = %v, want %v", got, tt.want)
}
})
}
}
In a third party library that uses this library, I have an error because marshaling a nested object does not work.
So I take the opportunity to post an issue here as well.
You can see an example test file which demonstrate this issue.
main.gomain_test.go