mirror of
https://github.com/json-iterator/go.git
synced 2025-03-23 21:09:11 +02:00
Add tests for malformed string input
This commit is contained in:
parent
d336ee6da6
commit
a447a8f797
@ -6,11 +6,58 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_read_string(t *testing.T) {
|
||||
badInputs := []string{
|
||||
``,
|
||||
`"`,
|
||||
`"\"`,
|
||||
`"\\\"`,
|
||||
"\"\n\"",
|
||||
`navy`,
|
||||
}
|
||||
|
||||
for _, input := range badInputs {
|
||||
testReadString(t, input, "", true, "json.Unmarshal", json.Unmarshal)
|
||||
testReadString(t, input, "", true, "jsoniter.Unmarshal", Unmarshal)
|
||||
testReadString(t, input, "", true, "jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal", ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
||||
|
||||
goodInputs := []struct {
|
||||
input string
|
||||
expectValue string
|
||||
}{
|
||||
{`""`, ""},
|
||||
{`"a"`, "a"},
|
||||
{`null`, ""},
|
||||
{`"Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"`, "Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"},
|
||||
}
|
||||
|
||||
for _, tc := range goodInputs {
|
||||
testReadString(t, tc.input, tc.expectValue, false, "json.Unmarshal", json.Unmarshal)
|
||||
testReadString(t, tc.input, tc.expectValue, false, "jsoniter.Unmarshal", Unmarshal)
|
||||
testReadString(t, tc.input, tc.expectValue, false, "jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal", ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
||||
}
|
||||
|
||||
func testReadString(t *testing.T, input string, expectValue string, expectError bool, marshalerName string, marshaler func([]byte, interface{}) error) {
|
||||
var value string
|
||||
err := marshaler([]byte(input), &value)
|
||||
if expectError != (err != nil) {
|
||||
t.Errorf("%q: %s: expected error %v, got %v", input, marshalerName, expectError, err)
|
||||
return
|
||||
}
|
||||
if value != expectValue {
|
||||
t.Errorf("%q: %s: expected %q, got %q", input, marshalerName, expectValue, value)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_normal_string(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
`"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
|
||||
|
Loading…
x
Reference in New Issue
Block a user