github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_skip_array_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_SkipArray(t *testing.T) { 11 t.Run("eof", func(t *testing.T) { 12 withIterator("", func(it *Iterator) { 13 err := it.SkipArray() 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.SkipArray() 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.SkipArray() 26 require.Equal(t, io.EOF, err) 27 }) 28 }) 29 t.Run("empty array", func(t *testing.T) { 30 withIterator(" [ ] ", func(it *Iterator) { 31 err := it.SkipArray() 32 require.NoError(t, err) 33 _, err = it.NextValueType() 34 require.Equal(t, io.EOF, err) 35 }) 36 }) 37 t.Run("invalid byte after bracket", func(t *testing.T) { 38 withIterator(" [ ,", func(it *Iterator) { 39 err := it.SkipArray() 40 require.IsType(t, UnexpectedByteError{}, err) 41 }) 42 }) 43 t.Run("eof after first element", func(t *testing.T) { 44 withIterator(" [ 1 ", func(it *Iterator) { 45 err := it.SkipArray() 46 require.Equal(t, io.EOF, err) 47 }) 48 }) 49 t.Run("one element", func(t *testing.T) { 50 withIterator(" [ 1 ] ", func(it *Iterator) { 51 err := it.SkipArray() 52 require.NoError(t, err) 53 }) 54 }) 55 t.Run("invalid byte after element", func(t *testing.T) { 56 withIterator(" [ 1 [ ", func(it *Iterator) { 57 err := it.SkipArray() 58 require.IsType(t, UnexpectedByteError{}, err) 59 }) 60 }) 61 t.Run("eof after dot", func(t *testing.T) { 62 withIterator(" [ 1 , ", func(it *Iterator) { 63 err := it.SkipArray() 64 require.Equal(t, io.EOF, err) 65 }) 66 }) 67 t.Run("two elements", func(t *testing.T) { 68 withIterator(" [ 1 , null ] ", func(it *Iterator) { 69 err := it.SkipArray() 70 require.NoError(t, err) 71 }) 72 }) 73 t.Run("nested error eof 1", func(t *testing.T) { 74 withIterator(` [ [ `, func(it *Iterator) { 75 err := it.SkipArray() 76 require.Equal(t, io.EOF, err) 77 }) 78 }) 79 t.Run("nested error 1", func(t *testing.T) { 80 withIterator(` [ [ 1 a`, func(it *Iterator) { 81 err := it.SkipArray() 82 require.IsType(t, UnexpectedByteError{}, err) 83 }) 84 }) 85 t.Run("nested error eof 2", func(t *testing.T) { 86 withIterator(` [ [ 1 , `, func(it *Iterator) { 87 err := it.SkipArray() 88 require.Equal(t, io.EOF, err) 89 }) 90 }) 91 t.Run("nested error token", func(t *testing.T) { 92 withIterator(` [ [ 1 , +`, func(it *Iterator) { 93 err := it.SkipArray() 94 require.IsType(t, UnexpectedByteError{}, err) 95 }) 96 }) 97 t.Run("nested 1", func(t *testing.T) { 98 s := nestedArray1(100) 99 withIterator(s, func(it *Iterator) { 100 err := it.SkipArray() 101 require.NoError(t, err) 102 _, err = it.NextValueType() 103 require.Equal(t, io.EOF, err) 104 }) 105 }) 106 t.Run("nested 2", func(t *testing.T) { 107 s := nestedArray2(100) 108 withIterator(s, func(it *Iterator) { 109 err := it.SkipArray() 110 require.NoError(t, err) 111 _, err = it.NextValueType() 112 require.Equal(t, io.EOF, err) 113 }) 114 }) 115 t.Run("nested with object", func(t *testing.T) { 116 s := nestedArrayWithObject(100) 117 withIterator(s, func(it *Iterator) { 118 err := it.SkipArray() 119 require.NoError(t, err) 120 _, err = it.NextValueType() 121 require.Equal(t, io.EOF, err) 122 }) 123 }) 124 }