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