-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_test.go
More file actions
116 lines (95 loc) · 2.72 KB
/
Copy pathencode_test.go
File metadata and controls
116 lines (95 loc) · 2.72 KB
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
113
114
115
116
package json
import (
"bytes"
stdjson "encoding/json"
"testing"
)
func TestMarshalCompatibility(t *testing.T) {
in := map[string]any{
"a": "x<y>",
"b": []int{1, 2, 3},
}
want, err := stdjson.Marshal(in)
if err != nil {
t.Fatalf("stdlib marshal failed: %v", err)
}
got, err := Marshal(in)
if err != nil {
t.Fatalf("compat marshal failed: %v", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("marshal mismatch:\ngot: %s\nwant: %s", got, want)
}
}
func TestMarshalIndentCompatibility(t *testing.T) {
in := map[string]any{"a": 1, "b": "x<y>"}
want, err := stdjson.MarshalIndent(in, "", " ")
if err != nil {
t.Fatalf("stdlib marshal indent failed: %v", err)
}
got, err := MarshalIndent(in, "", " ")
if err != nil {
t.Fatalf("compat marshal indent failed: %v", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("marshal indent mismatch:\ngot: %s\nwant: %s", got, want)
}
}
func TestEncoderCompatibility(t *testing.T) {
in := map[string]any{"a": "x<y>"}
var want bytes.Buffer
stdEnc := stdjson.NewEncoder(&want)
stdEnc.SetIndent("", " ")
stdEnc.SetEscapeHTML(false)
if err := stdEnc.Encode(in); err != nil {
t.Fatalf("stdlib encode failed: %v", err)
}
var got bytes.Buffer
enc := NewEncoder(&got)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
if err := enc.Encode(in); err != nil {
t.Fatalf("compat encode failed: %v", err)
}
if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Fatalf("encoder mismatch:\ngot: %s\nwant: %s", got.Bytes(), want.Bytes())
}
}
func TestCompactCompatibility(t *testing.T) {
src := []byte("{\n \"a\": 1,\n \"b\": [2, 3]\n}\n")
var want bytes.Buffer
if err := stdjson.Compact(&want, src); err != nil {
t.Fatalf("stdlib compact failed: %v", err)
}
var got bytes.Buffer
if err := Compact(&got, src); err != nil {
t.Fatalf("compat compact failed: %v", err)
}
if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Fatalf("compact mismatch:\ngot: %q\nwant: %q", got.String(), want.String())
}
}
func TestIndentCompatibility(t *testing.T) {
src := []byte("{\"a\":1,\"b\":[2,3]}")
var want bytes.Buffer
if err := stdjson.Indent(&want, src, ">", " "); err != nil {
t.Fatalf("stdlib indent failed: %v", err)
}
var got bytes.Buffer
if err := Indent(&got, src, ">", " "); err != nil {
t.Fatalf("compat indent failed: %v", err)
}
if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Fatalf("indent mismatch:\ngot: %q\nwant: %q", got.String(), want.String())
}
}
func TestHTMLEscapeCompatibility(t *testing.T) {
src := []byte(`{"x":"<tag>&"}`)
var want bytes.Buffer
stdjson.HTMLEscape(&want, src)
var got bytes.Buffer
HTMLEscape(&got, src)
if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Fatalf("html escape mismatch:\ngot: %q\nwant: %q", got.String(), want.String())
}
}