52 lines
937 B
Go
52 lines
937 B
Go
package jf2_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"source.toby3d.me/toby3d/jf2"
|
|
)
|
|
|
|
func TestType_UnmarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
v := new(struct {
|
|
Type jf2.Type `json:"type"`
|
|
Author struct {
|
|
Type jf2.Type `json:"type"`
|
|
} `json:"author"`
|
|
})
|
|
if err := json.Unmarshal([]byte(`{"type":"entry","author":{"type":"card"}}`), v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for actual, target := range map[jf2.Type]jf2.Type{
|
|
v.Type: jf2.TypeEntry,
|
|
v.Author.Type: jf2.TypeCard,
|
|
} {
|
|
if actual == target {
|
|
continue
|
|
}
|
|
|
|
t.Errorf("got '%s', want '%s'", actual, target)
|
|
}
|
|
}
|
|
|
|
func TestType_MarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
body, err := json.Marshal(struct {
|
|
Type jf2.Type `json:"type"`
|
|
}{
|
|
Type: jf2.TypeCard,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const expect string = `{"type":"card"}`
|
|
if actual := string(body); actual != expect {
|
|
t.Errorf("got '%s', want '%s'", actual, expect)
|
|
}
|
|
}
|