-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoder_test.go
85 lines (76 loc) · 1.76 KB
/
encoder_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
package jzon
import (
"bytes"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
type encFace interface {
Encode(v interface{}) error
SetEscapeHTML(on bool)
// This is incompatible with standard library
// SetIndent(prefix, indent string)
}
var _ encFace = &json.Encoder{}
var _ encFace = &Encoder{}
func TestEncoder_SetEscapeHTML(t *testing.T) {
f := func(t *testing.T, o interface{}, expOn, expOff string) {
must := require.New(t)
buf := bytes.NewBuffer(nil)
f := func(enc encFace) {
// enabled
buf.Reset()
err := enc.Encode(o)
must.NoError(err)
must.Equal(expOn+"\n", buf.String(), "%T", enc)
// disabled
buf.Reset()
enc.SetEscapeHTML(false)
err = enc.Encode(o)
must.NoError(err)
must.Equal(expOff+"\n", buf.String(), "%T", enc)
}
f(json.NewEncoder(buf))
enc := NewEncoder(buf)
defer enc.Release()
f(enc)
}
t.Run("normal", func(t *testing.T) {
s := "<>&"
expOn := `"\u003c\u003e\u0026"`
expOff := `"` + s + `"`
f(t, s, expOn, expOff)
})
t.Run("struct", func(t *testing.T) {
s := struct {
A int `json:"&"`
}{}
expOn := `{"\u0026":0}`
expOff := `{"&":0}`
f(t, s, expOn, expOff)
})
}
// func TestEncoder_SetIndent(t *testing.T) {
// must := require.New(t)
// s := map[string]interface{}{
// "k": "v",
// }
// buf := bytes.NewBuffer(nil)
// f := func(enc encFace) {
// // disabled
// buf.Reset()
// err := enc.Encode(s)
// must.NoError(err)
// must.Equal(`{"k":"v"}`+"\n", buf.String(), "%T", enc)
//
// // disabled
// buf.Reset()
// enc.SetIndent("p", "i")
// err = enc.Encode(s)
// must.NoError(err)
// t.Logf("\n%s", buf.Bytes())
// must.Equal("{\npi\"k\": \"v\"\np}\n", buf.String(), "%T", enc)
// }
// f(json.NewEncoder(buf))
// f(NewEncoder(buf))
// }