mirror of
https://github.com/golang/go
synced 2024-11-25 13:57:57 -07:00
encoding/json: add more tests for UTF-8 coercion
Suggested by Rob in CL 11211045, but the mail arrived moments after hg submit completed. R=golang-dev, r CC=golang-dev https://golang.org/cl/11138045
This commit is contained in:
parent
ccc4553491
commit
4274d074dc
@ -391,12 +391,23 @@ func TestMarshal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var badUTF8 = []struct {
|
||||
in, out string
|
||||
}{
|
||||
{"hello\xffworld", `"hello\ufffdworld"`},
|
||||
{"", `""`},
|
||||
{"\xff", `"\ufffd"`},
|
||||
{"\xff\xff", `"\ufffd\ufffd"`},
|
||||
{"a\xffb", `"a\ufffdb"`},
|
||||
{"\xe6\x97\xa5\xe6\x9c\xac\xff\xaa\x9e", `"日本\ufffd\ufffd\ufffd"`},
|
||||
}
|
||||
|
||||
func TestMarshalBadUTF8(t *testing.T) {
|
||||
s := "hello\xffworld"
|
||||
const enc = `"hello\ufffdworld"`
|
||||
b, err := Marshal(s)
|
||||
if string(b) != enc || err != nil {
|
||||
t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", s, b, err, enc)
|
||||
for _, tt := range badUTF8 {
|
||||
b, err := Marshal(tt.in)
|
||||
if string(b) != tt.out || err != nil {
|
||||
t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", tt.in, b, err, tt.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user