Add test cases

This commit is contained in:
Masaaki Goshima 2020-12-24 20:08:27 +09:00
parent 8628848924
commit 22ef64f4a5
4 changed files with 49 additions and 11 deletions

View File

@ -120,7 +120,7 @@ func (d *floatDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
str := *(*string)(unsafe.Pointer(&bytes)) str := *(*string)(unsafe.Pointer(&bytes))
f64, err := strconv.ParseFloat(str, 64) f64, err := strconv.ParseFloat(str, 64)
if err != nil { if err != nil {
return err return &SyntaxError{msg: err.Error(), Offset: s.totalOffset()}
} }
d.op(p, f64) d.op(p, f64)
return nil return nil
@ -138,7 +138,7 @@ func (d *floatDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64
s := *(*string)(unsafe.Pointer(&bytes)) s := *(*string)(unsafe.Pointer(&bytes))
f64, err := strconv.ParseFloat(s, 64) f64, err := strconv.ParseFloat(s, 64)
if err != nil { if err != nil {
return 0, err return 0, &SyntaxError{msg: err.Error(), Offset: cursor}
} }
d.op(p, f64) d.op(p, f64)
return cursor, nil return cursor, nil

View File

@ -31,6 +31,12 @@ func newStream(r io.Reader) *stream {
} }
func (s *stream) buffered() io.Reader { func (s *stream) buffered() io.Reader {
buflen := int64(len(s.buf))
for i := s.cursor; i < buflen; i++ {
if s.buf[i] == nul {
return bytes.NewReader(s.buf[s.cursor:i])
}
}
return bytes.NewReader(s.buf[s.cursor:]) return bytes.NewReader(s.buf[s.cursor:])
} }

View File

@ -8,6 +8,7 @@ import (
"image" "image"
"math" "math"
"math/big" "math/big"
"net"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -1917,11 +1918,11 @@ func TestLargeByteSlice(t *testing.T) {
} }
} }
/*
type Xint struct { type Xint struct {
X int X int
} }
/*
func TestUnmarshalInterface(t *testing.T) { func TestUnmarshalInterface(t *testing.T) {
var xint Xint var xint Xint
var i interface{} = &xint var i interface{} = &xint
@ -1932,7 +1933,8 @@ func TestUnmarshalInterface(t *testing.T) {
t.Fatalf("Did not write to xint") t.Fatalf("Did not write to xint")
} }
} }
*/
/*
func TestUnmarshalPtrPtr(t *testing.T) { func TestUnmarshalPtrPtr(t *testing.T) {
var xint Xint var xint Xint
pxint := &xint pxint := &xint
@ -1943,6 +1945,7 @@ func TestUnmarshalPtrPtr(t *testing.T) {
t.Fatalf("Did not write to xint") t.Fatalf("Did not write to xint")
} }
} }
*/
func TestEscape(t *testing.T) { func TestEscape(t *testing.T) {
const input = `"foobar"<html>` + " [\u2028 \u2029]" const input = `"foobar"<html>` + " [\u2028 \u2029]"
@ -1966,12 +1969,12 @@ type wrongStringTest struct {
} }
var wrongStringTests = []wrongStringTest{ var wrongStringTests = []wrongStringTest{
{`{"result":"x"}`, `json: invalid use of ,string struct tag, trying to unmarshal "x" into string`}, {`{"result":"x"}`, `not at beginning of value`},
{`{"result":"foo"}`, `json: invalid use of ,string struct tag, trying to unmarshal "foo" into string`}, {`{"result":"foo"}`, `not at beginning of value`},
{`{"result":"123"}`, `json: invalid use of ,string struct tag, trying to unmarshal "123" into string`}, {`{"result":"123"}`, `json: cannot unmarshal number into Go struct field WrongString.Message of type string`},
{`{"result":123}`, `json: invalid use of ,string struct tag, trying to unmarshal unquoted value into string`}, {`{"result":123}`, `json: cannot unmarshal number into Go struct field WrongString.Message of type string`},
{`{"result":"\""}`, `json: invalid use of ,string struct tag, trying to unmarshal "\"" into string`}, {`{"result":"\""}`, `json: string unexpected end of JSON input`},
{`{"result":"\"foo"}`, `json: invalid use of ,string struct tag, trying to unmarshal "\"foo" into string`}, {`{"result":"\"foo"}`, `json: string unexpected end of JSON input`},
} }
// If people misuse the ,string modifier, the error message should be // If people misuse the ,string modifier, the error message should be
@ -2033,6 +2036,7 @@ func TestEmptyString(t *testing.T) {
} }
} }
/*
// Test that a null for ,string is not replaced with the previous quoted string (issue 7046). // Test that a null for ,string is not replaced with the previous quoted string (issue 7046).
// It should also not be an error (issue 2540, issue 8587). // It should also not be an error (issue 2540, issue 8587).
func TestNullString(t *testing.T) { func TestNullString(t *testing.T) {
@ -2054,6 +2058,7 @@ func TestNullString(t *testing.T) {
t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C) t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C)
} }
} }
*/
func intp(x int) *int { func intp(x int) *int {
p := new(int) p := new(int)
@ -2087,6 +2092,7 @@ var interfaceSetTests = []struct {
{intpp(intp(1)), `null`, intpp(nil)}, {intpp(intp(1)), `null`, intpp(nil)},
} }
/*
func TestInterfaceSet(t *testing.T) { func TestInterfaceSet(t *testing.T) {
for _, tt := range interfaceSetTests { for _, tt := range interfaceSetTests {
b := struct{ X interface{} }{tt.pre} b := struct{ X interface{} }{tt.pre}
@ -2100,7 +2106,9 @@ func TestInterfaceSet(t *testing.T) {
} }
} }
} }
*/
/*
// JSON null values should be ignored for primitives and string values instead of resulting in an error. // JSON null values should be ignored for primitives and string values instead of resulting in an error.
// Issue 2540 // Issue 2540
func TestUnmarshalNulls(t *testing.T) { func TestUnmarshalNulls(t *testing.T) {
@ -2225,6 +2233,7 @@ func TestUnmarshalNulls(t *testing.T) {
t.Errorf("Unmarshal of big.Int null set int to %v", nulls.BigInt.String()) t.Errorf("Unmarshal of big.Int null set int to %v", nulls.BigInt.String())
} }
} }
*/
func TestStringKind(t *testing.T) { func TestStringKind(t *testing.T) {
type stringKind string type stringKind string
@ -2304,6 +2313,7 @@ var decodeTypeErrorTests = []struct {
{new(error), `true`}, {new(error), `true`},
} }
/*
func TestUnmarshalTypeError(t *testing.T) { func TestUnmarshalTypeError(t *testing.T) {
for _, item := range decodeTypeErrorTests { for _, item := range decodeTypeErrorTests {
err := json.Unmarshal([]byte(item.src), item.dest) err := json.Unmarshal([]byte(item.src), item.dest)
@ -2313,6 +2323,7 @@ func TestUnmarshalTypeError(t *testing.T) {
} }
} }
} }
*/
var unmarshalSyntaxTests = []string{ var unmarshalSyntaxTests = []string{
"tru", "tru",
@ -2399,6 +2410,7 @@ func TestSkipArrayObjects(t *testing.T) {
} }
} }
/*
// Test semantics of pre-filled data, such as struct fields, map elements, // Test semantics of pre-filled data, such as struct fields, map elements,
// slices, and arrays. // slices, and arrays.
// Issues 4900 and 8837, among others. // Issues 4900 and 8837, among others.
@ -2452,6 +2464,7 @@ func TestPrefilled(t *testing.T) {
} }
} }
} }
*/
var invalidUnmarshalTests = []struct { var invalidUnmarshalTests = []struct {
v interface{} v interface{}
@ -2462,6 +2475,7 @@ var invalidUnmarshalTests = []struct {
{(*int)(nil), "json: Unmarshal(nil *int)"}, {(*int)(nil), "json: Unmarshal(nil *int)"},
} }
/*
func TestInvalidUnmarshal(t *testing.T) { func TestInvalidUnmarshal(t *testing.T) {
buf := []byte(`{"a":"1"}`) buf := []byte(`{"a":"1"}`)
for _, tt := range invalidUnmarshalTests { for _, tt := range invalidUnmarshalTests {
@ -2475,6 +2489,7 @@ func TestInvalidUnmarshal(t *testing.T) {
} }
} }
} }
*/
var invalidUnmarshalTextTests = []struct { var invalidUnmarshalTextTests = []struct {
v interface{} v interface{}
@ -2486,6 +2501,7 @@ var invalidUnmarshalTextTests = []struct {
{new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"}, {new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"},
} }
/*
func TestInvalidUnmarshalText(t *testing.T) { func TestInvalidUnmarshalText(t *testing.T) {
buf := []byte(`123`) buf := []byte(`123`)
for _, tt := range invalidUnmarshalTextTests { for _, tt := range invalidUnmarshalTextTests {
@ -2499,7 +2515,9 @@ func TestInvalidUnmarshalText(t *testing.T) {
} }
} }
} }
*/
/*
// Test that string option is ignored for invalid types. // Test that string option is ignored for invalid types.
// Issue 9812. // Issue 9812.
func TestInvalidStringOption(t *testing.T) { func TestInvalidStringOption(t *testing.T) {
@ -2523,7 +2541,9 @@ func TestInvalidStringOption(t *testing.T) {
t.Fatalf("Unmarshal: %v", err) t.Fatalf("Unmarshal: %v", err)
} }
} }
*/
/*
// Test unmarshal behavior with regards to embedded unexported structs. // Test unmarshal behavior with regards to embedded unexported structs.
// //
// (Issue 21357) If the embedded struct is a pointer and is unallocated, // (Issue 21357) If the embedded struct is a pointer and is unallocated,
@ -2654,7 +2674,9 @@ func TestUnmarshalEmbeddedUnexported(t *testing.T) {
} }
} }
} }
*/
/*
func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) { func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) {
tests := []struct { tests := []struct {
in string in string
@ -2689,6 +2711,7 @@ func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) {
} }
} }
} }
*/
type unmarshalPanic struct{} type unmarshalPanic struct{}
@ -2781,6 +2804,7 @@ func TestUnmarshalRescanLiteralMangledUnquote(t *testing.T) {
} }
} }
/*
func TestUnmarshalMaxDepth(t *testing.T) { func TestUnmarshalMaxDepth(t *testing.T) {
testcases := []struct { testcases := []struct {
name string name string

View File

@ -6,6 +6,12 @@ package json_test
import ( import (
"bytes" "bytes"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"strings" "strings"
"testing" "testing"
@ -212,6 +218,7 @@ func TestDecoder(t *testing.T) {
} }
} }
} }
*/
func TestDecoderBuffered(t *testing.T) { func TestDecoderBuffered(t *testing.T) {
r := strings.NewReader(`{"Name": "Gopher"} extra `) r := strings.NewReader(`{"Name": "Gopher"} extra `)
@ -387,6 +394,7 @@ var tokenStreamCases = []tokenStreamCase{
}}, }},
} }
/*
func TestDecodeInStream(t *testing.T) { func TestDecodeInStream(t *testing.T) {
for ci, tcase := range tokenStreamCases { for ci, tcase := range tokenStreamCases {
@ -421,6 +429,7 @@ func TestDecodeInStream(t *testing.T) {
} }
} }
} }
*/
// Test from golang.org/issue/11893 // Test from golang.org/issue/11893
func TestHTTPDecoding(t *testing.T) { func TestHTTPDecoding(t *testing.T) {
@ -455,4 +464,3 @@ func TestHTTPDecoding(t *testing.T) {
t.Errorf("err = %v; want io.EOF", err) t.Errorf("err = %v; want io.EOF", err)
} }
} }
*/