jsoniter_1dot8_only_test.go 1.1 KB

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