Created
June 2, 2018 14:13
-
-
Save liamsi/6ddd3e824c351799905f0c0a7950aecf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build extensive_tests | |
// only built if manually enforced | |
package proto3 | |
import ( | |
"testing" | |
"github.com/golang/protobuf/proto" | |
pbf "github.com/golang/protobuf/proto/proto3_proto" | |
"github.com/stretchr/testify/assert" | |
"github.com/tendermint/go-amino" | |
) | |
// This file checks basic proto3 compatibility by checking encoding of some test-vectors generated by | |
// TODO(ismail): add a semi-automatic way to test for (more or less full) compatibility to proto3, ideally, | |
// using their .proto test files: https://github.com/golang/protobuf/tree/master/proto | |
// List of differences: | |
// panic: floating point types are unsafe for go-amino | |
func TestEncodeAminoDecodeProto(t *testing.T) { | |
cdc := amino.NewCodec() | |
// we have to define our own struct for amino enc because the proto3 test files contains floating types | |
type Msg struct { | |
Name string | |
Hilarity pbf.Message_Humour | |
} | |
m := pbf.Message{Name: "Cosmos"} | |
ab, err := cdc.MarshalBinaryBare(Msg{Name: "Cosmos"}) | |
assert.NoError(t, err, "unexpected error") | |
pb, err := proto.Marshal(&m) | |
assert.NoError(t, err, "unexpected error") | |
// This works: | |
assert.Equal(t, pb, ab, "encoding doesn't match") | |
m = pbf.Message{Name: "Cosmos", Hilarity: pbf.Message_PUNS} | |
ab, err = cdc.MarshalBinaryBare(Msg{Name: "Cosmos", Hilarity: pbf.Message_PUNS}) | |
assert.NoError(t, err, "unexpected error") | |
pb, err = proto.Marshal(&m) | |
assert.NoError(t, err, "unexpected error") | |
// This does not work (same if we drop Name and only have the int32 field): | |
assert.Equal(t, pb, ab, "encoding doesn't match") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment