github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_read_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestIterator_Read_Read(t *testing.T) {
    12  	t.Run("eof", func(t *testing.T) {
    13  		withIterator("", func(it *Iterator) {
    14  			_, err := it.Read()
    15  			require.Equal(t, io.EOF, err)
    16  		})
    17  	})
    18  	t.Run("string", func(t *testing.T) {
    19  		withIterator(`"test"`, func(it *Iterator) {
    20  			o, err := it.Read()
    21  			require.NoError(t, err)
    22  			require.Equal(t, "test", o)
    23  			_, err = it.NextValueType()
    24  			require.Equal(t, io.EOF, err)
    25  		})
    26  	})
    27  	t.Run("null", func(t *testing.T) {
    28  		withIterator(`null`, func(it *Iterator) {
    29  			o, err := it.Read()
    30  			require.NoError(t, err)
    31  			require.Equal(t, nil, o)
    32  			_, err = it.NextValueType()
    33  			require.Equal(t, io.EOF, err)
    34  		})
    35  	})
    36  	t.Run("true", func(t *testing.T) {
    37  		withIterator(`true`, func(it *Iterator) {
    38  			o, err := it.Read()
    39  			require.NoError(t, err)
    40  			require.Equal(t, true, o)
    41  			_, err = it.NextValueType()
    42  			require.Equal(t, io.EOF, err)
    43  		})
    44  	})
    45  	t.Run("false", func(t *testing.T) {
    46  		withIterator(`false`, func(it *Iterator) {
    47  			o, err := it.Read()
    48  			require.NoError(t, err)
    49  			require.Equal(t, false, o)
    50  			_, err = it.NextValueType()
    51  			require.Equal(t, io.EOF, err)
    52  		})
    53  	})
    54  	t.Run("number", func(t *testing.T) {
    55  		withIterator(`-123.456e7`, func(it *Iterator) {
    56  			o, err := it.Read()
    57  			require.NoError(t, err)
    58  			require.Equal(t, -123.456e7, o)
    59  			_, err = it.NextValueType()
    60  			require.Equal(t, io.EOF, err)
    61  		})
    62  	})
    63  	t.Run("invalid", func(t *testing.T) {
    64  		withIterator(`+`, func(it *Iterator) {
    65  			_, err := it.Read()
    66  			require.IsType(t, UnexpectedByteError{}, err)
    67  		})
    68  	})
    69  }
    70  
    71  func TestIterator_Read_ReadArray(t *testing.T) {
    72  	t.Run("eof after bracket", func(t *testing.T) {
    73  		withIterator(`[`, func(it *Iterator) {
    74  			_, err := it.Read()
    75  			require.Equal(t, io.EOF, err)
    76  		})
    77  	})
    78  	t.Run("empty array", func(t *testing.T) {
    79  		withIterator(`[]`, func(it *Iterator) {
    80  			o, err := it.Read()
    81  			require.NoError(t, err)
    82  			require.Equal(t, []interface{}{}, o)
    83  			_, err = it.NextValueType()
    84  			require.Equal(t, io.EOF, err)
    85  		})
    86  	})
    87  	t.Run("bad element", func(t *testing.T) {
    88  		withIterator(` [ + `, func(it *Iterator) {
    89  			_, err := it.Read()
    90  			require.IsType(t, UnexpectedByteError{}, err)
    91  		})
    92  	})
    93  	t.Run("eof after first element", func(t *testing.T) {
    94  		withIterator(` [ 1 `, func(it *Iterator) {
    95  			_, err := it.Read()
    96  			require.Equal(t, io.EOF, err)
    97  		})
    98  	})
    99  	t.Run("not nested", func(t *testing.T) {
   100  		withIterator(` [ null ] `, func(it *Iterator) {
   101  			o, err := it.Read()
   102  			require.NoError(t, err)
   103  			require.Equal(t, []interface{}{nil}, o)
   104  			_, err = it.NextValueType()
   105  			require.Equal(t, io.EOF, err)
   106  		})
   107  	})
   108  	t.Run("bad char after element", func(t *testing.T) {
   109  		withIterator(` [ 1 + `, func(it *Iterator) {
   110  			_, err := it.Read()
   111  			require.IsType(t, UnexpectedByteError{}, err)
   112  		})
   113  	})
   114  	t.Run("eof after comma", func(t *testing.T) {
   115  		withIterator(` [ 1 , `, func(it *Iterator) {
   116  			_, err := it.Read()
   117  			require.Equal(t, io.EOF, err)
   118  		})
   119  	})
   120  	t.Run("mix elements", func(t *testing.T) {
   121  		withIterator(` [ null , 1 , "a" , true , false ] `, func(it *Iterator) {
   122  			o, err := it.Read()
   123  			require.NoError(t, err)
   124  			require.Equal(t, []interface{}{nil, float64(1), "a", true, false}, o)
   125  			_, err = it.NextValueType()
   126  			require.Equal(t, io.EOF, err)
   127  		})
   128  	})
   129  	t.Run("nested eof", func(t *testing.T) {
   130  		withIterator(` [ [ `, func(it *Iterator) {
   131  			_, err := it.Read()
   132  			require.Equal(t, io.EOF, err)
   133  		})
   134  	})
   135  	t.Run("nested bad comma", func(t *testing.T) {
   136  		withIterator(` [ [ ] [ `, func(it *Iterator) {
   137  			_, err := it.Read()
   138  			require.IsType(t, UnexpectedByteError{}, err)
   139  		})
   140  	})
   141  	t.Run("nested eof after comma", func(t *testing.T) {
   142  		withIterator(` [ [ ] , `, func(it *Iterator) {
   143  			_, err := it.Read()
   144  			require.Equal(t, io.EOF, err)
   145  		})
   146  	})
   147  	t.Run("nested bad item", func(t *testing.T) {
   148  		withIterator(` [ [ ] , + `, func(it *Iterator) {
   149  			_, err := it.Read()
   150  			require.IsType(t, UnexpectedByteError{}, err)
   151  		})
   152  	})
   153  	t.Run("nested 1", func(t *testing.T) {
   154  		withIterator(` [ [ ] , null ] `, func(it *Iterator) {
   155  			o, err := it.Read()
   156  			require.NoError(t, err)
   157  			t.Log(o)
   158  			_, err = it.NextValueType()
   159  			require.Equal(t, io.EOF, err)
   160  		})
   161  	})
   162  	t.Run("nested 2", func(t *testing.T) {
   163  		s := nestedArray1(10)
   164  		withIterator(s, func(it *Iterator) {
   165  			o, err := it.Read()
   166  			require.NoError(t, err)
   167  			t.Log(o)
   168  			_, err = it.NextValueType()
   169  			require.Equal(t, io.EOF, err)
   170  		})
   171  	})
   172  	t.Run("nested 3", func(t *testing.T) {
   173  		s := nestedArray2(10)
   174  		withIterator(s, func(it *Iterator) {
   175  			o, err := it.Read()
   176  			require.NoError(t, err)
   177  			t.Log(o)
   178  			_, err = it.NextValueType()
   179  			require.Equal(t, io.EOF, err)
   180  		})
   181  	})
   182  	t.Run("nested with object", func(t *testing.T) {
   183  		s := nestedArrayWithObject(10)
   184  		withIterator(s, func(it *Iterator) {
   185  			o, err := it.Read()
   186  			require.NoError(t, err)
   187  			t.Log(o)
   188  			_, err = it.NextValueType()
   189  			require.Equal(t, io.EOF, err)
   190  		})
   191  	})
   192  }
   193  
   194  func TestIterator_Read_ReadObject(t *testing.T) {
   195  	t.Run("eof after bracket", func(t *testing.T) {
   196  		withIterator(`{`, func(it *Iterator) {
   197  			_, err := it.Read()
   198  			require.Equal(t, io.EOF, err)
   199  		})
   200  	})
   201  	t.Run("empty", func(t *testing.T) {
   202  		withIterator(` { } `, func(it *Iterator) {
   203  			o, err := it.Read()
   204  			require.NoError(t, err)
   205  			require.Equal(t, map[string]interface{}{}, o)
   206  			_, err = it.NextValueType()
   207  			require.Equal(t, io.EOF, err)
   208  		})
   209  	})
   210  	t.Run("invalid char after bracket", func(t *testing.T) {
   211  		withIterator(`{ ,`, func(it *Iterator) {
   212  			_, err := it.Read()
   213  			require.IsType(t, UnexpectedByteError{}, err)
   214  		})
   215  	})
   216  	t.Run("field eof", func(t *testing.T) {
   217  		withIterator(`{ " `, func(it *Iterator) {
   218  			_, err := it.Read()
   219  			require.Equal(t, io.EOF, err)
   220  		})
   221  	})
   222  	t.Run("eof after colon", func(t *testing.T) {
   223  		withIterator(`{ " " : `, func(it *Iterator) {
   224  			_, err := it.Read()
   225  			require.Equal(t, io.EOF, err)
   226  		})
   227  	})
   228  	t.Run("bad field value", func(t *testing.T) {
   229  		withIterator(`{ " " : + `, func(it *Iterator) {
   230  			_, err := it.Read()
   231  			require.IsType(t, UnexpectedByteError{}, err)
   232  		})
   233  	})
   234  	t.Run("eof after field value", func(t *testing.T) {
   235  		withIterator(`{ " " : 1 `, func(it *Iterator) {
   236  			_, err := it.Read()
   237  			require.Equal(t, io.EOF, err)
   238  		})
   239  	})
   240  	t.Run("one field", func(t *testing.T) {
   241  		withIterator(` { "k" : "v" } `, func(it *Iterator) {
   242  			o, err := it.Read()
   243  			require.NoError(t, err)
   244  			require.Equal(t, map[string]interface{}{"k": "v"}, o)
   245  			_, err = it.NextValueType()
   246  			require.Equal(t, io.EOF, err)
   247  		})
   248  	})
   249  	t.Run("bad char after field value", func(t *testing.T) {
   250  		withIterator(`{ " " : 1 { `, func(it *Iterator) {
   251  			_, err := it.Read()
   252  			require.IsType(t, UnexpectedByteError{}, err)
   253  		})
   254  	})
   255  	t.Run("eof after comma", func(t *testing.T) {
   256  		withIterator(`{ " " : 1 , `, func(it *Iterator) {
   257  			_, err := it.Read()
   258  			require.Equal(t, io.EOF, err)
   259  		})
   260  	})
   261  	t.Run("two fields", func(t *testing.T) {
   262  		withIterator(` { "k1" : "v1", "k2" : "v2" } `, func(it *Iterator) {
   263  			o, err := it.Read()
   264  			require.NoError(t, err)
   265  			require.Equal(t, map[string]interface{}{"k1": "v1", "k2": "v2"}, o)
   266  			_, err = it.NextValueType()
   267  			require.Equal(t, io.EOF, err)
   268  		})
   269  	})
   270  	t.Run("nested eof", func(t *testing.T) {
   271  		withIterator(` { "a" : { `, func(it *Iterator) {
   272  			_, err := it.Read()
   273  			require.Equal(t, io.EOF, err)
   274  		})
   275  	})
   276  	t.Run("nested bad comma", func(t *testing.T) {
   277  		withIterator(` { "a" : { } + `, func(it *Iterator) {
   278  			_, err := it.Read()
   279  			require.IsType(t, UnexpectedByteError{}, err)
   280  		})
   281  	})
   282  	t.Run("nested eof after comma", func(t *testing.T) {
   283  		withIterator(` { "a" : { } , `, func(it *Iterator) {
   284  			_, err := it.Read()
   285  			require.Equal(t, io.EOF, err)
   286  		})
   287  	})
   288  	t.Run("nested bad char after comma", func(t *testing.T) {
   289  		withIterator(` { "a" : { } , { `, func(it *Iterator) {
   290  			_, err := it.Read()
   291  			require.IsType(t, UnexpectedByteError{}, err)
   292  		})
   293  	})
   294  	t.Run("nested bad field", func(t *testing.T) {
   295  		withIterator(` { "a" : { } , " `, func(it *Iterator) {
   296  			_, err := it.Read()
   297  			require.Equal(t, io.EOF, err)
   298  		})
   299  	})
   300  	t.Run("nested eof after field", func(t *testing.T) {
   301  		withIterator(` { "a" : { } , "b" : `, func(it *Iterator) {
   302  			_, err := it.Read()
   303  			require.Equal(t, io.EOF, err)
   304  		})
   305  	})
   306  	t.Run("nested bad value", func(t *testing.T) {
   307  		withIterator(` { "a" : { } , "b" : } `, func(it *Iterator) {
   308  			_, err := it.Read()
   309  			require.IsType(t, UnexpectedByteError{}, err)
   310  		})
   311  	})
   312  	t.Run("nested 1", func(t *testing.T) {
   313  		withIterator(` { "a" : { } , "b" : null } `, func(it *Iterator) {
   314  			o, err := it.Read()
   315  			require.NoError(t, err)
   316  			t.Log(o)
   317  			_, err = it.NextValueType()
   318  			require.Equal(t, io.EOF, err)
   319  		})
   320  	})
   321  	t.Run("nested 2", func(t *testing.T) {
   322  		s := nestedObject(10)
   323  		withIterator(s, func(it *Iterator) {
   324  			o, err := it.Read()
   325  			require.NoError(t, err)
   326  			t.Log(o)
   327  			_, err = it.NextValueType()
   328  			require.Equal(t, io.EOF, err)
   329  		})
   330  	})
   331  	t.Run("nested with array", func(t *testing.T) {
   332  		s := nestedObjectWithArray(10)
   333  		withIterator(s, func(it *Iterator) {
   334  			o, err := it.Read()
   335  			require.NoError(t, err)
   336  			t.Log(o)
   337  			_, err = it.NextValueType()
   338  			require.Equal(t, io.EOF, err)
   339  		})
   340  	})
   341  }
   342  
   343  func TestIterator_Read_UseNumber(t *testing.T) {
   344  	decCfg := NewDecoderConfig(&DecoderOption{
   345  		UseNumber: true,
   346  	})
   347  	it := decCfg.NewIterator()
   348  	defer it.Release()
   349  	it.ResetBytes([]byte(" 123 "))
   350  	o, err := it.Read()
   351  	require.NoError(t, err)
   352  	require.Equal(t, json.Number("123"), o)
   353  	t.Log(o)
   354  	_, err = it.NextValueType()
   355  	require.Equal(t, io.EOF, err)
   356  }