github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/wellknown_test.go (about)

     1  package amino_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gnolang/gno/tm2/pkg/amino"
     7  	"github.com/gnolang/gno/tm2/pkg/amino/tests"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestAnyWellKnownNative(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	cdc := amino.NewCodec()
    15  
    16  	s1 := tests.InterfaceFieldsStruct{
    17  		F3: string("dontcare"),
    18  		F4: int(0),
    19  	}
    20  
    21  	bz, err := cdc.Marshal(s1)
    22  	assert.Nil(t, err)
    23  	assert.Equal(t,
    24  		//     0x1a --> field #3 Typ3ByteLength (F3)
    25  		//           0x2a --> length prefix (42 bytes)
    26  		//                 0x0a --> field #1 Typ3ByteLength (Any TypeURL)
    27  		//                       0x1c --> length prefix (28 bytes)
    28  		//                             0x2f, ... 0x65 --> "/google.protobuf.StringValue"
    29  		[]byte{
    30  			0x1a, 0x2a, 0x0a, 0x1c, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56, 0x61, 0x6c, 0x75, 0x65,
    31  			//   0x12 --> field #2 Typ3ByteLength (Any Value)
    32  			//         0x0a --> length prefix (10 bytes)
    33  			//               0x0a --> field #1, one and only, of implicit struct.
    34  			//                     0x08 --> length prefix (8 bytes)
    35  			//                           0x64, ... 0x65 --> "dontcare"
    36  			/**/ 0x12, 0x0a, 0x0a, 0x08, 0x64, 0x6f, 0x6e, 0x74, 0x63, 0x61, 0x72, 0x65,
    37  			//   0x22 --> field #4 Typ3ByteLength (F4)
    38  			//         0x1d --> length prefix (29 bytes)
    39  			//               0x0a --> field #1 Typ3ByteLength (Any TypeURL)
    40  			//                     0x1b --> length prefix (27 bytes)
    41  			//                           0x2f, ... 0x65 --> "/google.protobuf.Int64Value"
    42  			/**/ 0x22, 0x1d, 0x0a, 0x1b, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x49, 0x6e, 0x74, 0x36, 0x34, 0x56, 0x61, 0x6c, 0x75, 0x65,
    43  		},
    44  		bz,
    45  		"InterfaceFieldsStruct incorrectly serialized")
    46  
    47  	var s2 tests.InterfaceFieldsStruct
    48  	err = cdc.Unmarshal(bz, &s2)
    49  	assert.NoError(t, err)
    50  
    51  	s3 := tests.InterfaceFieldsStruct{
    52  		F3: string("dontcare"),
    53  		F4: int64(0), // ints get decoded as int64.
    54  	}
    55  	assert.Equal(t, s3, s2)
    56  }