1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00
json-iterator/api_tests/encoder_18_test.go

47 lines
1.1 KiB
Go
Raw Normal View History

2018-02-13 20:58:29 +08:00
//+build go1.8
2017-07-02 15:11:36 +08:00
2018-02-13 20:58:29 +08:00
package test
2017-07-02 14:27:16 +08:00
import (
"bytes"
2017-07-05 11:40:20 +08:00
"encoding/json"
"testing"
2017-07-02 14:27:16 +08:00
"unicode/utf8"
2017-07-05 11:40:20 +08:00
2018-02-13 20:58:29 +08:00
"github.com/json-iterator/go"
2018-02-24 22:04:41 +08:00
"github.com/stretchr/testify/require"
2017-07-02 14:27:16 +08:00
)
func Test_new_encoder(t *testing.T) {
should := require.New(t)
buf1 := &bytes.Buffer{}
encoder1 := json.NewEncoder(buf1)
encoder1.SetEscapeHTML(false)
encoder1.Encode([]int{1})
should.Equal("[1]\n", buf1.String())
buf2 := &bytes.Buffer{}
2018-02-13 20:58:29 +08:00
encoder2 := jsoniter.NewEncoder(buf2)
2017-07-02 14:27:16 +08:00
encoder2.SetEscapeHTML(false)
encoder2.Encode([]int{1})
should.Equal("[1]\n", buf2.String())
2017-07-02 14:27:16 +08:00
}
func Test_string_encode_with_std_without_html_escape(t *testing.T) {
2018-02-13 20:58:29 +08:00
api := jsoniter.Config{EscapeHTML: false}.Froze()
2017-07-02 14:27:16 +08:00
should := require.New(t)
for i := 0; i < utf8.RuneSelf; i++ {
input := string([]byte{byte(i)})
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(input)
should.Nil(err)
stdOutput := buf.String()
stdOutput = stdOutput[:len(stdOutput)-1]
jsoniterOutputBytes, err := api.Marshal(input)
should.Nil(err)
jsoniterOutput := string(jsoniterOutputBytes)
should.Equal(stdOutput, jsoniterOutput)
}
2017-07-05 11:40:20 +08:00
}