Commit 4274d074 authored by Russ Cox's avatar Russ Cox

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
parent ccc45534
......@@ -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)
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment