-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring_test.go
112 lines (100 loc) · 2.81 KB
/
string_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package jx
import (
hexEnc "encoding/hex"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_read_string(t *testing.T) {
badInputs := []string{
``,
`null`,
`"`,
`"\"`,
`"\\\"`,
"\"\n\"",
`"\U0001f64f"`,
`"\uD83D\u00"`,
}
for i := 0; i < 32; i++ {
// control characters are invalid
badInputs = append(badInputs, string([]byte{'"', byte(i), '"'}))
}
for _, input := range badInputs {
i := DecodeStr(input)
_, err := i.Str()
assert.Error(t, err, "input: %q", input)
}
goodInputs := []struct {
input string
expectValue string
}{
{`""`, ""},
{`"a"`, "a"},
{`"Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"`, "Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"},
{`"\uD83D"`, string([]byte{239, 191, 189})},
{`"\uD83D\\"`, string([]byte{239, 191, 189, '\\'})},
{`"\uD83D\ub000"`, string([]byte{239, 191, 189, 235, 128, 128})},
{`"\uD83D\ude04"`, "😄"},
{`"\uDEADBEEF"`, string([]byte{239, 191, 189, 66, 69, 69, 70})},
{`"hel\"lo"`, `hel"lo`},
{`"hel\\\/lo"`, `hel\/lo`},
{`"hel\\blo"`, `hel\blo`},
{`"hel\\\blo"`, "hel\\\blo"},
{`"hel\\nlo"`, `hel\nlo`},
{`"hel\\\nlo"`, "hel\\\nlo"},
{`"hel\\tlo"`, `hel\tlo`},
{`"hel\\flo"`, `hel\flo`},
{`"hel\\\flo"`, "hel\\\flo"},
{`"hel\\\rlo"`, "hel\\\rlo"},
{`"hel\\\tlo"`, "hel\\\tlo"},
{`"\u4e2d\u6587"`, "中文"},
{`"\ud83d\udc4a"`, "\xf0\x9f\x91\x8a"},
}
for _, tc := range goodInputs {
testReadString(t, tc.input, tc.expectValue, false, "json.Unmarshal", json.Unmarshal)
i := DecodeStr(tc.input)
s, err := i.Str()
assert.NoError(t, err)
assert.Equal(t, tc.expectValue, s)
}
}
func testReadString(t *testing.T, input string, expectValue string, expectError bool, marshalerName string, marshaler func([]byte, interface{}) error) {
t.Helper()
var value string
err := marshaler([]byte(input), &value)
if expectError != (err != nil) {
t.Errorf("%q: %s: expected error %v, got %v", input, marshalerName, expectError, err)
return
}
if value != expectValue {
t.Errorf("%q: %s: expected %q, got %q", input, marshalerName, expectValue, value)
return
}
}
func TestEncoder_Str(t *testing.T) {
for _, tt := range []struct {
Name string
Input string
}{
{Name: "\\x00", Input: "\x00"},
{Name: "\\x00TrailingSpace", Input: "\x00 "},
{Name: "Hello", Input: `"hello, world!"`},
} {
t.Run(tt.Name, func(t *testing.T) {
s := GetEncoder()
t.Logf("%v", []rune(tt.Input))
s.Str(tt.Input)
t.Logf("%v", []rune(s.String()))
// Check `encoding/json` compatibility.
var gotStd string
require.NoError(t, json.Unmarshal(s.Bytes(), &gotStd))
require.Equal(t, tt.Input, gotStd)
i := DecodeBytes(s.Bytes())
got, err := i.Str()
require.NoError(t, err)
require.Equal(t, tt.Input, got, "%s\n%s", s, hexEnc.Dump(s.Bytes()))
})
}
}