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

     1  package jzon
     2  
     3  import (
     4  	"io"
     5  	"reflect"
     6  	"runtime"
     7  	"testing"
     8  )
     9  
    10  var (
    11  	runtimeErrorType = reflect.TypeOf((*runtime.Error)(nil)).Elem()
    12  )
    13  
    14  func skipTest(t *testing.T, fmt string, args ...interface{}) {
    15  	t.Skipf(fmt, args...)
    16  }
    17  
    18  func withIterator(data string, cb func(it *Iterator)) {
    19  	it := NewIterator()
    20  	defer it.Release()
    21  	if data != "" {
    22  		it.ResetBytes(localStringToBytes(data))
    23  	}
    24  	cb(it)
    25  }
    26  
    27  type stepByteReader struct {
    28  	b    string
    29  	step int
    30  	err  error
    31  }
    32  
    33  var _ io.Reader = &stepByteReader{}
    34  
    35  func (o *stepByteReader) Read(p []byte) (n int, err error) {
    36  	if len(p) == 0 {
    37  		return 0, nil
    38  	}
    39  	if len(o.b) == 0 {
    40  		if o.err != nil {
    41  			return 0, o.err
    42  		}
    43  		return 0, io.EOF
    44  	}
    45  	step := o.step
    46  	if step == 0 {
    47  		step = 1
    48  	} else {
    49  		if len(p) < step {
    50  			step = len(p)
    51  		}
    52  	}
    53  	n = copy(p[:step], o.b)
    54  	o.b = o.b[n:]
    55  	return n, nil
    56  }
    57  
    58  type repeatByteReader struct {
    59  	b     byte
    60  	count int
    61  }
    62  
    63  var _ io.Reader = &repeatByteReader{}
    64  
    65  func (r *repeatByteReader) Read(p []byte) (n int, err error) {
    66  	l := len(p)
    67  	for r.count > 0 && n < l {
    68  		p[n] = r.b
    69  		r.count--
    70  		n++
    71  	}
    72  	return
    73  }