github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_array_test.go (about) 1 package jzon 2 3 import ( 4 "errors" 5 "io" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestIterator_Array_ReadArrayBegin(t *testing.T) { 12 t.Run("leading space", func(t *testing.T) { 13 withIterator(" [ ]", func(it *Iterator) { 14 more, err := it.ReadArrayBegin() 15 require.NoError(t, err) 16 require.False(t, more) 17 }) 18 }) 19 t.Run("eof", func(t *testing.T) { 20 withIterator("", func(it *Iterator) { 21 _, err := it.ReadArrayBegin() 22 require.Equal(t, io.EOF, err) 23 }) 24 }) 25 t.Run("invalid first byte", func(t *testing.T) { 26 withIterator("1", func(it *Iterator) { 27 _, err := it.ReadArrayBegin() 28 require.IsType(t, UnexpectedByteError{}, err) 29 }) 30 }) 31 t.Run("eof after bracket", func(t *testing.T) { 32 withIterator("[", func(it *Iterator) { 33 _, err := it.ReadArrayBegin() 34 require.Equal(t, io.EOF, err) 35 }) 36 }) 37 t.Run("more", func(t *testing.T) { 38 withIterator(`["`, func(it *Iterator) { 39 more, err := it.ReadArrayBegin() 40 require.NoError(t, err) 41 require.True(t, more) 42 }) 43 }) 44 } 45 46 func TestIterator_Array_ReadArrayMore(t *testing.T) { 47 t.Run("leading space", func(t *testing.T) { 48 withIterator(" ,", func(it *Iterator) { 49 more, err := it.ReadArrayMore() 50 require.NoError(t, err) 51 require.True(t, more) 52 }) 53 }) 54 t.Run("eof", func(t *testing.T) { 55 withIterator(" ", func(it *Iterator) { 56 _, err := it.ReadArrayMore() 57 require.Equal(t, io.EOF, err) 58 }) 59 }) 60 t.Run("more", func(t *testing.T) { 61 withIterator(" ,", func(it *Iterator) { 62 more, err := it.ReadArrayMore() 63 require.NoError(t, err) 64 require.True(t, more) 65 }) 66 }) 67 t.Run("no more", func(t *testing.T) { 68 withIterator(" ]", func(it *Iterator) { 69 more, err := it.ReadArrayMore() 70 require.NoError(t, err) 71 require.False(t, more) 72 }) 73 }) 74 t.Run("error", func(t *testing.T) { 75 withIterator("a", func(it *Iterator) { 76 _, err := it.ReadArrayMore() 77 require.IsType(t, UnexpectedByteError{}, err) 78 }) 79 }) 80 } 81 82 func TestIterator_Array_ReadArray_Example(t *testing.T) { 83 withIterator(" [ 0 , 1 , 2 ] ", func(it *Iterator) { 84 must := require.New(t) 85 more, err := it.ReadArrayBegin() 86 must.NoError(err) 87 i := 0 88 for ; more; more, err = it.ReadArrayMore() { 89 ri, err := it.ReadInt() 90 must.NoError(err) 91 must.Equal(i, ri) 92 i++ 93 } 94 must.NoError(err) 95 _, err = it.NextValueType() 96 must.Equal(io.EOF, err) 97 }) 98 } 99 100 func TestIterator_Array_ReadArrayCB(t *testing.T) { 101 t.Run("leading space", func(t *testing.T) { 102 withIterator(" [ ]", func(it *Iterator) { 103 err := it.ReadArrayCB(nil) 104 require.NoError(t, err) 105 }) 106 }) 107 t.Run("eof", func(t *testing.T) { 108 withIterator(" ", func(it *Iterator) { 109 err := it.ReadArrayCB(nil) 110 require.Equal(t, io.EOF, err) 111 }) 112 }) 113 t.Run("invalid bracket", func(t *testing.T) { 114 withIterator(" a", func(it *Iterator) { 115 err := it.ReadArrayCB(nil) 116 require.IsType(t, UnexpectedByteError{}, err) 117 }) 118 }) 119 t.Run("eof after bracket", func(t *testing.T) { 120 withIterator(" [ ", func(it *Iterator) { 121 err := it.ReadArrayCB(nil) 122 require.Equal(t, io.EOF, err) 123 }) 124 }) 125 t.Run("no element", func(t *testing.T) { 126 withIterator(" [ ] ", func(it *Iterator) { 127 err := it.ReadArrayCB(nil) 128 require.NoError(t, err) 129 }) 130 }) 131 t.Run("callback error", func(t *testing.T) { 132 withIterator(" [ 1 ] ", func(it *Iterator) { 133 e := errors.New("test") 134 err := it.ReadArrayCB(func(*Iterator) error { 135 return e 136 }) 137 require.Equal(t, e, err) 138 }) 139 }) 140 t.Run("error on more", func(t *testing.T) { 141 withIterator(" [ 1 ", func(it *Iterator) { 142 err := it.ReadArrayCB(func(it *Iterator) (err error) { 143 _, err = it.ReadUint() 144 return 145 }) 146 require.Equal(t, io.EOF, err) 147 }) 148 }) 149 t.Run("error on more 2", func(t *testing.T) { 150 withIterator(" [ 1a", func(it *Iterator) { 151 err := it.ReadArrayCB(func(it *Iterator) (err error) { 152 _, err = it.ReadUint() 153 return 154 }) 155 require.IsType(t, UnexpectedByteError{}, err) 156 }) 157 }) 158 t.Run("2 items", func(t *testing.T) { 159 withIterator(" [ 1 , 2 ] ", func(it *Iterator) { 160 err := it.ReadArrayCB(func(it *Iterator) (err error) { 161 _, err = it.ReadUint() 162 return 163 }) 164 require.NoError(t, err) 165 }) 166 }) 167 } 168 169 func TestIterator_Array_ReadArrayCB_Example(t *testing.T) { 170 withIterator(" [ 0 , 1 , 2 ] ", func(it *Iterator) { 171 must := require.New(t) 172 i := 0 173 err := it.ReadArrayCB(func(it *Iterator) (err error) { 174 j, err := it.ReadInt() 175 must.NoError(err) 176 must.Equal(i, j) 177 i++ 178 return nil 179 }) 180 must.NoError(err) 181 _, err = it.NextValueType() 182 must.Equal(io.EOF, err) 183 }) 184 }