MCPcopy
hub / github.com/golang/protobuf / TestClone

Function TestClone

proto/proto_clone_test.go:51–88  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

49}
50
51func TestClone(t *testing.T) {
52 // Create a clone using a marshal/unmarshal roundtrip.
53 vanilla := new(pb2.MyMessage)
54 b, err := proto.Marshal(cloneTestMessage)
55 if err != nil {
56 t.Errorf("unexpected Marshal error: %v", err)
57 }
58 if err := proto.Unmarshal(b, vanilla); err != nil {
59 t.Errorf("unexpected Unarshal error: %v", err)
60 }
61
62 // Create a clone using Clone and verify that it is equal to the original.
63 m := proto.Clone(cloneTestMessage).(*pb2.MyMessage)
64 if !proto.Equal(m, cloneTestMessage) {
65 t.Fatalf("Clone(%v) = %v", cloneTestMessage, m)
66 }
67
68 // Mutate the clone, which should not affect the original.
69 x1, err := proto.GetExtension(m, pb2.E_Ext_More)
70 if err != nil {
71 t.Errorf("unexpected GetExtension(%v) error: %v", pb2.E_Ext_More.Name, err)
72 }
73 x2, err := proto.GetExtension(m, pb2.E_Ext_Text)
74 if err != nil {
75 t.Errorf("unexpected GetExtension(%v) error: %v", pb2.E_Ext_Text.Name, err)
76 }
77 x3, err := proto.GetExtension(m, pb2.E_Greeting)
78 if err != nil {
79 t.Errorf("unexpected GetExtension(%v) error: %v", pb2.E_Greeting.Name, err)
80 }
81 *m.Inner.Port++
82 *(x1.(*pb2.Ext)).Data = "blah blah"
83 *(x2.(*string)) = "goodbye"
84 x3.([]string)[0] = "zero"
85 if !proto.Equal(cloneTestMessage, vanilla) {
86 t.Fatalf("mutation on original detected:\ngot %v\nwant %v", cloneTestMessage, vanilla)
87 }
88}
89
90func TestCloneNil(t *testing.T) {
91 var m *pb2.MyMessage

Callers

nothing calls this directly

Calls 5

MarshalFunction · 0.92
UnmarshalFunction · 0.92
CloneFunction · 0.92
EqualFunction · 0.92
GetExtensionFunction · 0.92

Tested by

no test coverage detected