encoding/json: add test for Unmarshal of malformed data

Roll back CL making primitive type unmarshal faster,
because it broke the Unmarshal of malformed data.

Add benchmarks for unmarshal of primitive types.

Update #3949.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/7228061
This commit is contained in:
Russ Cox 2013-01-29 13:34:18 -08:00
parent ec1948a44d
commit ad37081b67
3 changed files with 55 additions and 21 deletions

View file

@ -1059,12 +1059,33 @@ func TestUnmarshalTypeError(t *testing.T) {
for _, item := range decodeTypeErrorTests {
err := Unmarshal([]byte(item.src), item.dest)
if _, ok := err.(*UnmarshalTypeError); !ok {
t.Errorf("expected type error for Unmarshal(%q, type %T): got %v instead",
t.Errorf("expected type error for Unmarshal(%q, type %T): got %T",
item.src, item.dest, err)
}
}
}
var unmarshalSyntaxTests = []string{
"tru",
"fals",
"nul",
"123e",
`"hello`,
`[1,2,3`,
`{"key":1`,
`{"key":1,`,
}
func TestUnmarshalSyntax(t *testing.T) {
var x interface{}
for _, src := range unmarshalSyntaxTests {
err := Unmarshal([]byte(src), &x)
if _, ok := err.(*SyntaxError); !ok {
t.Errorf("expected syntax error for Unmarshal(%q): got %T", src, err)
}
}
}
// Test handling of unexported fields that should be ignored.
// Issue 4660
type unexportedFields struct {