jsoniter_stream_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package jsoniter
  2. import (
  3. "github.com/json-iterator/go/require"
  4. "testing"
  5. )
  6. func Test_writeByte_should_grow_buffer(t *testing.T) {
  7. should := require.New(t)
  8. stream := NewStream(nil, 1)
  9. stream.writeByte('1')
  10. should.Equal("1", string(stream.Buffer()))
  11. should.Equal(1, len(stream.buf))
  12. stream.writeByte('2')
  13. should.Equal("12", string(stream.Buffer()))
  14. should.Equal(2, len(stream.buf))
  15. stream.writeThreeBytes('3', '4', '5')
  16. should.Equal("12345", string(stream.Buffer()))
  17. }
  18. func Test_writeBytes_should_grow_buffer(t *testing.T) {
  19. should := require.New(t)
  20. stream := NewStream(nil, 1)
  21. stream.Write([]byte{'1', '2'})
  22. should.Equal("12", string(stream.Buffer()))
  23. should.Equal(3, len(stream.buf))
  24. stream.Write([]byte{'3', '4', '5', '6', '7'})
  25. should.Equal("1234567", string(stream.Buffer()))
  26. should.Equal(8, len(stream.buf))
  27. }
  28. func Test_writeIndention_should_grow_buffer(t *testing.T) {
  29. should := require.New(t)
  30. stream := NewStream(nil, 1)
  31. stream.IndentionStep = 2
  32. stream.WriteVal([]int{1, 2, 3})
  33. should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
  34. }
  35. func Test_writeRaw_should_grow_buffer(t *testing.T) {
  36. should := require.New(t)
  37. stream := NewStream(nil, 1)
  38. stream.WriteRaw("123")
  39. should.Nil(stream.Error)
  40. should.Equal("123", string(stream.Buffer()))
  41. }
  42. func Test_writeString_should_grow_buffer(t *testing.T) {
  43. should := require.New(t)
  44. stream := NewStream(nil, 0)
  45. stream.WriteString("123")
  46. should.Nil(stream.Error)
  47. should.Equal(`"123"`, string(stream.Buffer()))
  48. }