github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_skip_str_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_Skip_SkipString(t *testing.T) {
    11  	t.Run("eof", func(t *testing.T) {
    12  		withIterator("", func(it *Iterator) {
    13  			err := it.SkipString()
    14  			require.Equal(t, io.EOF, err)
    15  		})
    16  	})
    17  	t.Run("invalid first byte", func(t *testing.T) {
    18  		withIterator(` a`, func(it *Iterator) {
    19  			err := it.SkipString()
    20  			require.IsType(t, UnexpectedByteError{}, err)
    21  		})
    22  	})
    23  	t.Run("empty string", func(t *testing.T) {
    24  		withIterator(` ""`, func(it *Iterator) {
    25  			err := it.SkipString()
    26  			require.NoError(t, err)
    27  			_, err = it.NextValueType()
    28  			require.Equal(t, io.EOF, err)
    29  		})
    30  	})
    31  	t.Run("eof on escape", func(t *testing.T) {
    32  		withIterator(` "\`, func(it *Iterator) {
    33  			err := it.SkipString()
    34  			require.Equal(t, io.EOF, err)
    35  		})
    36  	})
    37  	t.Run("valid escape", func(t *testing.T) {
    38  		withIterator(` "\t" `, func(it *Iterator) {
    39  			err := it.SkipString()
    40  			require.NoError(t, err)
    41  			_, err = it.NextValueType()
    42  			require.Equal(t, io.EOF, err)
    43  		})
    44  	})
    45  	t.Run("invalid escape", func(t *testing.T) {
    46  		withIterator(` "\a`, func(it *Iterator) {
    47  			err := it.SkipString()
    48  			require.IsType(t, InvalidEscapeCharError{}, err)
    49  		})
    50  	})
    51  	t.Run("invalid u4", func(t *testing.T) {
    52  		withIterator(` "\uX`, func(it *Iterator) {
    53  			err := it.SkipString()
    54  			require.IsType(t, InvalidUnicodeCharError{}, err)
    55  		})
    56  	})
    57  	t.Run("eof u4", func(t *testing.T) {
    58  		withIterator(` "\u0`, func(it *Iterator) {
    59  			err := it.SkipString()
    60  			require.Equal(t, io.EOF, err)
    61  		})
    62  	})
    63  	t.Run("valid u4", func(t *testing.T) {
    64  		withIterator(` "\u0000" `, func(it *Iterator) {
    65  			err := it.SkipString()
    66  			require.NoError(t, err)
    67  			_, err = it.NextValueType()
    68  			require.Equal(t, io.EOF, err)
    69  		})
    70  	})
    71  	t.Run("invalid string char", func(t *testing.T) {
    72  		s := []byte{' ', '"', 0}
    73  		withIterator(localByteToString(s), func(it *Iterator) {
    74  			err := it.SkipString()
    75  			require.IsType(t, InvalidStringCharError{}, err)
    76  		})
    77  	})
    78  	t.Run("eof after first byte", func(t *testing.T) {
    79  		withIterator(` " `, func(it *Iterator) {
    80  			err := it.SkipString()
    81  			require.Equal(t, io.EOF, err)
    82  		})
    83  	})
    84  }