github.com/cosmos/cosmos-proto@v1.0.0-beta.3/internal/testprotos/test3/fuzz_test.go (about)

     1  package test3
     2  
     3  import (
     4  	"github.com/cosmos/cosmos-proto/internal/fuzz"
     5  	"github.com/google/go-cmp/cmp"
     6  	"github.com/stretchr/testify/require"
     7  	"google.golang.org/protobuf/proto"
     8  	"google.golang.org/protobuf/testing/protocmp"
     9  	"pgregory.net/rapid"
    10  	"testing"
    11  )
    12  
    13  func TestMarshalUnmarshal(t *testing.T) {
    14  	t.Run("marshal unmarshal", rapid.MakeCheck(func(t *rapid.T) {
    15  		mType := (&TestAllTypes{}).ProtoReflect().Type()
    16  		msg := fuzz.Message(t, mType)
    17  
    18  		msgBytes, err := proto.MarshalOptions{Deterministic: true}.Marshal(msg.Interface())
    19  		require.NoError(t, err)
    20  
    21  		uMsg := mType.New()
    22  		err = proto.UnmarshalOptions{}.Unmarshal(msgBytes, uMsg.Interface())
    23  		require.NoError(t, err)
    24  		cmpOpt := protocmp.Transform()
    25  		diff := cmp.Diff(uMsg.Interface(), msg.Interface(), cmpOpt)
    26  		require.Emptyf(t, diff, "non matching messages\n%s", diff)
    27  	}))
    28  }
    29  
    30  // TestZeroValueOneofIsMarshalled tests that zero values in oneofs are marshalled
    31  func TestZeroValueOneofIsMarshalled(t *testing.T) {
    32  	msg1 := &TestAllTypes{OneofField: &TestAllTypes_OneofEnum{OneofEnum: TestAllTypes_FOO}}
    33  	b, err := proto.Marshal(msg1)
    34  	require.NoError(t, err)
    35  
    36  	msg2 := &TestAllTypes{}
    37  	require.NoError(t, proto.Unmarshal(b, msg2))
    38  
    39  	require.True(t, msg2.ProtoReflect().Has(fd_TestAllTypes_oneof_enum))
    40  }