jsoniter_io_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "github.com/stretchr/testify/require"
  5. "io"
  6. "testing"
  7. )
  8. func Test_read_by_one(t *testing.T) {
  9. iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 1)
  10. b := iter.readByte()
  11. if iter.Error != nil {
  12. t.Fatal(iter.Error)
  13. }
  14. if b != 'a' {
  15. t.Fatal(b)
  16. }
  17. iter.unreadByte()
  18. if iter.Error != nil {
  19. t.Fatal(iter.Error)
  20. }
  21. b = iter.readByte()
  22. if iter.Error != nil {
  23. t.Fatal(iter.Error)
  24. }
  25. if b != 'a' {
  26. t.Fatal(b)
  27. }
  28. }
  29. func Test_read_by_two(t *testing.T) {
  30. should := require.New(t)
  31. iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
  32. b := iter.readByte()
  33. should.Nil(iter.Error)
  34. should.Equal(byte('a'), b)
  35. b = iter.readByte()
  36. should.Nil(iter.Error)
  37. should.Equal(byte('b'), b)
  38. iter.unreadByte()
  39. should.Nil(iter.Error)
  40. iter.unreadByte()
  41. should.Nil(iter.Error)
  42. b = iter.readByte()
  43. should.Nil(iter.Error)
  44. should.Equal(byte('a'), b)
  45. }
  46. func Test_read_until_eof(t *testing.T) {
  47. iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
  48. iter.readByte()
  49. iter.readByte()
  50. b := iter.readByte()
  51. if iter.Error != nil {
  52. t.Fatal(iter.Error)
  53. }
  54. if b != 'c' {
  55. t.Fatal(b)
  56. }
  57. iter.readByte()
  58. if iter.Error != io.EOF {
  59. t.Fatal(iter.Error)
  60. }
  61. }