github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_skip_object_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_SkipObject(t *testing.T) {
    11  	t.Run("eof", func(t *testing.T) {
    12  		withIterator("", func(it *Iterator) {
    13  			err := it.SkipObject()
    14  			require.Equal(t, io.EOF, err)
    15  		})
    16  	})
    17  	t.Run("invalid first byte", func(t *testing.T) {
    18  		withIterator(" }", func(it *Iterator) {
    19  			err := it.SkipObject()
    20  			require.IsType(t, UnexpectedByteError{}, err)
    21  		})
    22  	})
    23  	t.Run("eof after bracket", func(t *testing.T) {
    24  		withIterator(" {", func(it *Iterator) {
    25  			err := it.SkipObject()
    26  			require.Equal(t, io.EOF, err)
    27  		})
    28  	})
    29  	t.Run("empty", func(t *testing.T) {
    30  		withIterator(" { } ", func(it *Iterator) {
    31  			err := it.SkipObject()
    32  			require.NoError(t, err)
    33  			_, err = it.NextValueType()
    34  			require.Equal(t, io.EOF, err)
    35  		})
    36  	})
    37  	t.Run("invalid field first byte", func(t *testing.T) {
    38  		withIterator(` { 1`, func(it *Iterator) {
    39  			err := it.SkipObject()
    40  			require.IsType(t, UnexpectedByteError{}, err)
    41  		})
    42  	})
    43  	t.Run("field error", func(t *testing.T) {
    44  		withIterator(` { " " `, func(it *Iterator) {
    45  			err := it.SkipObject()
    46  			require.Equal(t, io.EOF, err)
    47  		})
    48  	})
    49  	t.Run("value eof", func(t *testing.T) {
    50  		withIterator(` { " " : `, func(it *Iterator) {
    51  			err := it.SkipObject()
    52  			require.Equal(t, io.EOF, err)
    53  		})
    54  	})
    55  	t.Run("invalid first value", func(t *testing.T) {
    56  		withIterator(` { " " : +`, func(it *Iterator) {
    57  			err := it.SkipObject()
    58  			require.IsType(t, UnexpectedByteError{}, err)
    59  		})
    60  	})
    61  	t.Run("eof after first value", func(t *testing.T) {
    62  		withIterator(` { " " : 1 `, func(it *Iterator) {
    63  			err := it.SkipObject()
    64  			require.Equal(t, io.EOF, err)
    65  		})
    66  	})
    67  	t.Run("only one pair", func(t *testing.T) {
    68  		withIterator(` { " " : 1 } `, func(it *Iterator) {
    69  			err := it.SkipObject()
    70  			require.NoError(t, err)
    71  		})
    72  	})
    73  	t.Run("non nested second item dot error", func(t *testing.T) {
    74  		withIterator(` { " " : true a `, func(it *Iterator) {
    75  			err := it.SkipObject()
    76  			require.IsType(t, UnexpectedByteError{}, err)
    77  		})
    78  	})
    79  	t.Run("non nested second item eof", func(t *testing.T) {
    80  		withIterator(` { " " : false , `, func(it *Iterator) {
    81  			err := it.SkipObject()
    82  			require.Equal(t, io.EOF, err)
    83  		})
    84  	})
    85  	t.Run("non nested second item field error", func(t *testing.T) {
    86  		withIterator(` { " " : false , a`, func(it *Iterator) {
    87  			err := it.SkipObject()
    88  			require.IsType(t, UnexpectedByteError{}, err)
    89  		})
    90  	})
    91  	t.Run("non nested second item", func(t *testing.T) {
    92  		withIterator(` { " " : false , " " : true } `, func(it *Iterator) {
    93  			err := it.SkipObject()
    94  			require.NoError(t, err)
    95  		})
    96  	})
    97  	t.Run("nested eof", func(t *testing.T) {
    98  		withIterator(` { " " : { `, func(it *Iterator) {
    99  			err := it.SkipObject()
   100  			require.Equal(t, io.EOF, err)
   101  		})
   102  	})
   103  	t.Run("nested empty value", func(t *testing.T) {
   104  		withIterator(` { " " : { } } `, func(it *Iterator) {
   105  			err := it.SkipObject()
   106  			require.NoError(t, err)
   107  		})
   108  	})
   109  	t.Run("nested second item comma error", func(t *testing.T) {
   110  		withIterator(` { " " : { } a `, func(it *Iterator) {
   111  			err := it.SkipObject()
   112  			require.IsType(t, UnexpectedByteError{}, err)
   113  		})
   114  	})
   115  	t.Run("nested second item no field", func(t *testing.T) {
   116  		withIterator(` { " " : { } , `, func(it *Iterator) {
   117  			err := it.SkipObject()
   118  			require.Equal(t, io.EOF, err)
   119  		})
   120  	})
   121  	t.Run("nested second item bad field", func(t *testing.T) {
   122  		withIterator(` { " " : { } , a`, func(it *Iterator) {
   123  			err := it.SkipObject()
   124  			require.IsType(t, UnexpectedByteError{}, err)
   125  		})
   126  	})
   127  	t.Run("nested second item field eof", func(t *testing.T) {
   128  		withIterator(` { " " : { } , " " `, func(it *Iterator) {
   129  			err := it.SkipObject()
   130  			require.Equal(t, io.EOF, err)
   131  		})
   132  	})
   133  	t.Run("nested second item eof value", func(t *testing.T) {
   134  		withIterator(` { " " : { } , " " : `, func(it *Iterator) {
   135  			err := it.SkipObject()
   136  			require.Equal(t, io.EOF, err)
   137  		})
   138  	})
   139  	t.Run("nested second item bad value", func(t *testing.T) {
   140  		withIterator(` { " " : { } , " " : } `, func(it *Iterator) {
   141  			err := it.SkipObject()
   142  			require.IsType(t, UnexpectedByteError{}, err)
   143  		})
   144  	})
   145  	t.Run("nested second item", func(t *testing.T) {
   146  		withIterator(` { " " : { } , " " : 0 } `, func(it *Iterator) {
   147  			err := it.SkipObject()
   148  			require.NoError(t, err)
   149  			_, err = it.NextValueType()
   150  			require.Equal(t, io.EOF, err)
   151  		})
   152  	})
   153  	t.Run("nested", func(t *testing.T) {
   154  		s := nestedObject(100)
   155  		withIterator(s, func(it *Iterator) {
   156  			err := it.SkipObject()
   157  			require.NoError(t, err)
   158  			_, err = it.NextValueType()
   159  			require.Equal(t, io.EOF, err)
   160  		})
   161  	})
   162  	t.Run("nested with array", func(t *testing.T) {
   163  		s := nestedObjectWithArray(100)
   164  		withIterator(s, func(it *Iterator) {
   165  			err := it.SkipObject()
   166  			require.NoError(t, err)
   167  			_, err = it.NextValueType()
   168  			require.Equal(t, io.EOF, err)
   169  		})
   170  	})
   171  }