github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_skip_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_Skip_SkipNumber(t *testing.T) {
    13  	t.Run("eof", func(t *testing.T) {
    14  		withIterator("", func(it *Iterator) {
    15  			err := it.SkipNumber()
    16  			require.Equal(t, io.EOF, err)
    17  		})
    18  	})
    19  	t.Run("invalid first byte", func(t *testing.T) {
    20  		withIterator(" +", func(it *Iterator) {
    21  			err := it.SkipNumber()
    22  			require.IsType(t, UnexpectedByteError{}, err)
    23  		})
    24  	})
    25  	t.Run("eof after negative sign", func(t *testing.T) {
    26  		withIterator(" -", func(it *Iterator) {
    27  			err := it.SkipNumber()
    28  			require.Equal(t, io.EOF, err)
    29  		})
    30  	})
    31  	t.Run("invalid char after negative sign", func(t *testing.T) {
    32  		withIterator(" - 1 ", func(it *Iterator) {
    33  			err := it.SkipNumber()
    34  			require.IsType(t, InvalidDigitError{}, err)
    35  		})
    36  	})
    37  	t.Run("zero", func(t *testing.T) {
    38  		withIterator(" -0", func(it *Iterator) {
    39  			err := it.SkipNumber()
    40  			require.NoError(t, err)
    41  		})
    42  	})
    43  	t.Run("reader error after zero", func(t *testing.T) {
    44  		withIterator("", func(it *Iterator) {
    45  			e := errors.New("test")
    46  			it.Reset(&stepByteReader{
    47  				b:   " -0",
    48  				err: e,
    49  			})
    50  			err := it.SkipNumber()
    51  			require.Equal(t, e, err)
    52  		})
    53  	})
    54  	t.Run("double zero", func(t *testing.T) {
    55  		withIterator(" -00 ", func(it *Iterator) {
    56  			err := it.SkipNumber()
    57  			require.NoError(t, err)
    58  		})
    59  	})
    60  	t.Run("zero with fraction", func(t *testing.T) {
    61  		withIterator(" -0.1 ", func(it *Iterator) {
    62  			err := it.SkipNumber()
    63  			require.NoError(t, err)
    64  			_, err = it.NextValueType()
    65  			require.Equal(t, io.EOF, err)
    66  		})
    67  	})
    68  	t.Run("zero with exponent", func(t *testing.T) {
    69  		withIterator(" -0e+1 ", func(it *Iterator) {
    70  			err := it.SkipNumber()
    71  			require.NoError(t, err)
    72  			_, err = it.NextValueType()
    73  			require.Equal(t, io.EOF, err)
    74  		})
    75  	})
    76  	t.Run("non zero", func(t *testing.T) {
    77  		withIterator(" 1 ", func(it *Iterator) {
    78  			err := it.SkipNumber()
    79  			require.NoError(t, err)
    80  			_, err = it.NextValueType()
    81  			require.Equal(t, io.EOF, err)
    82  		})
    83  	})
    84  	t.Run("non zero with fraction", func(t *testing.T) {
    85  		withIterator(" 1.1 ", func(it *Iterator) {
    86  			err := it.SkipNumber()
    87  			require.NoError(t, err)
    88  			_, err = it.NextValueType()
    89  			require.Equal(t, io.EOF, err)
    90  		})
    91  	})
    92  	t.Run("non zero with exponent", func(t *testing.T) {
    93  		withIterator(" 1e-1 ", func(it *Iterator) {
    94  			err := it.SkipNumber()
    95  			require.NoError(t, err)
    96  			_, err = it.NextValueType()
    97  			require.Equal(t, io.EOF, err)
    98  		})
    99  	})
   100  	t.Run("non zero eof", func(t *testing.T) {
   101  		withIterator(" 1", func(it *Iterator) {
   102  			err := it.SkipNumber()
   103  			require.NoError(t, err)
   104  			_, err = it.NextValueType()
   105  			require.Equal(t, io.EOF, err)
   106  		})
   107  	})
   108  	t.Run("non zero reader error", func(t *testing.T) {
   109  		withIterator("", func(it *Iterator) {
   110  			e := errors.New("test")
   111  			it.Reset(&stepByteReader{
   112  				b:   " 1",
   113  				err: e,
   114  			})
   115  			err := it.SkipNumber()
   116  			require.Equal(t, e, err)
   117  		})
   118  	})
   119  	t.Run("fraction empty", func(t *testing.T) {
   120  		withIterator(" 0.", func(it *Iterator) {
   121  			err := it.SkipNumber()
   122  			require.Equal(t, io.EOF, err)
   123  		})
   124  	})
   125  	t.Run("fraction invalid", func(t *testing.T) {
   126  		withIterator(" 0.+", func(it *Iterator) {
   127  			err := it.SkipNumber()
   128  			require.IsType(t, InvalidDigitError{}, err)
   129  		})
   130  	})
   131  	t.Run("fraction with exponent", func(t *testing.T) {
   132  		withIterator(" -1.2e+3 ", func(it *Iterator) {
   133  			err := it.SkipNumber()
   134  			require.NoError(t, err)
   135  			_, err = it.NextValueType()
   136  			require.Equal(t, io.EOF, err)
   137  		})
   138  	})
   139  	t.Run("fraction eof", func(t *testing.T) {
   140  		withIterator(" -1.2", func(it *Iterator) {
   141  			err := it.SkipNumber()
   142  			require.NoError(t, err)
   143  			_, err = it.NextValueType()
   144  			require.Equal(t, io.EOF, err)
   145  		})
   146  	})
   147  	t.Run("fraction reader error", func(t *testing.T) {
   148  		withIterator("", func(it *Iterator) {
   149  			e := errors.New("test")
   150  			it.Reset(&stepByteReader{
   151  				b:   " 1.2",
   152  				err: e,
   153  			})
   154  			err := it.SkipNumber()
   155  			require.Equal(t, e, err)
   156  		})
   157  	})
   158  	t.Run("exponent empty", func(t *testing.T) {
   159  		withIterator(" 0e", func(it *Iterator) {
   160  			err := it.SkipNumber()
   161  			require.Equal(t, io.EOF, err)
   162  		})
   163  	})
   164  	t.Run("exponent eof after sign", func(t *testing.T) {
   165  		withIterator(" 0e+", func(it *Iterator) {
   166  			err := it.SkipNumber()
   167  			require.Equal(t, io.EOF, err)
   168  		})
   169  	})
   170  	t.Run("exponent invalid char after sign", func(t *testing.T) {
   171  		withIterator(" 0e++", func(it *Iterator) {
   172  			err := it.SkipNumber()
   173  			require.IsType(t, InvalidDigitError{}, err)
   174  		})
   175  	})
   176  	t.Run("exponent eof", func(t *testing.T) {
   177  		withIterator(" 0e+1", func(it *Iterator) {
   178  			err := it.SkipNumber()
   179  			require.NoError(t, err)
   180  		})
   181  	})
   182  	t.Run("exponent reader error", func(t *testing.T) {
   183  		withIterator("", func(it *Iterator) {
   184  			e := errors.New("test")
   185  			it.Reset(&stepByteReader{
   186  				b:   " 0e+1",
   187  				err: e,
   188  			})
   189  			err := it.SkipNumber()
   190  			require.Equal(t, e, err)
   191  		})
   192  	})
   193  }
   194  
   195  func TestIterator_Skip_SkipNumber_LargeNumber(t *testing.T) {
   196  	s := "-" + strings.Repeat("123", 20) + "." +
   197  		strings.Repeat("0456", 20) + "e+" +
   198  		strings.Repeat("789", 20)
   199  	withIterator(" "+s+" ", func(it *Iterator) {
   200  		err := it.SkipNumber()
   201  		require.NoError(t, err)
   202  	})
   203  }