1
0
Fork 0
jf2/html_test.go

55 lines
1.0 KiB
Go

package jf2_test
import (
"encoding/json"
"strings"
"testing"
"source.toby3d.me/toby3d/jf2"
)
func TestHTML_UnmarshalJSON(t *testing.T) {
t.Parallel()
v := new(struct {
Content struct {
HTML jf2.HTML `json:"html"`
} `json:"content"`
})
if err := json.Unmarshal([]byte(`{"content":{"html":"<p>Hello World</p>"}}`), v); err != nil {
t.Fatal(err)
}
expect, err := jf2.NewHTML(strings.NewReader(`<p>Hello World</p>`))
if err != nil {
t.Fatal(err)
}
if v.Content.HTML.String() != expect.String() {
t.Errorf("got '%s', want '%s'", v.Content.HTML, *expect)
}
}
func TestHTML_MarshalJSON(t *testing.T) {
t.Parallel()
v, err := jf2.NewHTML(strings.NewReader(`<p>Hello World</p>`))
if err != nil {
t.Fatal(err)
}
body, err := json.Marshal(struct {
Content *jf2.HTML `json:"content"`
}{
Content: v,
})
if err != nil {
t.Fatal(err)
}
const expect string = `{"content":"\u003cp\u003eHello World\u003c/p\u003e"}`
if actual := string(body); actual != expect {
t.Errorf("got '%s', want '%s'", actual, expect)
}
}