github.com/cosmos/cosmos-proto@v1.0.0-beta.3/anyutil/any_test.go (about)

     1  package anyutil_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/stretchr/testify/require"
     8  	"google.golang.org/protobuf/proto"
     9  	"google.golang.org/protobuf/reflect/protoregistry"
    10  	"google.golang.org/protobuf/testing/protocmp"
    11  	"google.golang.org/protobuf/types/known/anypb"
    12  
    13  	"github.com/cosmos/cosmos-proto/anyutil"
    14  	"github.com/cosmos/cosmos-proto/testpb"
    15  )
    16  
    17  func TestAny(t *testing.T) {
    18  	value := &testpb.A{SomeBoolean: true}
    19  
    20  	dst1 := &anypb.Any{}
    21  	err := anyutil.MarshalFrom(dst1, value, proto.MarshalOptions{})
    22  	require.NoError(t, err)
    23  	require.Equal(t, "/A", dst1.TypeUrl) // Make sure there's no "type.googleapis.com/" prefix.
    24  
    25  	dst2, err := anyutil.New(value)
    26  	require.NoError(t, err)
    27  	require.Equal(t, "/A", dst2.TypeUrl) // Make sure there's no "type.googleapis.com/" prefix.
    28  
    29  	// Round trip.
    30  	newValue, err := anypb.UnmarshalNew(dst2, proto.UnmarshalOptions{})
    31  	require.NoError(t, err)
    32  	diff := cmp.Diff(value, newValue, protocmp.Transform())
    33  	require.Empty(t, diff)
    34  }
    35  
    36  func TestUnpack(t *testing.T) {
    37  	value := &testpb.A{SomeBoolean: true}
    38  	any, err := anyutil.New(value)
    39  	require.NoError(t, err)
    40  
    41  	msg, err := anyutil.Unpack(any, nil, nil)
    42  	require.NoError(t, err)
    43  	diff := cmp.Diff(value, msg, protocmp.Transform())
    44  	require.Empty(t, diff)
    45  
    46  	// Test the same thing with using the dynamicpb path.
    47  	msg, err = anyutil.Unpack(any, protoregistry.GlobalFiles, &protoregistry.Types{})
    48  	require.NoError(t, err)
    49  	diff = cmp.Diff(value, msg, protocmp.Transform())
    50  	require.Empty(t, diff)
    51  }