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

     1  package jzon
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"strings"
     7  	"testing"
     8  	"testing/iotest"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestUnmarshalFromReader(t *testing.T) {
    14  	t.Run("success", func(t *testing.T) {
    15  		r := strings.NewReader("{}")
    16  		var i interface{}
    17  		err := UnmarshalFromReader(r, &i)
    18  		require.NoError(t, err)
    19  	})
    20  	t.Run("failure", func(t *testing.T) {
    21  		r := iotest.TimeoutReader(iotest.OneByteReader(strings.NewReader("{}")))
    22  		var i interface{}
    23  		err := UnmarshalFromReader(r, &i)
    24  		checkError(t, iotest.ErrTimeout, err)
    25  	})
    26  	t.Run("final error", func(t *testing.T) {
    27  		e := errors.New("test")
    28  		r := &stepByteReader{
    29  			b:   "{}",
    30  			err: e,
    31  		}
    32  		var i interface{}
    33  		err := UnmarshalFromReader(r, &i)
    34  		checkError(t, e, err)
    35  	})
    36  }
    37  
    38  func TestUnmarshalFromString(t *testing.T) {
    39  	t.Run("success", func(t *testing.T) {
    40  		s := "{}"
    41  		var i interface{}
    42  		err := UnmarshalFromString(s, &i)
    43  		require.NoError(t, err)
    44  	})
    45  	t.Run("failure", func(t *testing.T) {
    46  		s := "{"
    47  		var i interface{}
    48  		err := UnmarshalFromString(s, &i)
    49  		checkError(t, io.EOF, err)
    50  	})
    51  }