]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: add more tests for UTF-8 coercion
authorRuss Cox <rsc@golang.org>
Sat, 13 Jul 2013 00:40:50 +0000 (20:40 -0400)
committerRuss Cox <rsc@golang.org>
Sat, 13 Jul 2013 00:40:50 +0000 (20:40 -0400)
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

src/pkg/encoding/json/decode_test.go

index 65e6d6ec38243867d79a76d20eeb293d18744818..e868f4f1b7ce47cca94e76b02757b2617c748c93 100644 (file)
@@ -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)
+               }
        }
 }