Add compatible test

This commit is contained in:
Masaaki Goshima 2021-01-19 14:01:43 +09:00
parent e6e5fe665c
commit 30ab976eb4
6 changed files with 36 additions and 0 deletions

View File

@ -1,5 +1,10 @@
package json_test
import (
"bytes"
stdjson "encoding/json"
)
func intptr(v int) *int { return &v }
func int8ptr(v int8) *int8 { return &v }
func int16ptr(v int16) *int16 { return &v }
@ -13,3 +18,14 @@ func uint64ptr(v uint64) *uint64 { return &v }
func float32ptr(v float32) *float32 { return &v }
func float64ptr(v float64) *float64 { return &v }
func stringptr(v string) *string { return &v }
func encodeByEncodingJSON(data interface{}, indent, escape bool) string {
var buf bytes.Buffer
enc := stdjson.NewEncoder(&buf)
enc.SetEscapeHTML(escape)
if indent {
enc.SetIndent("", " ")
}
enc.Encode(data)
return buf.String()
}

View File

@ -2922,6 +2922,10 @@ null
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
}
if indent {
got := "\n" + buf.String()
if got != test.indentExpected {

View File

@ -2922,6 +2922,10 @@ null
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
}
if indent {
got := "\n" + buf.String()
if got != test.indentExpected {

View File

@ -2922,6 +2922,10 @@ null
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
}
if indent {
got := "\n" + buf.String()
if got != test.indentExpected {

View File

@ -2922,6 +2922,10 @@ null
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
}
if indent {
got := "\n" + buf.String()
if got != test.indentExpected {

View File

@ -2922,6 +2922,10 @@ null
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
}
if indent {
got := "\n" + buf.String()
if got != test.indentExpected {