Debian Codegraph @

value_tests/string_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package test
2
3 import (
4 "encoding/json"
5 "github.com/json-iterator/go"
6 "testing"
7 "unicode/utf8"
8 )
9
10 func init() {
11 marshalCases = append(marshalCases,
12 `>`,
13 `"数字山谷"`,
14 "he\u2029\u2028he",
15 )
16 for i := 0; i < utf8.RuneSelf; i++ {
•17 if i == '\b' || i == '\f' {
18 // https://github.com/golang/go/issues/64346
19 continue
20 }
21 marshalCases = append(marshalCases, string([]byte{byte(i)}))
22 }
23 }
24
25 func Test_read_string(t *testing.T) {
26 badInputs := []string{
27 ``,
28 `"`,
29 `"\"`,
30 `"\\\"`,
31 "\"\n\"",
32 `"\U0001f64f"`,
33 `"\uD83D\u00"`,
34 }
35 for i := 0; i < 32; i++ {
36 // control characters are invalid
37 badInputs = append(badInputs, string([]byte{'"', byte(i), '"'}))
38 }
39
40 for _, input := range badInputs {
41 testReadString(t, input, "", true, "json.Unmarshal", json.Unmarshal)
42 testReadString(t, input, "", true, "jsoniter.Unmarshal", jsoniter.Unmarshal)
43 testReadString(t, input, "", true, "jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
44 }
45
46 goodInputs := []struct {
47 input string
48 expectValue string
49 }{
50 {`""`, ""},
51 {`"a"`, "a"},
52 {`null`, ""},
53 {`"Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"`, "Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"},
54 {`"\uD83D"`, string([]byte{239, 191, 189})},
55 {`"\uD83D\\"`, string([]byte{239, 191, 189, '\\'})},
56 {`"\uD83D\ub000"`, string([]byte{239, 191, 189, 235, 128, 128})},
57 {`"\uD83D\ude04"`, "😄"},
58 {`"\uDEADBEEF"`, string([]byte{239, 191, 189, 66, 69, 69, 70})},
59 {`"hel\"lo"`, `hel"lo`},
60 {`"hel\\\/lo"`, `hel\/lo`},
61 {`"hel\\blo"`, `hel\blo`},
62 {`"hel\\\blo"`, "hel\\\blo"},
63 {`"hel\\nlo"`, `hel\nlo`},
64 {`"hel\\\nlo"`, "hel\\\nlo"},
65 {`"hel\\tlo"`, `hel\tlo`},
66 {`"hel\\flo"`, `hel\flo`},
67 {`"hel\\\flo"`, "hel\\\flo"},
68 {`"hel\\\rlo"`, "hel\\\rlo"},
69 {`"hel\\\tlo"`, "hel\\\tlo"},
70 {`"\u4e2d\u6587"`, "中文"},
71 {`"\ud83d\udc4a"`, "\xf0\x9f\x91\x8a"},
72 }
73
74 for _, tc := range goodInputs {
75 testReadString(t, tc.input, tc.expectValue, false, "json.Unmarshal", json.Unmarshal)
76 testReadString(t, tc.input, tc.expectValue, false, "jsoniter.Unmarshal", jsoniter.Unmarshal)
77 testReadString(t, tc.input, tc.expectValue, false, "jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
78 }
79 }
80
81 func testReadString(t *testing.T, input string, expectValue string, expectError bool, marshalerName string, marshaler func([]byte, interface{}) error) {
82 var value string
83 err := marshaler([]byte(input), &value)
84 if expectError != (err != nil) {
85 t.Errorf("%q: %s: expected error %v, got %v", input, marshalerName, expectError, err)
86 return
87 }
88 if value != expectValue {
89 t.Errorf("%q: %s: expected %q, got %q", input, marshalerName, expectValue, value)
90 return
91 }
92 }