File: encoder_18_test.go

package info (click to toggle)
golang-github-json-iterator-go 1.1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 916 kB
  • sloc: sh: 19; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download
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
//+build go1.8,!go1.22

package test

import (
	"bytes"
	"encoding/json"
	"testing"
	"unicode/utf8"

	"github.com/json-iterator/go"
	"github.com/stretchr/testify/require"
)

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{}
	encoder2 := jsoniter.NewEncoder(buf2)
	encoder2.SetEscapeHTML(false)
	encoder2.Encode([]int{1})
	should.Equal("[1]\n", buf2.String())
}

func Test_string_encode_with_std_without_html_escape(t *testing.T) {
	api := jsoniter.Config{EscapeHTML: false}.Froze()
	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)
	}
}