github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/level_1/interface_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/stretchr/testify/require"
     6  	"io"
     7  	"github.com/v2pro/plz/msgfmt/jsonfmt"
     8  	"github.com/v2pro/plz/reflect2"
     9  )
    10  
    11  func Test_slice_of_eface(t *testing.T) {
    12  	should := require.New(t)
    13  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(([]interface{})(nil)))
    14  	should.Equal("[1,null,3]", string(encoder.Encode(nil,nil, reflect2.PtrOf([]interface{}{
    15  		1, nil, 3,
    16  	}))))
    17  }
    18  
    19  type TestCloser int
    20  
    21  func (closer TestCloser) Close() error {
    22  	return nil
    23  }
    24  
    25  func Test_slice_of_iface(t *testing.T) {
    26  	should := require.New(t)
    27  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(([]io.Closer)(nil)))
    28  	should.Equal("[1,null,3]", string(encoder.Encode(nil,nil, reflect2.PtrOf([]io.Closer{
    29  		TestCloser(1), nil, TestCloser(3),
    30  	}))))
    31  }