Ejemplo n.º 1
0
func TestGetExtensionStability(t *testing.T) {
	check := func(m *pb.MyMessage) bool {
		ext1, err := proto.GetExtension(m, pb.E_Ext_More)
		if err != nil {
			t.Fatalf("GetExtension() failed: %s", err)
		}
		ext2, err := proto.GetExtension(m, pb.E_Ext_More)
		if err != nil {
			t.Fatalf("GetExtension() failed: %s", err)
		}
		return ext1 == ext2
	}
	msg := &pb.MyMessage{Count: proto.Int32(4)}
	ext0 := &pb.Ext{}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext0); err != nil {
		t.Fatalf("Could not set ext1: %s", ext0)
	}
	if !check(msg) {
		t.Errorf("GetExtension() not stable before marshaling")
	}
	bb, err := proto.Marshal(msg)
	if err != nil {
		t.Fatalf("Marshal() failed: %s", err)
	}
	msg1 := &pb.MyMessage{}
	err = proto.Unmarshal(bb, msg1)
	if err != nil {
		t.Fatalf("Unmarshal() failed: %s", err)
	}
	if !check(msg1) {
		t.Errorf("GetExtension() not stable after unmarshaling")
	}
}
Ejemplo n.º 2
0
func ExampleCompile() {
	a := &test.NinOptNative{
		Field4: proto.Int64(1234),
		Field7: proto.Int32(123),
	}
	fp1, err := fieldpath.NewInt64Path("test", "NinOptNative", test.ThetestDescription(), "Field4")
	if err != nil {
		panic(err)
	}
	fp2, err := fieldpath.NewSint32Path("test", "NinOptNative", test.ThetestDescription(), "Field7")
	if err != nil {
		panic(err)
	}
	buf, err := proto.Marshal(a)
	if err != nil {
		panic(err)
	}
	u1 := fieldpath.NewInt64Unmarshaler(fp1, &handler64{})
	u2 := fieldpath.NewSint32Unmarshaler(fp2, &handler32{})
	c := fieldpath.Compile(u1, u2)
	err = c.Unmarshal(buf)
	if err != nil {
		panic(err)
	}
	// Output:
	// 1234
	// 123
}
Ejemplo n.º 3
0
func TestNilExtension(t *testing.T) {
	msg := &pb.MyMessage{
		Count: proto.Int32(1),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_Text, proto.String("hello")); err != nil {
		t.Fatal(err)
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, (*pb.Ext)(nil)); err == nil {
		t.Error("expected SetExtension to fail due to a nil extension")
	} else if want := "proto: SetExtension called with nil value of type *testdata.Ext"; err.Error() != want {
		t.Errorf("expected error %v, got %v", want, err)
	}
	// Note: if the behavior of Marshal is ever changed to ignore nil extensions, update
	// this test to verify that E_Ext_Text is properly propagated through marshal->unmarshal.
}
Ejemplo n.º 4
0
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package proto_test

import (
	"testing"

	"github.com/pixty/gogoprotobuf/proto"

	pb "github.com/pixty/gogoprotobuf/proto/testdata"
)

var cloneTestMessage = &pb.MyMessage{
	Count: proto.Int32(42),
	Name:  proto.String("Dave"),
	Pet:   []string{"bunny", "kitty", "horsey"},
	Inner: &pb.InnerMessage{
		Host:      proto.String("niles"),
		Port:      proto.Int32(9099),
		Connected: proto.Bool(true),
	},
	Others: []*pb.OtherMessage{
		{
			Value: []byte("some bytes"),
		},
	},
	Somegroup: &pb.MyMessage_SomeGroup{
		GroupField: proto.Int32(6),
	},
Ejemplo n.º 5
0
func TestInt32Int64Compatibility(t *testing.T) {

	//test nullable int32 and int64

	data1, err := testSize(&NinOptNative{
		Field3: proto.Int32(-1),
	}, "nullable", 11)
	if err != nil {
		t.Error(err)
	}
	//change marshaled data1 to unmarshal into 4th field which is an int64
	data1[0] = uint8(uint32(4 /*fieldNumber*/)<<3 | uint32(0 /*wireType*/))
	u1 := &NinOptNative{}
	err = proto.Unmarshal(data1, u1)
	if err != nil {
		t.Error(err)
	}
	if !u1.Equal(&NinOptNative{
		Field4: proto.Int64(-1),
	}) {
		t.Error("nullable unmarshaled int32 is not the same int64")
	}

	//test non-nullable int32 and int64

	data2, err := testSize(&NidOptNative{
		Field3: -1,
	}, "non nullable", 67)
	if err != nil {
		t.Error(err)
	}
	//change marshaled data2 to unmarshal into 4th field which is an int64
	field3 := uint8(uint32(3 /*fieldNumber*/)<<3 | uint32(0 /*wireType*/))
	field4 := uint8(uint32(4 /*fieldNumber*/)<<3 | uint32(0 /*wireType*/))
	for i, c := range data2 {
		if c == field4 {
			data2[i] = field3
		} else if c == field3 {
			data2[i] = field4
		}
	}
	u2 := &NidOptNative{}
	err = proto.Unmarshal(data2, u2)
	if err != nil {
		t.Error(err)
	}
	if !u2.Equal(&NidOptNative{
		Field4: -1,
	}) {
		t.Error("non nullable unmarshaled int32 is not the same int64")
	}

	//test repeated int32 and int64

	if _, err := testSize(&NinRepNative{
		Field3: []int32{-1},
	}, "repeated", 11); err != nil {
		t.Error(err)
	}

	//test packed repeated int32 and int64

	m4 := &NinRepPackedNative{
		Field3: []int32{-1},
	}
	data4, err := testSize(m4, "packed", 12)
	if err != nil {
		t.Error(err)
	}
	u4 := &NinRepPackedNative{}
	err = proto.Unmarshal(data4, u4)
	if err != nil {
		t.Error(err)
	}
	if err := u4.VerboseEqual(m4); err != nil {
		t.Fatalf("%#v", u4)
	}

	t.Logf("tested all")
}