github.com/urso/go-structform@v0.0.2/ubjson/ubjson_test.go (about)

     1  package ubjson
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/urso/go-structform"
     9  	"github.com/urso/go-structform/sftest"
    10  )
    11  
    12  func TestEncParseConsistent(t *testing.T) {
    13  	testEncParseConsistent(t, Parse)
    14  }
    15  
    16  func TestEncDecoderConsistent(t *testing.T) {
    17  	testEncParseConsistent(t, func(content []byte, to structform.Visitor) error {
    18  		dec := NewBytesDecoder(content, to)
    19  		err := dec.Next()
    20  		if err == io.EOF {
    21  			err = nil
    22  		}
    23  		return err
    24  	})
    25  }
    26  
    27  func TestEncParseBytesConsistent(t *testing.T) {
    28  	testEncParseConsistent(t, func(content []byte, to structform.Visitor) error {
    29  		p := NewParser(to)
    30  		for _, b := range content {
    31  			err := p.feed([]byte{b})
    32  			if err != nil {
    33  				return err
    34  			}
    35  		}
    36  		return p.finalize()
    37  	})
    38  }
    39  
    40  func testEncParseConsistent(
    41  	t *testing.T,
    42  	parse func([]byte, structform.Visitor) error,
    43  ) {
    44  	sftest.TestEncodeParseConsistent(t, sftest.Samples,
    45  		func() (structform.Visitor, func(structform.Visitor) error) {
    46  			buf := bytes.NewBuffer(nil)
    47  			vs := NewVisitor(buf)
    48  
    49  			return vs, func(to structform.Visitor) error {
    50  				return parse(buf.Bytes(), to)
    51  			}
    52  		})
    53  }