github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_raw_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestIterator_Raw_ReadRaw(t *testing.T) {
    11  	t.Run("eof", func(t *testing.T) {
    12  		withIterator("", func(it *Iterator) {
    13  			_, err := it.ReadRaw()
    14  			require.Equal(t, io.EOF, err)
    15  		})
    16  	})
    17  	t.Run("error", func(t *testing.T) {
    18  		withIterator(" + ", func(it *Iterator) {
    19  			_, err := it.ReadRaw()
    20  			require.IsType(t, UnexpectedByteError{}, err)
    21  		})
    22  	})
    23  	t.Run("no error", func(t *testing.T) {
    24  		exp := `{ " " : null }`
    25  		data := []byte(" " + exp + " ")
    26  		withIterator(localByteToString(data), func(it *Iterator) {
    27  			raw, err := it.ReadRaw()
    28  			require.NoError(t, err)
    29  			require.Equal(t, exp, string(raw))
    30  
    31  			copy(data, []byte(exp+"  ")) // modify content
    32  			require.Equal(t, exp, string(raw))
    33  
    34  			_, err = it.ReadRaw()
    35  			require.Equal(t, io.EOF, err)
    36  		})
    37  	})
    38  }
    39  
    40  func TestIterator_Raw_AppendRaw(t *testing.T) {
    41  	data := `{}`
    42  	withIterator(data, func(it *Iterator) {
    43  		prefix := []byte(`test`)
    44  		b, err := it.AppendRaw(prefix)
    45  		require.NoError(t, err)
    46  		require.Equal(t, append(prefix, data...), b)
    47  	})
    48  }