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

     1  package jzon
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestIterator_New(t *testing.T) {
    14  	must := require.New(t)
    15  
    16  	it := NewIterator()
    17  	must.Nil(it.reader)
    18  	must.Nil(it.buffer)
    19  	must.Equal(0, it.offset)
    20  	must.Equal(0, it.head)
    21  	must.Equal(0, it.tail)
    22  	// must.Nil(it.Error)
    23  }
    24  
    25  func TestIterator_Reset_Nil(t *testing.T) {
    26  	must := require.New(t)
    27  	it := NewIterator()
    28  	must.Nil(it.reader)
    29  	must.Nil(it.buffer)
    30  	must.Equal(0, it.offset)
    31  	must.Equal(0, it.head)
    32  	must.Equal(0, it.tail)
    33  
    34  	it.Reset(nil)
    35  	must.Nil(it.reader)
    36  	must.Nil(it.buffer)
    37  	must.Equal(0, it.offset)
    38  	must.Equal(0, it.head)
    39  	must.Equal(0, it.tail)
    40  }
    41  
    42  func TestIterator_Reset(t *testing.T) {
    43  	must := require.New(t)
    44  
    45  	// nil -> reader
    46  	it := NewIterator()
    47  	r := bytes.NewReader(nil)
    48  	it.Reset(r)
    49  	must.Equal(r, it.reader)
    50  	must.NotEmpty(it.buffer)
    51  	must.Equal(0, it.head)
    52  	must.Equal(0, it.tail)
    53  
    54  	// reader -> reader
    55  	addr := &it.buffer[0]
    56  	r2 := bytes.NewReader(nil)
    57  	it.Reset(r2)
    58  	must.Equal(r2, it.reader)
    59  	must.True(addr == &it.buffer[0])
    60  	must.Equal(0, it.head)
    61  	must.Equal(0, it.tail)
    62  
    63  	// reader -> byte
    64  	b := []byte("abc")
    65  	it.ResetBytes(b)
    66  	must.Nil(it.reader)
    67  	must.True(&b[0] == &it.buffer[0])
    68  	must.Equal(0, it.head)
    69  	must.Equal(len(b), it.tail)
    70  
    71  	// nil -> byte
    72  	it = NewIterator()
    73  	b2 := []byte("abc")
    74  	it.ResetBytes(b2)
    75  	must.Nil(it.reader)
    76  	must.True(&b2[0] == &it.buffer[0])
    77  	must.Equal(0, it.head)
    78  	must.Equal(len(b2), it.tail)
    79  
    80  	// byte -> byte
    81  	b3 := []byte("defg")
    82  	it.ResetBytes(b3)
    83  	must.Nil(it.reader)
    84  	must.True(&b3[0] == &it.buffer[0])
    85  	must.Equal(0, it.head)
    86  	must.Equal(len(b3), it.tail)
    87  
    88  	// byte -> reader
    89  	r3 := bytes.NewReader(nil)
    90  	it.Reset(r3)
    91  	must.Equal(r3, it.reader)
    92  	must.Equal(0, it.head)
    93  	must.Equal(0, it.tail)
    94  }
    95  
    96  func TestIterator_NextValueType(t *testing.T) {
    97  	must := require.New(t)
    98  	it := NewIterator()
    99  	for c, typ := range valueTypeMap {
   100  		it.ResetBytes([]byte{byte(c)})
   101  		next, err := it.NextValueType()
   102  		if typ == WhiteSpaceValue {
   103  			require.Equal(t, io.EOF, err)
   104  		} else {
   105  			require.NoError(t, err)
   106  			must.Equal(typ, next)
   107  		}
   108  	}
   109  }
   110  
   111  func TestIterator_WrapError(t *testing.T) {
   112  	must := require.New(t)
   113  	it := NewIterator()
   114  	s := strings.Repeat(" ", errWidth+1)
   115  	it.ResetBytes([]byte(s))
   116  	ex := errors.New("test")
   117  	err := it.WrapError(ex)
   118  	err2 := it.WrapError(err)
   119  	must.Equal(err, err2)
   120  }