...

Source file src/github.com/json-iterator/go/api_tests/encoder_18_test.go

Documentation: github.com/json-iterator/go/api_tests

     1  //+build go1.8
     2  
     3  package test
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/json"
     8  	"testing"
     9  	"unicode/utf8"
    10  
    11  	"github.com/json-iterator/go"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func Test_new_encoder(t *testing.T) {
    16  	should := require.New(t)
    17  	buf1 := &bytes.Buffer{}
    18  	encoder1 := json.NewEncoder(buf1)
    19  	encoder1.SetEscapeHTML(false)
    20  	encoder1.Encode([]int{1})
    21  	should.Equal("[1]\n", buf1.String())
    22  	buf2 := &bytes.Buffer{}
    23  	encoder2 := jsoniter.NewEncoder(buf2)
    24  	encoder2.SetEscapeHTML(false)
    25  	encoder2.Encode([]int{1})
    26  	should.Equal("[1]\n", buf2.String())
    27  }
    28  
    29  func Test_string_encode_with_std_without_html_escape(t *testing.T) {
    30  	api := jsoniter.Config{EscapeHTML: false}.Froze()
    31  	should := require.New(t)
    32  	for i := 0; i < utf8.RuneSelf; i++ {
    33  		input := string([]byte{byte(i)})
    34  		buf := &bytes.Buffer{}
    35  		encoder := json.NewEncoder(buf)
    36  		encoder.SetEscapeHTML(false)
    37  		err := encoder.Encode(input)
    38  		should.Nil(err)
    39  		stdOutput := buf.String()
    40  		stdOutput = stdOutput[:len(stdOutput)-1]
    41  		jsoniterOutputBytes, err := api.Marshal(input)
    42  		should.Nil(err)
    43  		jsoniterOutput := string(jsoniterOutputBytes)
    44  		should.Equal(stdOutput, jsoniterOutput)
    45  	}
    46  }
    47  

View as plain text