github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_number_test.go (about) 1 package jzon 2 3 import ( 4 "errors" 5 "io" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestIterator_Number_ReadNumber(t *testing.T) { 13 t.Run("eof", func(t *testing.T) { 14 withIterator("", func(it *Iterator) { 15 _, err := it.ReadNumber() 16 require.Equal(t, io.EOF, err) 17 }) 18 }) 19 t.Run("invalid type", func(t *testing.T) { 20 withIterator(" +", func(it *Iterator) { 21 _, err := it.ReadNumber() 22 require.IsType(t, UnexpectedByteError{}, err) 23 }) 24 }) 25 t.Run("negative eof", func(t *testing.T) { 26 withIterator(" -", func(it *Iterator) { 27 _, err := it.ReadNumber() 28 require.Equal(t, io.EOF, err) 29 }) 30 }) 31 t.Run("invalid first byte after dash", func(t *testing.T) { 32 withIterator(" --", func(it *Iterator) { 33 _, err := it.ReadNumber() 34 require.IsType(t, InvalidDigitError{}, err) 35 }) 36 }) 37 } 38 39 func TestIterator_Number_ReadNumber_LeadingZero(t *testing.T) { 40 t.Run("one zero", func(t *testing.T) { 41 withIterator(" 0", func(it *Iterator) { 42 n, err := it.ReadNumber() 43 require.NoError(t, err) 44 require.Equal(t, "0", string(n)) 45 }) 46 }) 47 t.Run("reader error", func(t *testing.T) { 48 withIterator("", func(it *Iterator) { 49 e := errors.New("test") 50 it.Reset(&stepByteReader{ 51 b: " 0", 52 err: e, 53 }) 54 _, err := it.ReadNumber() 55 require.Equal(t, e, err) 56 }) 57 }) 58 t.Run("double zero", func(t *testing.T) { 59 withIterator(" 00", func(it *Iterator) { 60 n, err := it.ReadNumber() 61 require.NoError(t, err) 62 require.Equal(t, "0", string(n)) 63 }) 64 }) 65 t.Run("fraction eof", func(t *testing.T) { 66 withIterator(" 0.", func(it *Iterator) { 67 _, err := it.ReadNumber() 68 require.Equal(t, io.EOF, err) 69 }) 70 }) 71 t.Run("invalid fraction", func(t *testing.T) { 72 withIterator(" 0.a", func(it *Iterator) { 73 _, err := it.ReadNumber() 74 require.IsType(t, InvalidDigitError{}, err) 75 }) 76 }) 77 t.Run("fraction end with eof", func(t *testing.T) { 78 withIterator(" 0.12", func(it *Iterator) { 79 n, err := it.ReadNumber() 80 require.NoError(t, err) 81 require.Equal(t, "0.12", string(n)) 82 }) 83 }) 84 t.Run("fraction end with other char", func(t *testing.T) { 85 withIterator(" 0.12 ", func(it *Iterator) { 86 n, err := it.ReadNumber() 87 require.NoError(t, err) 88 require.Equal(t, "0.12", string(n)) 89 }) 90 }) 91 t.Run("fraction error", func(t *testing.T) { 92 withIterator("", func(it *Iterator) { 93 e := errors.New("test") 94 it.Reset(&stepByteReader{ 95 b: " 0.12", 96 err: e, 97 }) 98 _, err := it.ReadNumber() 99 require.Equal(t, e, err) 100 }) 101 }) 102 t.Run("exponent eof", func(t *testing.T) { 103 withIterator(" 0.1e", func(it *Iterator) { 104 _, err := it.ReadNumber() 105 require.Equal(t, io.EOF, err) 106 }) 107 }) 108 t.Run("exponent eof after sign", func(t *testing.T) { 109 withIterator(" 0.1e+", func(it *Iterator) { 110 _, err := it.ReadNumber() 111 require.Equal(t, io.EOF, err) 112 }) 113 }) 114 t.Run("exponent invalid byte after sign", func(t *testing.T) { 115 withIterator(" 0.1e++", func(it *Iterator) { 116 _, err := it.ReadNumber() 117 require.IsType(t, InvalidDigitError{}, err) 118 }) 119 }) 120 t.Run("exponent end with eof", func(t *testing.T) { 121 withIterator(" 0.1e+2", func(it *Iterator) { 122 n, err := it.ReadNumber() 123 require.NoError(t, err) 124 require.Equal(t, "0.1e+2", string(n)) 125 }) 126 }) 127 t.Run("exponent end with another char", func(t *testing.T) { 128 withIterator(" 0.1e+2 ", func(it *Iterator) { 129 n, err := it.ReadNumber() 130 require.NoError(t, err) 131 require.Equal(t, "0.1e+2", string(n)) 132 }) 133 }) 134 t.Run("exponent end error", func(t *testing.T) { 135 withIterator("", func(it *Iterator) { 136 e := errors.New("test") 137 it.Reset(&stepByteReader{ 138 b: " 0.1e+2", 139 err: e, 140 }) 141 _, err := it.ReadNumber() 142 require.Equal(t, e, err) 143 }) 144 }) 145 t.Run("exponent only", func(t *testing.T) { 146 withIterator(" 0e+1 ", func(it *Iterator) { 147 n, err := it.ReadNumber() 148 require.NoError(t, err) 149 require.Equal(t, "0e+1", string(n)) 150 }) 151 }) 152 } 153 154 func TestIterator_Number_ReadNumber_NonLeadingZero(t *testing.T) { 155 t.Run("integer", func(t *testing.T) { 156 withIterator(" 1", func(it *Iterator) { 157 n, err := it.ReadNumber() 158 require.NoError(t, err) 159 require.Equal(t, "1", string(n)) 160 }) 161 }) 162 t.Run("reader error", func(t *testing.T) { 163 withIterator("", func(it *Iterator) { 164 e := errors.New("test") 165 it.Reset(&stepByteReader{ 166 b: " 1", 167 err: e, 168 }) 169 _, err := it.ReadNumber() 170 require.Equal(t, e, err) 171 }) 172 }) 173 t.Run("double digit", func(t *testing.T) { 174 withIterator(" 12", func(it *Iterator) { 175 n, err := it.ReadNumber() 176 require.NoError(t, err) 177 require.Equal(t, "12", string(n)) 178 }) 179 }) 180 t.Run("double digit end with other char", func(t *testing.T) { 181 withIterator(" 12 ", func(it *Iterator) { 182 n, err := it.ReadNumber() 183 require.NoError(t, err) 184 require.Equal(t, "12", string(n)) 185 }) 186 }) 187 t.Run("fraction eof", func(t *testing.T) { 188 withIterator(" 1.", func(it *Iterator) { 189 _, err := it.ReadNumber() 190 require.Equal(t, io.EOF, err) 191 }) 192 }) 193 t.Run("invalid fraction", func(t *testing.T) { 194 withIterator(" 1.+", func(it *Iterator) { 195 _, err := it.ReadNumber() 196 require.IsType(t, InvalidDigitError{}, err) 197 }) 198 }) 199 t.Run("fraction end with eof", func(t *testing.T) { 200 withIterator(" 1.23", func(it *Iterator) { 201 n, err := it.ReadNumber() 202 require.NoError(t, err) 203 require.Equal(t, "1.23", string(n)) 204 }) 205 }) 206 t.Run("fraction end with other char", func(t *testing.T) { 207 withIterator(" 1.23 ", func(it *Iterator) { 208 n, err := it.ReadNumber() 209 require.NoError(t, err) 210 require.Equal(t, "1.23", string(n)) 211 }) 212 }) 213 t.Run("fraction error", func(t *testing.T) { 214 withIterator("", func(it *Iterator) { 215 e := errors.New("test") 216 it.Reset(&stepByteReader{ 217 b: " 1.23", 218 err: e, 219 }) 220 _, err := it.ReadNumber() 221 require.Equal(t, e, err) 222 }) 223 }) 224 t.Run("exponent eof", func(t *testing.T) { 225 withIterator(" 1.2e", func(it *Iterator) { 226 _, err := it.ReadNumber() 227 require.Equal(t, io.EOF, err) 228 }) 229 }) 230 t.Run("exponent eof after sign", func(t *testing.T) { 231 withIterator(" 1.2e+", func(it *Iterator) { 232 _, err := it.ReadNumber() 233 require.Equal(t, io.EOF, err) 234 }) 235 }) 236 t.Run("exponent invalid byte after sign", func(t *testing.T) { 237 withIterator(" 1.2e++", func(it *Iterator) { 238 _, err := it.ReadNumber() 239 require.IsType(t, InvalidDigitError{}, err) 240 }) 241 }) 242 t.Run("exponent end with eof", func(t *testing.T) { 243 withIterator(" 1.2e+3", func(it *Iterator) { 244 n, err := it.ReadNumber() 245 require.NoError(t, err) 246 require.Equal(t, "1.2e+3", string(n)) 247 }) 248 }) 249 t.Run("exponent end with another char", func(t *testing.T) { 250 withIterator(" 1.2e+3 ", func(it *Iterator) { 251 n, err := it.ReadNumber() 252 require.NoError(t, err) 253 require.Equal(t, "1.2e+3", string(n)) 254 }) 255 }) 256 t.Run("exponent end error", func(t *testing.T) { 257 withIterator("", func(it *Iterator) { 258 e := errors.New("test") 259 it.Reset(&stepByteReader{ 260 b: " 1.2e+3", 261 err: e, 262 }) 263 _, err := it.ReadNumber() 264 require.Equal(t, e, err) 265 }) 266 }) 267 t.Run("exponent only", func(t *testing.T) { 268 withIterator(" 1e+2 ", func(it *Iterator) { 269 n, err := it.ReadNumber() 270 require.NoError(t, err) 271 require.Equal(t, "1e+2", string(n)) 272 }) 273 }) 274 } 275 276 func TestIterator_Number_ReadNumber_LargeNumber(t *testing.T) { 277 s := "-" + strings.Repeat("123", 20) + "." + 278 strings.Repeat("0456", 20) + "e+" + 279 strings.Repeat("789", 20) 280 withIterator(" "+s+" ", func(it *Iterator) { 281 n, err := it.ReadNumber() 282 require.NoError(t, err) 283 require.Equal(t, s, string(n)) 284 }) 285 }