...
1 package json
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestEscapeStringBytes(t *testing.T) {
9 cases := map[string]struct {
10 expected string
11 input []byte
12 }{
13 "safeSet only": {
14 expected: `"mountainPotato"`,
15 input: []byte("mountainPotato"),
16 },
17 "parenthesis": {
18 expected: `"foo\""`,
19 input: []byte(`foo"`),
20 },
21 "double escape": {
22 expected: `"hello\\\\world"`,
23 input: []byte(`hello\\world`),
24 },
25 "new line": {
26 expected: `"foo\nbar"`,
27 input: []byte("foo\nbar"),
28 },
29 "carriage return": {
30 expected: `"foo\rbar"`,
31 input: []byte("foo\rbar"),
32 },
33 "tab": {
34 expected: `"foo\tbar"`,
35 input: []byte("foo\tbar"),
36 },
37 }
38 for name, c := range cases {
39 t.Run(name, func(t *testing.T) {
40 var buffer bytes.Buffer
41 escapeStringBytes(&buffer, c.input)
42 expected := c.expected
43 actual := buffer.String()
44 if expected != actual {
45 t.Errorf("\nexpected %v \nactual %v", expected, actual)
46 }
47 })
48 }
49 }
50
View as plain text