mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	Dep helper (#2151)
* Add dep task to update go dependencies * Update go dependencies
This commit is contained in:
		
				
					committed by
					
						 Miek Gieben
						Miek Gieben
					
				
			
			
				
	
			
			
			
						parent
						
							8f8b81f56b
						
					
				
				
					commit
					0e8977761d
				
			
							
								
								
									
										2278
									
								
								vendor/github.com/gogo/protobuf/proto/all_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										2278
									
								
								vendor/github.com/gogo/protobuf/proto/all_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										300
									
								
								vendor/github.com/gogo/protobuf/proto/any_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										300
									
								
								vendor/github.com/gogo/protobuf/proto/any_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,300 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2016 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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 ( | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/gogo/protobuf/proto" | ||||
|  | ||||
| 	pb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| 	testpb "github.com/gogo/protobuf/proto/testdata" | ||||
| 	"github.com/gogo/protobuf/types" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	expandedMarshaler        = proto.TextMarshaler{ExpandAny: true} | ||||
| 	expandedCompactMarshaler = proto.TextMarshaler{Compact: true, ExpandAny: true} | ||||
| ) | ||||
|  | ||||
| // anyEqual reports whether two messages which may be google.protobuf.Any or may | ||||
| // contain google.protobuf.Any fields are equal. We can't use proto.Equal for | ||||
| // comparison, because semantically equivalent messages may be marshaled to | ||||
| // binary in different tag order. Instead, trust that TextMarshaler with | ||||
| // ExpandAny option works and compare the text marshaling results. | ||||
| func anyEqual(got, want proto.Message) bool { | ||||
| 	// if messages are proto.Equal, no need to marshal. | ||||
| 	if proto.Equal(got, want) { | ||||
| 		return true | ||||
| 	} | ||||
| 	g := expandedMarshaler.Text(got) | ||||
| 	w := expandedMarshaler.Text(want) | ||||
| 	return g == w | ||||
| } | ||||
|  | ||||
| type golden struct { | ||||
| 	m    proto.Message | ||||
| 	t, c string | ||||
| } | ||||
|  | ||||
| var goldenMessages = makeGolden() | ||||
|  | ||||
| func makeGolden() []golden { | ||||
| 	nested := &pb.Nested{Bunny: "Monty"} | ||||
| 	nb, err := proto.Marshal(nested) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	m1 := &pb.Message{ | ||||
| 		Name:        "David", | ||||
| 		ResultCount: 47, | ||||
| 		Anything:    &types.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(nested), Value: nb}, | ||||
| 	} | ||||
| 	m2 := &pb.Message{ | ||||
| 		Name:        "David", | ||||
| 		ResultCount: 47, | ||||
| 		Anything:    &types.Any{TypeUrl: "http://[::1]/type.googleapis.com/" + proto.MessageName(nested), Value: nb}, | ||||
| 	} | ||||
| 	m3 := &pb.Message{ | ||||
| 		Name:        "David", | ||||
| 		ResultCount: 47, | ||||
| 		Anything:    &types.Any{TypeUrl: `type.googleapis.com/"/` + proto.MessageName(nested), Value: nb}, | ||||
| 	} | ||||
| 	m4 := &pb.Message{ | ||||
| 		Name:        "David", | ||||
| 		ResultCount: 47, | ||||
| 		Anything:    &types.Any{TypeUrl: "type.googleapis.com/a/path/" + proto.MessageName(nested), Value: nb}, | ||||
| 	} | ||||
| 	m5 := &types.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(nested), Value: nb} | ||||
|  | ||||
| 	any1 := &testpb.MyMessage{Count: proto.Int32(47), Name: proto.String("David")} | ||||
| 	proto.SetExtension(any1, testpb.E_Ext_More, &testpb.Ext{Data: proto.String("foo")}) | ||||
| 	proto.SetExtension(any1, testpb.E_Ext_Text, proto.String("bar")) | ||||
| 	any1b, err := proto.Marshal(any1) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	any2 := &testpb.MyMessage{Count: proto.Int32(42), Bikeshed: testpb.MyMessage_GREEN.Enum(), RepBytes: [][]byte{[]byte("roboto")}} | ||||
| 	proto.SetExtension(any2, testpb.E_Ext_More, &testpb.Ext{Data: proto.String("baz")}) | ||||
| 	any2b, err := proto.Marshal(any2) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	m6 := &pb.Message{ | ||||
| 		Name:        "David", | ||||
| 		ResultCount: 47, | ||||
| 		Anything:    &types.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(any1), Value: any1b}, | ||||
| 		ManyThings: []*types.Any{ | ||||
| 			{TypeUrl: "type.googleapis.com/" + proto.MessageName(any2), Value: any2b}, | ||||
| 			{TypeUrl: "type.googleapis.com/" + proto.MessageName(any1), Value: any1b}, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	const ( | ||||
| 		m1Golden = ` | ||||
| name: "David" | ||||
| result_count: 47 | ||||
| anything: < | ||||
|   [type.googleapis.com/proto3_proto.Nested]: < | ||||
|     bunny: "Monty" | ||||
|   > | ||||
| > | ||||
| ` | ||||
| 		m2Golden = ` | ||||
| name: "David" | ||||
| result_count: 47 | ||||
| anything: < | ||||
|   ["http://[::1]/type.googleapis.com/proto3_proto.Nested"]: < | ||||
|     bunny: "Monty" | ||||
|   > | ||||
| > | ||||
| ` | ||||
| 		m3Golden = ` | ||||
| name: "David" | ||||
| result_count: 47 | ||||
| anything: < | ||||
|   ["type.googleapis.com/\"/proto3_proto.Nested"]: < | ||||
|     bunny: "Monty" | ||||
|   > | ||||
| > | ||||
| ` | ||||
| 		m4Golden = ` | ||||
| name: "David" | ||||
| result_count: 47 | ||||
| anything: < | ||||
|   [type.googleapis.com/a/path/proto3_proto.Nested]: < | ||||
|     bunny: "Monty" | ||||
|   > | ||||
| > | ||||
| ` | ||||
| 		m5Golden = ` | ||||
| [type.googleapis.com/proto3_proto.Nested]: < | ||||
|   bunny: "Monty" | ||||
| > | ||||
| ` | ||||
| 		m6Golden = ` | ||||
| name: "David" | ||||
| result_count: 47 | ||||
| anything: < | ||||
|   [type.googleapis.com/testdata.MyMessage]: < | ||||
|     count: 47 | ||||
|     name: "David" | ||||
|     [testdata.Ext.more]: < | ||||
|       data: "foo" | ||||
|     > | ||||
|     [testdata.Ext.text]: "bar" | ||||
|   > | ||||
| > | ||||
| many_things: < | ||||
|   [type.googleapis.com/testdata.MyMessage]: < | ||||
|     count: 42 | ||||
|     bikeshed: GREEN | ||||
|     rep_bytes: "roboto" | ||||
|     [testdata.Ext.more]: < | ||||
|       data: "baz" | ||||
|     > | ||||
|   > | ||||
| > | ||||
| many_things: < | ||||
|   [type.googleapis.com/testdata.MyMessage]: < | ||||
|     count: 47 | ||||
|     name: "David" | ||||
|     [testdata.Ext.more]: < | ||||
|       data: "foo" | ||||
|     > | ||||
|     [testdata.Ext.text]: "bar" | ||||
|   > | ||||
| > | ||||
| ` | ||||
| 	) | ||||
| 	return []golden{ | ||||
| 		{m1, strings.TrimSpace(m1Golden) + "\n", strings.TrimSpace(compact(m1Golden)) + " "}, | ||||
| 		{m2, strings.TrimSpace(m2Golden) + "\n", strings.TrimSpace(compact(m2Golden)) + " "}, | ||||
| 		{m3, strings.TrimSpace(m3Golden) + "\n", strings.TrimSpace(compact(m3Golden)) + " "}, | ||||
| 		{m4, strings.TrimSpace(m4Golden) + "\n", strings.TrimSpace(compact(m4Golden)) + " "}, | ||||
| 		{m5, strings.TrimSpace(m5Golden) + "\n", strings.TrimSpace(compact(m5Golden)) + " "}, | ||||
| 		{m6, strings.TrimSpace(m6Golden) + "\n", strings.TrimSpace(compact(m6Golden)) + " "}, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMarshalGolden(t *testing.T) { | ||||
| 	for _, tt := range goldenMessages { | ||||
| 		if got, want := expandedMarshaler.Text(tt.m), tt.t; got != want { | ||||
| 			t.Errorf("message %v: got:\n%s\nwant:\n%s", tt.m, got, want) | ||||
| 		} | ||||
| 		if got, want := expandedCompactMarshaler.Text(tt.m), tt.c; got != want { | ||||
| 			t.Errorf("message %v: got:\n`%s`\nwant:\n`%s`", tt.m, got, want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestUnmarshalGolden(t *testing.T) { | ||||
| 	for _, tt := range goldenMessages { | ||||
| 		want := tt.m | ||||
| 		got := proto.Clone(tt.m) | ||||
| 		got.Reset() | ||||
| 		if err := proto.UnmarshalText(tt.t, got); err != nil { | ||||
| 			t.Errorf("failed to unmarshal\n%s\nerror: %v", tt.t, err) | ||||
| 		} | ||||
| 		if !anyEqual(got, want) { | ||||
| 			t.Errorf("message:\n%s\ngot:\n%s\nwant:\n%s", tt.t, got, want) | ||||
| 		} | ||||
| 		got.Reset() | ||||
| 		if err := proto.UnmarshalText(tt.c, got); err != nil { | ||||
| 			t.Errorf("failed to unmarshal\n%s\nerror: %v", tt.c, err) | ||||
| 		} | ||||
| 		if !anyEqual(got, want) { | ||||
| 			t.Errorf("message:\n%s\ngot:\n%s\nwant:\n%s", tt.c, got, want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMarshalUnknownAny(t *testing.T) { | ||||
| 	m := &pb.Message{ | ||||
| 		Anything: &types.Any{ | ||||
| 			TypeUrl: "foo", | ||||
| 			Value:   []byte("bar"), | ||||
| 		}, | ||||
| 	} | ||||
| 	want := `anything: < | ||||
|   type_url: "foo" | ||||
|   value: "bar" | ||||
| > | ||||
| ` | ||||
| 	got := expandedMarshaler.Text(m) | ||||
| 	if got != want { | ||||
| 		t.Errorf("got\n`%s`\nwant\n`%s`", got, want) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestAmbiguousAny(t *testing.T) { | ||||
| 	pb := &types.Any{} | ||||
| 	err := proto.UnmarshalText(` | ||||
| 	type_url: "ttt/proto3_proto.Nested" | ||||
| 	value: "\n\x05Monty" | ||||
| 	`, pb) | ||||
| 	t.Logf("result: %v (error: %v)", expandedMarshaler.Text(pb), err) | ||||
| 	if err != nil { | ||||
| 		t.Errorf("failed to parse ambiguous Any message: %v", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestUnmarshalOverwriteAny(t *testing.T) { | ||||
| 	pb := &types.Any{} | ||||
| 	err := proto.UnmarshalText(` | ||||
|   [type.googleapis.com/a/path/proto3_proto.Nested]: < | ||||
|     bunny: "Monty" | ||||
|   > | ||||
|   [type.googleapis.com/a/path/proto3_proto.Nested]: < | ||||
|     bunny: "Rabbit of Caerbannog" | ||||
|   > | ||||
| 	`, pb) | ||||
| 	want := `line 7: Any message unpacked multiple times, or "type_url" already set` | ||||
| 	if err.Error() != want { | ||||
| 		t.Errorf("incorrect error.\nHave: %v\nWant: %v", err.Error(), want) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestUnmarshalAnyMixAndMatch(t *testing.T) { | ||||
| 	pb := &types.Any{} | ||||
| 	err := proto.UnmarshalText(` | ||||
| 	value: "\n\x05Monty" | ||||
|   [type.googleapis.com/a/path/proto3_proto.Nested]: < | ||||
|     bunny: "Rabbit of Caerbannog" | ||||
|   > | ||||
| 	`, pb) | ||||
| 	want := `line 5: Any message unpacked multiple times, or "value" already set` | ||||
| 	if err.Error() != want { | ||||
| 		t.Errorf("incorrect error.\nHave: %v\nWant: %v", err.Error(), want) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										300
									
								
								vendor/github.com/gogo/protobuf/proto/clone_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										300
									
								
								vendor/github.com/gogo/protobuf/proto/clone_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,300 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2011 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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/gogo/protobuf/proto" | ||||
|  | ||||
| 	proto3pb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| 	pb "github.com/gogo/protobuf/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), | ||||
| 	}, | ||||
| 	RepBytes: [][]byte{[]byte("sham"), []byte("wow")}, | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	ext := &pb.Ext{ | ||||
| 		Data: proto.String("extension"), | ||||
| 	} | ||||
| 	if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil { | ||||
| 		panic("SetExtension: " + err.Error()) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestClone(t *testing.T) { | ||||
| 	m := proto.Clone(cloneTestMessage).(*pb.MyMessage) | ||||
| 	if !proto.Equal(m, cloneTestMessage) { | ||||
| 		t.Errorf("Clone(%v) = %v", cloneTestMessage, m) | ||||
| 	} | ||||
|  | ||||
| 	// Verify it was a deep copy. | ||||
| 	*m.Inner.Port++ | ||||
| 	if proto.Equal(m, cloneTestMessage) { | ||||
| 		t.Error("Mutating clone changed the original") | ||||
| 	} | ||||
| 	// Byte fields and repeated fields should be copied. | ||||
| 	if &m.Pet[0] == &cloneTestMessage.Pet[0] { | ||||
| 		t.Error("Pet: repeated field not copied") | ||||
| 	} | ||||
| 	if &m.Others[0] == &cloneTestMessage.Others[0] { | ||||
| 		t.Error("Others: repeated field not copied") | ||||
| 	} | ||||
| 	if &m.Others[0].Value[0] == &cloneTestMessage.Others[0].Value[0] { | ||||
| 		t.Error("Others[0].Value: bytes field not copied") | ||||
| 	} | ||||
| 	if &m.RepBytes[0] == &cloneTestMessage.RepBytes[0] { | ||||
| 		t.Error("RepBytes: repeated field not copied") | ||||
| 	} | ||||
| 	if &m.RepBytes[0][0] == &cloneTestMessage.RepBytes[0][0] { | ||||
| 		t.Error("RepBytes[0]: bytes field not copied") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestCloneNil(t *testing.T) { | ||||
| 	var m *pb.MyMessage | ||||
| 	if c := proto.Clone(m); !proto.Equal(m, c) { | ||||
| 		t.Errorf("Clone(%v) = %v", m, c) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| var mergeTests = []struct { | ||||
| 	src, dst, want proto.Message | ||||
| }{ | ||||
| 	{ | ||||
| 		src: &pb.MyMessage{ | ||||
| 			Count: proto.Int32(42), | ||||
| 		}, | ||||
| 		dst: &pb.MyMessage{ | ||||
| 			Name: proto.String("Dave"), | ||||
| 		}, | ||||
| 		want: &pb.MyMessage{ | ||||
| 			Count: proto.Int32(42), | ||||
| 			Name:  proto.String("Dave"), | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		src: &pb.MyMessage{ | ||||
| 			Inner: &pb.InnerMessage{ | ||||
| 				Host:      proto.String("hey"), | ||||
| 				Connected: proto.Bool(true), | ||||
| 			}, | ||||
| 			Pet: []string{"horsey"}, | ||||
| 			Others: []*pb.OtherMessage{ | ||||
| 				{ | ||||
| 					Value: []byte("some bytes"), | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		dst: &pb.MyMessage{ | ||||
| 			Inner: &pb.InnerMessage{ | ||||
| 				Host: proto.String("niles"), | ||||
| 				Port: proto.Int32(9099), | ||||
| 			}, | ||||
| 			Pet: []string{"bunny", "kitty"}, | ||||
| 			Others: []*pb.OtherMessage{ | ||||
| 				{ | ||||
| 					Key: proto.Int64(31415926535), | ||||
| 				}, | ||||
| 				{ | ||||
| 					// Explicitly test a src=nil field | ||||
| 					Inner: nil, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		want: &pb.MyMessage{ | ||||
| 			Inner: &pb.InnerMessage{ | ||||
| 				Host:      proto.String("hey"), | ||||
| 				Connected: proto.Bool(true), | ||||
| 				Port:      proto.Int32(9099), | ||||
| 			}, | ||||
| 			Pet: []string{"bunny", "kitty", "horsey"}, | ||||
| 			Others: []*pb.OtherMessage{ | ||||
| 				{ | ||||
| 					Key: proto.Int64(31415926535), | ||||
| 				}, | ||||
| 				{}, | ||||
| 				{ | ||||
| 					Value: []byte("some bytes"), | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		src: &pb.MyMessage{ | ||||
| 			RepBytes: [][]byte{[]byte("wow")}, | ||||
| 		}, | ||||
| 		dst: &pb.MyMessage{ | ||||
| 			Somegroup: &pb.MyMessage_SomeGroup{ | ||||
| 				GroupField: proto.Int32(6), | ||||
| 			}, | ||||
| 			RepBytes: [][]byte{[]byte("sham")}, | ||||
| 		}, | ||||
| 		want: &pb.MyMessage{ | ||||
| 			Somegroup: &pb.MyMessage_SomeGroup{ | ||||
| 				GroupField: proto.Int32(6), | ||||
| 			}, | ||||
| 			RepBytes: [][]byte{[]byte("sham"), []byte("wow")}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Check that a scalar bytes field replaces rather than appends. | ||||
| 	{ | ||||
| 		src:  &pb.OtherMessage{Value: []byte("foo")}, | ||||
| 		dst:  &pb.OtherMessage{Value: []byte("bar")}, | ||||
| 		want: &pb.OtherMessage{Value: []byte("foo")}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		src: &pb.MessageWithMap{ | ||||
| 			NameMapping: map[int32]string{6: "Nigel"}, | ||||
| 			MsgMapping: map[int64]*pb.FloatingPoint{ | ||||
| 				0x4001: {F: proto.Float64(2.0)}, | ||||
| 				0x4002: { | ||||
| 					F: proto.Float64(2.0), | ||||
| 				}, | ||||
| 			}, | ||||
| 			ByteMapping: map[bool][]byte{true: []byte("wowsa")}, | ||||
| 		}, | ||||
| 		dst: &pb.MessageWithMap{ | ||||
| 			NameMapping: map[int32]string{ | ||||
| 				6: "Bruce", // should be overwritten | ||||
| 				7: "Andrew", | ||||
| 			}, | ||||
| 			MsgMapping: map[int64]*pb.FloatingPoint{ | ||||
| 				0x4002: { | ||||
| 					F:     proto.Float64(3.0), | ||||
| 					Exact: proto.Bool(true), | ||||
| 				}, // the entire message should be overwritten | ||||
| 			}, | ||||
| 		}, | ||||
| 		want: &pb.MessageWithMap{ | ||||
| 			NameMapping: map[int32]string{ | ||||
| 				6: "Nigel", | ||||
| 				7: "Andrew", | ||||
| 			}, | ||||
| 			MsgMapping: map[int64]*pb.FloatingPoint{ | ||||
| 				0x4001: {F: proto.Float64(2.0)}, | ||||
| 				0x4002: { | ||||
| 					F: proto.Float64(2.0), | ||||
| 				}, | ||||
| 			}, | ||||
| 			ByteMapping: map[bool][]byte{true: []byte("wowsa")}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// proto3 shouldn't merge zero values, | ||||
| 	// in the same way that proto2 shouldn't merge nils. | ||||
| 	{ | ||||
| 		src: &proto3pb.Message{ | ||||
| 			Name: "Aaron", | ||||
| 			Data: []byte(""), // zero value, but not nil | ||||
| 		}, | ||||
| 		dst: &proto3pb.Message{ | ||||
| 			HeightInCm: 176, | ||||
| 			Data:       []byte("texas!"), | ||||
| 		}, | ||||
| 		want: &proto3pb.Message{ | ||||
| 			Name:       "Aaron", | ||||
| 			HeightInCm: 176, | ||||
| 			Data:       []byte("texas!"), | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Oneof fields should merge by assignment. | ||||
| 	{ | ||||
| 		src: &pb.Communique{ | ||||
| 			Union: &pb.Communique_Number{Number: 41}, | ||||
| 		}, | ||||
| 		dst: &pb.Communique{ | ||||
| 			Union: &pb.Communique_Name{Name: "Bobby Tables"}, | ||||
| 		}, | ||||
| 		want: &pb.Communique{ | ||||
| 			Union: &pb.Communique_Number{Number: 41}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Oneof nil is the same as not set. | ||||
| 	{ | ||||
| 		src: &pb.Communique{}, | ||||
| 		dst: &pb.Communique{ | ||||
| 			Union: &pb.Communique_Name{Name: "Bobby Tables"}, | ||||
| 		}, | ||||
| 		want: &pb.Communique{ | ||||
| 			Union: &pb.Communique_Name{Name: "Bobby Tables"}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		src: &proto3pb.Message{ | ||||
| 			Terrain: map[string]*proto3pb.Nested{ | ||||
| 				"kay_a": {Cute: true},      // replace | ||||
| 				"kay_b": {Bunny: "rabbit"}, // insert | ||||
| 			}, | ||||
| 		}, | ||||
| 		dst: &proto3pb.Message{ | ||||
| 			Terrain: map[string]*proto3pb.Nested{ | ||||
| 				"kay_a": {Bunny: "lost"},  // replaced | ||||
| 				"kay_c": {Bunny: "bunny"}, // keep | ||||
| 			}, | ||||
| 		}, | ||||
| 		want: &proto3pb.Message{ | ||||
| 			Terrain: map[string]*proto3pb.Nested{ | ||||
| 				"kay_a": {Cute: true}, | ||||
| 				"kay_b": {Bunny: "rabbit"}, | ||||
| 				"kay_c": {Bunny: "bunny"}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| } | ||||
|  | ||||
| func TestMerge(t *testing.T) { | ||||
| 	for _, m := range mergeTests { | ||||
| 		got := proto.Clone(m.dst) | ||||
| 		proto.Merge(got, m.src) | ||||
| 		if !proto.Equal(got, m.want) { | ||||
| 			t.Errorf("Merge(%v, %v)\n got %v\nwant %v\n", m.dst, m.src, got, m.want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										262
									
								
								vendor/github.com/gogo/protobuf/proto/decode_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										262
									
								
								vendor/github.com/gogo/protobuf/proto/decode_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,262 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2010 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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. | ||||
|  | ||||
| // +build go1.7 | ||||
|  | ||||
| package proto_test | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/gogo/protobuf/proto" | ||||
| 	tpb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	bytesBlackhole []byte | ||||
| 	msgBlackhole   = new(tpb.Message) | ||||
| ) | ||||
|  | ||||
| // Disabled this Benchmark because it is using features (b.Run) from go1.7 and gogoprotobuf still have compatibility with go1.5 | ||||
| // BenchmarkVarint32ArraySmall shows the performance on an array of small int32 fields (1 and | ||||
| // 2 bytes long). | ||||
| // func BenchmarkVarint32ArraySmall(b *testing.B) { | ||||
| // 	for i := uint(1); i <= 10; i++ { | ||||
| // 		dist := genInt32Dist([7]int{0, 3, 1}, 1<<i) | ||||
| // 		raw, err := proto.Marshal(&tpb.Message{ | ||||
| // 			ShortKey: dist, | ||||
| // 		}) | ||||
| // 		if err != nil { | ||||
| // 			b.Error("wrong encode", err) | ||||
| // 		} | ||||
| // 		b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) { | ||||
| // 			scratchBuf := proto.NewBuffer(nil) | ||||
| // 			b.ResetTimer() | ||||
| // 			for k := 0; k < b.N; k++ { | ||||
| // 				scratchBuf.SetBuf(raw) | ||||
| // 				msgBlackhole.Reset() | ||||
| // 				if err := scratchBuf.Unmarshal(msgBlackhole); err != nil { | ||||
| // 					b.Error("wrong decode", err) | ||||
| // 				} | ||||
| // 			} | ||||
| // 		}) | ||||
| // 	} | ||||
| // } | ||||
|  | ||||
| // Disabled this Benchmark because it is using features (b.Run) from go1.7 and gogoprotobuf still have compatibility with go1.5 | ||||
| // BenchmarkVarint32ArrayLarge shows the performance on an array of large int32 fields (3 and | ||||
| // 4 bytes long, with a small number of 1, 2, 5 and 10 byte long versions). | ||||
| // func BenchmarkVarint32ArrayLarge(b *testing.B) { | ||||
| // 	for i := uint(1); i <= 10; i++ { | ||||
| // 		dist := genInt32Dist([7]int{0, 1, 2, 4, 8, 1, 1}, 1<<i) | ||||
| // 		raw, err := proto.Marshal(&tpb.Message{ | ||||
| // 			ShortKey: dist, | ||||
| // 		}) | ||||
| // 		if err != nil { | ||||
| // 			b.Error("wrong encode", err) | ||||
| // 		} | ||||
| // 		b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) { | ||||
| // 			scratchBuf := proto.NewBuffer(nil) | ||||
| // 			b.ResetTimer() | ||||
| // 			for k := 0; k < b.N; k++ { | ||||
| // 				scratchBuf.SetBuf(raw) | ||||
| // 				msgBlackhole.Reset() | ||||
| // 				if err := scratchBuf.Unmarshal(msgBlackhole); err != nil { | ||||
| // 					b.Error("wrong decode", err) | ||||
| // 				} | ||||
| // 			} | ||||
| // 		}) | ||||
| // 	} | ||||
| // } | ||||
|  | ||||
| // Disabled this Benchmark because it is using features (b.Run) from go1.7 and gogoprotobuf still have compatibility with go1.5 | ||||
| // BenchmarkVarint64ArraySmall shows the performance on an array of small int64 fields (1 and | ||||
| // 2 bytes long). | ||||
| // func BenchmarkVarint64ArraySmall(b *testing.B) { | ||||
| // 	for i := uint(1); i <= 10; i++ { | ||||
| // 		dist := genUint64Dist([11]int{0, 3, 1}, 1<<i) | ||||
| // 		raw, err := proto.Marshal(&tpb.Message{ | ||||
| // 			Key: dist, | ||||
| // 		}) | ||||
| // 		if err != nil { | ||||
| // 			b.Error("wrong encode", err) | ||||
| // 		} | ||||
| // 		b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) { | ||||
| // 			scratchBuf := proto.NewBuffer(nil) | ||||
| // 			b.ResetTimer() | ||||
| // 			for k := 0; k < b.N; k++ { | ||||
| // 				scratchBuf.SetBuf(raw) | ||||
| // 				msgBlackhole.Reset() | ||||
| // 				if err := scratchBuf.Unmarshal(msgBlackhole); err != nil { | ||||
| // 					b.Error("wrong decode", err) | ||||
| // 				} | ||||
| // 			} | ||||
| // 		}) | ||||
| // 	} | ||||
| // } | ||||
|  | ||||
| // Disabled this Benchmark because it is using features (b.Run) from go1.7 and gogoprotobuf still have compatibility with go1.5 | ||||
| // BenchmarkVarint64ArrayLarge shows the performance on an array of large int64 fields (6, 7, | ||||
| // and 8 bytes long with a small number of the other sizes). | ||||
| // func BenchmarkVarint64ArrayLarge(b *testing.B) { | ||||
| // 	for i := uint(1); i <= 10; i++ { | ||||
| // 		dist := genUint64Dist([11]int{0, 1, 1, 2, 4, 8, 16, 32, 16, 1, 1}, 1<<i) | ||||
| // 		raw, err := proto.Marshal(&tpb.Message{ | ||||
| // 			Key: dist, | ||||
| // 		}) | ||||
| // 		if err != nil { | ||||
| // 			b.Error("wrong encode", err) | ||||
| // 		} | ||||
| // 		b.Run(fmt.Sprintf("Len%v", len(dist)), func(b *testing.B) { | ||||
| // 			scratchBuf := proto.NewBuffer(nil) | ||||
| // 			b.ResetTimer() | ||||
| // 			for k := 0; k < b.N; k++ { | ||||
| // 				scratchBuf.SetBuf(raw) | ||||
| // 				msgBlackhole.Reset() | ||||
| // 				if err := scratchBuf.Unmarshal(msgBlackhole); err != nil { | ||||
| // 					b.Error("wrong decode", err) | ||||
| // 				} | ||||
| // 			} | ||||
| // 		}) | ||||
| // 	} | ||||
| // } | ||||
|  | ||||
| // Disabled this Benchmark because it is using features (b.Run) from go1.7 and gogoprotobuf still have compatibility with go1.5 | ||||
| // BenchmarkVarint64ArrayMixed shows the performance of lots of small messages, each | ||||
| // containing a small number of large (3, 4, and 5 byte) repeated int64s. | ||||
| // func BenchmarkVarint64ArrayMixed(b *testing.B) { | ||||
| // 	for i := uint(1); i <= 1<<5; i <<= 1 { | ||||
| // 		dist := genUint64Dist([11]int{0, 0, 0, 4, 6, 4, 0, 0, 0, 0, 0}, int(i)) | ||||
| // 		// number of sub fields | ||||
| // 		for k := uint(1); k <= 1<<10; k <<= 2 { | ||||
| // 			msg := &tpb.Message{} | ||||
| // 			for m := uint(0); m < k; m++ { | ||||
| // 				msg.Children = append(msg.Children, &tpb.Message{ | ||||
| // 					Key: dist, | ||||
| // 				}) | ||||
| // 			} | ||||
| // 			raw, err := proto.Marshal(msg) | ||||
| // 			if err != nil { | ||||
| // 				b.Error("wrong encode", err) | ||||
| // 			} | ||||
| // 			b.Run(fmt.Sprintf("Fields%vLen%v", k, i), func(b *testing.B) { | ||||
| // 				scratchBuf := proto.NewBuffer(nil) | ||||
| // 				b.ResetTimer() | ||||
| // 				for k := 0; k < b.N; k++ { | ||||
| // 					scratchBuf.SetBuf(raw) | ||||
| // 					msgBlackhole.Reset() | ||||
| // 					if err := scratchBuf.Unmarshal(msgBlackhole); err != nil { | ||||
| // 						b.Error("wrong decode", err) | ||||
| // 					} | ||||
| // 				} | ||||
| // 			}) | ||||
| // 		} | ||||
| // 	} | ||||
| // } | ||||
|  | ||||
| // genInt32Dist generates a slice of ints that will match the size distribution of dist. | ||||
| // A size of 6 corresponds to a max length varint32, which is 10 bytes.  The distribution | ||||
| // is 1-indexed. (i.e. the value at index 1 is how many 1 byte ints to create). | ||||
| func genInt32Dist(dist [7]int, count int) (dest []int32) { | ||||
| 	for i := 0; i < count; i++ { | ||||
| 		for k := 0; k < len(dist); k++ { | ||||
| 			var num int32 | ||||
| 			switch k { | ||||
| 			case 1: | ||||
| 				num = 1<<7 - 1 | ||||
| 			case 2: | ||||
| 				num = 1<<14 - 1 | ||||
| 			case 3: | ||||
| 				num = 1<<21 - 1 | ||||
| 			case 4: | ||||
| 				num = 1<<28 - 1 | ||||
| 			case 5: | ||||
| 				num = 1<<29 - 1 | ||||
| 			case 6: | ||||
| 				num = -1 | ||||
| 			} | ||||
| 			for m := 0; m < dist[k]; m++ { | ||||
| 				dest = append(dest, num) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // genUint64Dist generates a slice of ints that will match the size distribution of dist. | ||||
| // The distribution is 1-indexed. (i.e. the value at index 1 is how many 1 byte ints to create). | ||||
| func genUint64Dist(dist [11]int, count int) (dest []uint64) { | ||||
| 	for i := 0; i < count; i++ { | ||||
| 		for k := 0; k < len(dist); k++ { | ||||
| 			var num uint64 | ||||
| 			switch k { | ||||
| 			case 1: | ||||
| 				num = 1<<7 - 1 | ||||
| 			case 2: | ||||
| 				num = 1<<14 - 1 | ||||
| 			case 3: | ||||
| 				num = 1<<21 - 1 | ||||
| 			case 4: | ||||
| 				num = 1<<28 - 1 | ||||
| 			case 5: | ||||
| 				num = 1<<35 - 1 | ||||
| 			case 6: | ||||
| 				num = 1<<42 - 1 | ||||
| 			case 7: | ||||
| 				num = 1<<49 - 1 | ||||
| 			case 8: | ||||
| 				num = 1<<56 - 1 | ||||
| 			case 9: | ||||
| 				num = 1<<63 - 1 | ||||
| 			case 10: | ||||
| 				num = 1<<64 - 1 | ||||
| 			} | ||||
| 			for m := 0; m < dist[k]; m++ { | ||||
| 				dest = append(dest, num) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // BenchmarkDecodeEmpty measures the overhead of doing the minimal possible decode. | ||||
| func BenchmarkDecodeEmpty(b *testing.B) { | ||||
| 	raw, err := proto.Marshal(&tpb.Message{}) | ||||
| 	if err != nil { | ||||
| 		b.Error("wrong encode", err) | ||||
| 	} | ||||
| 	b.ResetTimer() | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		if err := proto.Unmarshal(raw, msgBlackhole); err != nil { | ||||
| 			b.Error("wrong decode", err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										84
									
								
								vendor/github.com/gogo/protobuf/proto/encode_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										84
									
								
								vendor/github.com/gogo/protobuf/proto/encode_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,84 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2010 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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. | ||||
|  | ||||
| // +build go1.7 | ||||
|  | ||||
| package proto_test | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/gogo/protobuf/proto" | ||||
| 	tpb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	blackhole []byte | ||||
| ) | ||||
|  | ||||
| // Disabled this Benchmark because it is using features (b.Run) from go1.7 and gogoprotobuf still have compatibility with go1.5 | ||||
| // BenchmarkAny creates increasingly large arbitrary Any messages.  The type is always the | ||||
| // same. | ||||
| // func BenchmarkAny(b *testing.B) { | ||||
| // 	data := make([]byte, 1<<20) | ||||
| // 	quantum := 1 << 10 | ||||
| // 	for i := uint(0); i <= 10; i++ { | ||||
| // 		b.Run(strconv.Itoa(quantum<<i), func(b *testing.B) { | ||||
| // 			for k := 0; k < b.N; k++ { | ||||
| // 				inner := &tpb.Message{ | ||||
| // 					Data: data[:quantum<<i], | ||||
| // 				} | ||||
| // 				outer, err := types.MarshalAny(inner) | ||||
| // 				if err != nil { | ||||
| // 					b.Error("wrong encode", err) | ||||
| // 				} | ||||
| // 				raw, err := proto.Marshal(&tpb.Message{ | ||||
| // 					Anything: outer, | ||||
| // 				}) | ||||
| // 				if err != nil { | ||||
| // 					b.Error("wrong encode", err) | ||||
| // 				} | ||||
| // 				blackhole = raw | ||||
| // 			} | ||||
| // 		}) | ||||
| // 	} | ||||
| // } | ||||
|  | ||||
| // BenchmarkEmpy measures the overhead of doing the minimal possible encode. | ||||
| func BenchmarkEmpy(b *testing.B) { | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		raw, err := proto.Marshal(&tpb.Message{}) | ||||
| 		if err != nil { | ||||
| 			b.Error("wrong encode", err) | ||||
| 		} | ||||
| 		blackhole = raw | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										224
									
								
								vendor/github.com/gogo/protobuf/proto/equal_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										224
									
								
								vendor/github.com/gogo/protobuf/proto/equal_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,224 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2011 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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/gogo/protobuf/proto" | ||||
| 	proto3pb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| 	pb "github.com/gogo/protobuf/proto/testdata" | ||||
| ) | ||||
|  | ||||
| // Four identical base messages. | ||||
| // The init function adds extensions to some of them. | ||||
| var messageWithoutExtension = &pb.MyMessage{Count: Int32(7)} | ||||
| var messageWithExtension1a = &pb.MyMessage{Count: Int32(7)} | ||||
| var messageWithExtension1b = &pb.MyMessage{Count: Int32(7)} | ||||
| var messageWithExtension2 = &pb.MyMessage{Count: Int32(7)} | ||||
|  | ||||
| // Two messages with non-message extensions. | ||||
| var messageWithInt32Extension1 = &pb.MyMessage{Count: Int32(8)} | ||||
| var messageWithInt32Extension2 = &pb.MyMessage{Count: Int32(8)} | ||||
|  | ||||
| func init() { | ||||
| 	ext1 := &pb.Ext{Data: String("Kirk")} | ||||
| 	ext2 := &pb.Ext{Data: String("Picard")} | ||||
|  | ||||
| 	// messageWithExtension1a has ext1, but never marshals it. | ||||
| 	if err := SetExtension(messageWithExtension1a, pb.E_Ext_More, ext1); err != nil { | ||||
| 		panic("SetExtension on 1a failed: " + err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	// messageWithExtension1b is the unmarshaled form of messageWithExtension1a. | ||||
| 	if err := SetExtension(messageWithExtension1b, pb.E_Ext_More, ext1); err != nil { | ||||
| 		panic("SetExtension on 1b failed: " + err.Error()) | ||||
| 	} | ||||
| 	buf, err := Marshal(messageWithExtension1b) | ||||
| 	if err != nil { | ||||
| 		panic("Marshal of 1b failed: " + err.Error()) | ||||
| 	} | ||||
| 	messageWithExtension1b.Reset() | ||||
| 	if err := Unmarshal(buf, messageWithExtension1b); err != nil { | ||||
| 		panic("Unmarshal of 1b failed: " + err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	// messageWithExtension2 has ext2. | ||||
| 	if err := SetExtension(messageWithExtension2, pb.E_Ext_More, ext2); err != nil { | ||||
| 		panic("SetExtension on 2 failed: " + err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(23)); err != nil { | ||||
| 		panic("SetExtension on Int32-1 failed: " + err.Error()) | ||||
| 	} | ||||
| 	if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(24)); err != nil { | ||||
| 		panic("SetExtension on Int32-2 failed: " + err.Error()) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| var EqualTests = []struct { | ||||
| 	desc string | ||||
| 	a, b Message | ||||
| 	exp  bool | ||||
| }{ | ||||
| 	{"different types", &pb.GoEnum{}, &pb.GoTestField{}, false}, | ||||
| 	{"equal empty", &pb.GoEnum{}, &pb.GoEnum{}, true}, | ||||
| 	{"nil vs nil", nil, nil, true}, | ||||
| 	{"typed nil vs typed nil", (*pb.GoEnum)(nil), (*pb.GoEnum)(nil), true}, | ||||
| 	{"typed nil vs empty", (*pb.GoEnum)(nil), &pb.GoEnum{}, false}, | ||||
| 	{"different typed nil", (*pb.GoEnum)(nil), (*pb.GoTestField)(nil), false}, | ||||
|  | ||||
| 	{"one set field, one unset field", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{}, false}, | ||||
| 	{"one set field zero, one unset field", &pb.GoTest{Param: Int32(0)}, &pb.GoTest{}, false}, | ||||
| 	{"different set fields", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("bar")}, false}, | ||||
| 	{"equal set", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("foo")}, true}, | ||||
|  | ||||
| 	{"repeated, one set", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{}, false}, | ||||
| 	{"repeated, different length", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{F_Int32Repeated: []int32{2}}, false}, | ||||
| 	{"repeated, different value", &pb.GoTest{F_Int32Repeated: []int32{2}}, &pb.GoTest{F_Int32Repeated: []int32{3}}, false}, | ||||
| 	{"repeated, equal", &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, true}, | ||||
| 	{"repeated, nil equal nil", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: nil}, true}, | ||||
| 	{"repeated, nil equal empty", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: []int32{}}, true}, | ||||
| 	{"repeated, empty equal nil", &pb.GoTest{F_Int32Repeated: []int32{}}, &pb.GoTest{F_Int32Repeated: nil}, true}, | ||||
|  | ||||
| 	{ | ||||
| 		"nested, different", | ||||
| 		&pb.GoTest{RequiredField: &pb.GoTestField{Label: String("foo")}}, | ||||
| 		&pb.GoTest{RequiredField: &pb.GoTestField{Label: String("bar")}}, | ||||
| 		false, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"nested, equal", | ||||
| 		&pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}}, | ||||
| 		&pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}}, | ||||
| 		true, | ||||
| 	}, | ||||
|  | ||||
| 	{"bytes", &pb.OtherMessage{Value: []byte("foo")}, &pb.OtherMessage{Value: []byte("foo")}, true}, | ||||
| 	{"bytes, empty", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: []byte{}}, true}, | ||||
| 	{"bytes, empty vs nil", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: nil}, false}, | ||||
| 	{ | ||||
| 		"repeated bytes", | ||||
| 		&pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}}, | ||||
| 		&pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}}, | ||||
| 		true, | ||||
| 	}, | ||||
| 	// In proto3, []byte{} and []byte(nil) are equal. | ||||
| 	{"proto3 bytes, empty vs nil", &proto3pb.Message{Data: []byte{}}, &proto3pb.Message{Data: nil}, true}, | ||||
|  | ||||
| 	{"extension vs. no extension", messageWithoutExtension, messageWithExtension1a, false}, | ||||
| 	{"extension vs. same extension", messageWithExtension1a, messageWithExtension1b, true}, | ||||
| 	{"extension vs. different extension", messageWithExtension1a, messageWithExtension2, false}, | ||||
|  | ||||
| 	{"int32 extension vs. itself", messageWithInt32Extension1, messageWithInt32Extension1, true}, | ||||
| 	{"int32 extension vs. a different int32", messageWithInt32Extension1, messageWithInt32Extension2, false}, | ||||
|  | ||||
| 	{ | ||||
| 		"message with group", | ||||
| 		&pb.MyMessage{ | ||||
| 			Count: Int32(1), | ||||
| 			Somegroup: &pb.MyMessage_SomeGroup{ | ||||
| 				GroupField: Int32(5), | ||||
| 			}, | ||||
| 		}, | ||||
| 		&pb.MyMessage{ | ||||
| 			Count: Int32(1), | ||||
| 			Somegroup: &pb.MyMessage_SomeGroup{ | ||||
| 				GroupField: Int32(5), | ||||
| 			}, | ||||
| 		}, | ||||
| 		true, | ||||
| 	}, | ||||
|  | ||||
| 	{ | ||||
| 		"map same", | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, | ||||
| 		true, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"map different entry", | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob"}}, | ||||
| 		false, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"map different key only", | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{2: "Ken"}}, | ||||
| 		false, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"map different value only", | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob"}}, | ||||
| 		false, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"zero-length maps same", | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{}}, | ||||
| 		&pb.MessageWithMap{NameMapping: nil}, | ||||
| 		true, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"orders in map don't matter", | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken", 2: "Rob"}}, | ||||
| 		&pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob", 1: "Ken"}}, | ||||
| 		true, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"oneof same", | ||||
| 		&pb.Communique{Union: &pb.Communique_Number{Number: 41}}, | ||||
| 		&pb.Communique{Union: &pb.Communique_Number{Number: 41}}, | ||||
| 		true, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"oneof one nil", | ||||
| 		&pb.Communique{Union: &pb.Communique_Number{Number: 41}}, | ||||
| 		&pb.Communique{}, | ||||
| 		false, | ||||
| 	}, | ||||
| 	{ | ||||
| 		"oneof different", | ||||
| 		&pb.Communique{Union: &pb.Communique_Number{Number: 41}}, | ||||
| 		&pb.Communique{Union: &pb.Communique_Name{Name: "Bobby Tables"}}, | ||||
| 		false, | ||||
| 	}, | ||||
| } | ||||
|  | ||||
| func TestEqual(t *testing.T) { | ||||
| 	for _, tc := range EqualTests { | ||||
| 		if res := Equal(tc.a, tc.b); res != tc.exp { | ||||
| 			t.Errorf("%v: Equal(%v, %v) = %v, want %v", tc.desc, tc.a, tc.b, res, tc.exp) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										538
									
								
								vendor/github.com/gogo/protobuf/proto/extensions_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										538
									
								
								vendor/github.com/gogo/protobuf/proto/extensions_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,538 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2014 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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 ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"reflect" | ||||
| 	"sort" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/gogo/protobuf/proto" | ||||
| 	pb "github.com/gogo/protobuf/proto/testdata" | ||||
| ) | ||||
|  | ||||
| func TestGetExtensionsWithMissingExtensions(t *testing.T) { | ||||
| 	msg := &pb.MyMessage{} | ||||
| 	ext1 := &pb.Ext{} | ||||
| 	if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil { | ||||
| 		t.Fatalf("Could not set ext1: %s", err) | ||||
| 	} | ||||
| 	exts, err := proto.GetExtensions(msg, []*proto.ExtensionDesc{ | ||||
| 		pb.E_Ext_More, | ||||
| 		pb.E_Ext_Text, | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("GetExtensions() failed: %s", err) | ||||
| 	} | ||||
| 	if exts[0] != ext1 { | ||||
| 		t.Errorf("ext1 not in returned extensions: %T %v", exts[0], exts[0]) | ||||
| 	} | ||||
| 	if exts[1] != nil { | ||||
| 		t.Errorf("ext2 in returned extensions: %T %v", exts[1], exts[1]) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestExtensionDescsWithMissingExtensions(t *testing.T) { | ||||
| 	msg := &pb.MyMessage{Count: proto.Int32(0)} | ||||
| 	extdesc1 := pb.E_Ext_More | ||||
| 	if descs, err := proto.ExtensionDescs(msg); len(descs) != 0 || err != nil { | ||||
| 		t.Errorf("proto.ExtensionDescs: got %d descs, error %v; want 0, nil", len(descs), err) | ||||
| 	} | ||||
|  | ||||
| 	ext1 := &pb.Ext{} | ||||
| 	if err := proto.SetExtension(msg, extdesc1, ext1); err != nil { | ||||
| 		t.Fatalf("Could not set ext1: %s", err) | ||||
| 	} | ||||
| 	extdesc2 := &proto.ExtensionDesc{ | ||||
| 		ExtendedType:  (*pb.MyMessage)(nil), | ||||
| 		ExtensionType: (*bool)(nil), | ||||
| 		Field:         123456789, | ||||
| 		Name:          "a.b", | ||||
| 		Tag:           "varint,123456789,opt", | ||||
| 	} | ||||
| 	ext2 := proto.Bool(false) | ||||
| 	if err := proto.SetExtension(msg, extdesc2, ext2); err != nil { | ||||
| 		t.Fatalf("Could not set ext2: %s", err) | ||||
| 	} | ||||
|  | ||||
| 	b, err := proto.Marshal(msg) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Could not marshal msg: %v", err) | ||||
| 	} | ||||
| 	if err = proto.Unmarshal(b, msg); err != nil { | ||||
| 		t.Fatalf("Could not unmarshal into msg: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	descs, err := proto.ExtensionDescs(msg) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("proto.ExtensionDescs: got error %v", err) | ||||
| 	} | ||||
| 	sortExtDescs(descs) | ||||
| 	wantDescs := []*proto.ExtensionDesc{extdesc1, {Field: extdesc2.Field}} | ||||
| 	if !reflect.DeepEqual(descs, wantDescs) { | ||||
| 		t.Errorf("proto.ExtensionDescs(msg) sorted extension ids: got %+v, want %+v", descs, wantDescs) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type ExtensionDescSlice []*proto.ExtensionDesc | ||||
|  | ||||
| func (s ExtensionDescSlice) Len() int           { return len(s) } | ||||
| func (s ExtensionDescSlice) Less(i, j int) bool { return s[i].Field < s[j].Field } | ||||
| func (s ExtensionDescSlice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] } | ||||
|  | ||||
| func sortExtDescs(s []*proto.ExtensionDesc) { | ||||
| 	sort.Sort(ExtensionDescSlice(s)) | ||||
| } | ||||
|  | ||||
| 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") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestGetExtensionDefaults(t *testing.T) { | ||||
| 	var setFloat64 float64 = 1 | ||||
| 	var setFloat32 float32 = 2 | ||||
| 	var setInt32 int32 = 3 | ||||
| 	var setInt64 int64 = 4 | ||||
| 	var setUint32 uint32 = 5 | ||||
| 	var setUint64 uint64 = 6 | ||||
| 	var setBool = true | ||||
| 	var setBool2 = false | ||||
| 	var setString = "Goodnight string" | ||||
| 	var setBytes = []byte("Goodnight bytes") | ||||
| 	var setEnum = pb.DefaultsMessage_TWO | ||||
|  | ||||
| 	type testcase struct { | ||||
| 		ext  *proto.ExtensionDesc // Extension we are testing. | ||||
| 		want interface{}          // Expected value of extension, or nil (meaning that GetExtension will fail). | ||||
| 		def  interface{}          // Expected value of extension after ClearExtension(). | ||||
| 	} | ||||
| 	tests := []testcase{ | ||||
| 		{pb.E_NoDefaultDouble, setFloat64, nil}, | ||||
| 		{pb.E_NoDefaultFloat, setFloat32, nil}, | ||||
| 		{pb.E_NoDefaultInt32, setInt32, nil}, | ||||
| 		{pb.E_NoDefaultInt64, setInt64, nil}, | ||||
| 		{pb.E_NoDefaultUint32, setUint32, nil}, | ||||
| 		{pb.E_NoDefaultUint64, setUint64, nil}, | ||||
| 		{pb.E_NoDefaultSint32, setInt32, nil}, | ||||
| 		{pb.E_NoDefaultSint64, setInt64, nil}, | ||||
| 		{pb.E_NoDefaultFixed32, setUint32, nil}, | ||||
| 		{pb.E_NoDefaultFixed64, setUint64, nil}, | ||||
| 		{pb.E_NoDefaultSfixed32, setInt32, nil}, | ||||
| 		{pb.E_NoDefaultSfixed64, setInt64, nil}, | ||||
| 		{pb.E_NoDefaultBool, setBool, nil}, | ||||
| 		{pb.E_NoDefaultBool, setBool2, nil}, | ||||
| 		{pb.E_NoDefaultString, setString, nil}, | ||||
| 		{pb.E_NoDefaultBytes, setBytes, nil}, | ||||
| 		{pb.E_NoDefaultEnum, setEnum, nil}, | ||||
| 		{pb.E_DefaultDouble, setFloat64, float64(3.1415)}, | ||||
| 		{pb.E_DefaultFloat, setFloat32, float32(3.14)}, | ||||
| 		{pb.E_DefaultInt32, setInt32, int32(42)}, | ||||
| 		{pb.E_DefaultInt64, setInt64, int64(43)}, | ||||
| 		{pb.E_DefaultUint32, setUint32, uint32(44)}, | ||||
| 		{pb.E_DefaultUint64, setUint64, uint64(45)}, | ||||
| 		{pb.E_DefaultSint32, setInt32, int32(46)}, | ||||
| 		{pb.E_DefaultSint64, setInt64, int64(47)}, | ||||
| 		{pb.E_DefaultFixed32, setUint32, uint32(48)}, | ||||
| 		{pb.E_DefaultFixed64, setUint64, uint64(49)}, | ||||
| 		{pb.E_DefaultSfixed32, setInt32, int32(50)}, | ||||
| 		{pb.E_DefaultSfixed64, setInt64, int64(51)}, | ||||
| 		{pb.E_DefaultBool, setBool, true}, | ||||
| 		{pb.E_DefaultBool, setBool2, true}, | ||||
| 		{pb.E_DefaultString, setString, "Hello, string"}, | ||||
| 		{pb.E_DefaultBytes, setBytes, []byte("Hello, bytes")}, | ||||
| 		{pb.E_DefaultEnum, setEnum, pb.DefaultsMessage_ONE}, | ||||
| 	} | ||||
|  | ||||
| 	checkVal := func(test testcase, msg *pb.DefaultsMessage, valWant interface{}) error { | ||||
| 		val, err := proto.GetExtension(msg, test.ext) | ||||
| 		if err != nil { | ||||
| 			if valWant != nil { | ||||
| 				return fmt.Errorf("GetExtension(): %s", err) | ||||
| 			} | ||||
| 			if want := proto.ErrMissingExtension; err != want { | ||||
| 				return fmt.Errorf("Unexpected error: got %v, want %v", err, want) | ||||
| 			} | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		// All proto2 extension values are either a pointer to a value or a slice of values. | ||||
| 		ty := reflect.TypeOf(val) | ||||
| 		tyWant := reflect.TypeOf(test.ext.ExtensionType) | ||||
| 		if got, want := ty, tyWant; got != want { | ||||
| 			return fmt.Errorf("unexpected reflect.TypeOf(): got %v want %v", got, want) | ||||
| 		} | ||||
| 		tye := ty.Elem() | ||||
| 		tyeWant := tyWant.Elem() | ||||
| 		if got, want := tye, tyeWant; got != want { | ||||
| 			return fmt.Errorf("unexpected reflect.TypeOf().Elem(): got %v want %v", got, want) | ||||
| 		} | ||||
|  | ||||
| 		// Check the name of the type of the value. | ||||
| 		// If it is an enum it will be type int32 with the name of the enum. | ||||
| 		if got, want := tye.Name(), tye.Name(); got != want { | ||||
| 			return fmt.Errorf("unexpected reflect.TypeOf().Elem().Name(): got %v want %v", got, want) | ||||
| 		} | ||||
|  | ||||
| 		// Check that value is what we expect. | ||||
| 		// If we have a pointer in val, get the value it points to. | ||||
| 		valExp := val | ||||
| 		if ty.Kind() == reflect.Ptr { | ||||
| 			valExp = reflect.ValueOf(val).Elem().Interface() | ||||
| 		} | ||||
| 		if got, want := valExp, valWant; !reflect.DeepEqual(got, want) { | ||||
| 			return fmt.Errorf("unexpected reflect.DeepEqual(): got %v want %v", got, want) | ||||
| 		} | ||||
|  | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	setTo := func(test testcase) interface{} { | ||||
| 		setTo := reflect.ValueOf(test.want) | ||||
| 		if typ := reflect.TypeOf(test.ext.ExtensionType); typ.Kind() == reflect.Ptr { | ||||
| 			setTo = reflect.New(typ).Elem() | ||||
| 			setTo.Set(reflect.New(setTo.Type().Elem())) | ||||
| 			setTo.Elem().Set(reflect.ValueOf(test.want)) | ||||
| 		} | ||||
| 		return setTo.Interface() | ||||
| 	} | ||||
|  | ||||
| 	for _, test := range tests { | ||||
| 		msg := &pb.DefaultsMessage{} | ||||
| 		name := test.ext.Name | ||||
|  | ||||
| 		// Check the initial value. | ||||
| 		if err := checkVal(test, msg, test.def); err != nil { | ||||
| 			t.Errorf("%s: %v", name, err) | ||||
| 		} | ||||
|  | ||||
| 		// Set the per-type value and check value. | ||||
| 		name = fmt.Sprintf("%s (set to %T %v)", name, test.want, test.want) | ||||
| 		if err := proto.SetExtension(msg, test.ext, setTo(test)); err != nil { | ||||
| 			t.Errorf("%s: SetExtension(): %v", name, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if err := checkVal(test, msg, test.want); err != nil { | ||||
| 			t.Errorf("%s: %v", name, err) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// Set and check the value. | ||||
| 		name += " (cleared)" | ||||
| 		proto.ClearExtension(msg, test.ext) | ||||
| 		if err := checkVal(test, msg, test.def); err != nil { | ||||
| 			t.Errorf("%s: %v", name, err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestExtensionsRoundTrip(t *testing.T) { | ||||
| 	msg := &pb.MyMessage{} | ||||
| 	ext1 := &pb.Ext{ | ||||
| 		Data: proto.String("hi"), | ||||
| 	} | ||||
| 	ext2 := &pb.Ext{ | ||||
| 		Data: proto.String("there"), | ||||
| 	} | ||||
| 	exists := proto.HasExtension(msg, pb.E_Ext_More) | ||||
| 	if exists { | ||||
| 		t.Error("Extension More present unexpectedly") | ||||
| 	} | ||||
| 	if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 	if err := proto.SetExtension(msg, pb.E_Ext_More, ext2); err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 	e, err := proto.GetExtension(msg, pb.E_Ext_More) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 	x, ok := e.(*pb.Ext) | ||||
| 	if !ok { | ||||
| 		t.Errorf("e has type %T, expected testdata.Ext", e) | ||||
| 	} else if *x.Data != "there" { | ||||
| 		t.Errorf("SetExtension failed to overwrite, got %+v, not 'there'", x) | ||||
| 	} | ||||
| 	proto.ClearExtension(msg, pb.E_Ext_More) | ||||
| 	if _, err = proto.GetExtension(msg, pb.E_Ext_More); err != proto.ErrMissingExtension { | ||||
| 		t.Errorf("got %v, expected ErrMissingExtension", e) | ||||
| 	} | ||||
| 	if _, err := proto.GetExtension(msg, pb.E_X215); err == nil { | ||||
| 		t.Error("expected bad extension error, got nil") | ||||
| 	} | ||||
| 	if err := proto.SetExtension(msg, pb.E_X215, 12); err == nil { | ||||
| 		t.Error("expected extension err") | ||||
| 	} | ||||
| 	if err := proto.SetExtension(msg, pb.E_Ext_More, 12); err == nil { | ||||
| 		t.Error("expected some sort of type mismatch error, got nil") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| 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. | ||||
| } | ||||
|  | ||||
| func TestMarshalUnmarshalRepeatedExtension(t *testing.T) { | ||||
| 	// Add a repeated extension to the result. | ||||
| 	tests := []struct { | ||||
| 		name string | ||||
| 		ext  []*pb.ComplexExtension | ||||
| 	}{ | ||||
| 		{ | ||||
| 			"two fields", | ||||
| 			[]*pb.ComplexExtension{ | ||||
| 				{First: proto.Int32(7)}, | ||||
| 				{Second: proto.Int32(11)}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			"repeated field", | ||||
| 			[]*pb.ComplexExtension{ | ||||
| 				{Third: []int32{1000}}, | ||||
| 				{Third: []int32{2000}}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			"two fields and repeated field", | ||||
| 			[]*pb.ComplexExtension{ | ||||
| 				{Third: []int32{1000}}, | ||||
| 				{First: proto.Int32(9)}, | ||||
| 				{Second: proto.Int32(21)}, | ||||
| 				{Third: []int32{2000}}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	for _, test := range tests { | ||||
| 		// Marshal message with a repeated extension. | ||||
| 		msg1 := new(pb.OtherMessage) | ||||
| 		err := proto.SetExtension(msg1, pb.E_RComplex, test.ext) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("[%s] Error setting extension: %v", test.name, err) | ||||
| 		} | ||||
| 		b, err := proto.Marshal(msg1) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("[%s] Error marshaling message: %v", test.name, err) | ||||
| 		} | ||||
|  | ||||
| 		// Unmarshal and read the merged proto. | ||||
| 		msg2 := new(pb.OtherMessage) | ||||
| 		err = proto.Unmarshal(b, msg2) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("[%s] Error unmarshaling message: %v", test.name, err) | ||||
| 		} | ||||
| 		e, err := proto.GetExtension(msg2, pb.E_RComplex) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("[%s] Error getting extension: %v", test.name, err) | ||||
| 		} | ||||
| 		ext := e.([]*pb.ComplexExtension) | ||||
| 		if ext == nil { | ||||
| 			t.Fatalf("[%s] Invalid extension", test.name) | ||||
| 		} | ||||
| 		if !reflect.DeepEqual(ext, test.ext) { | ||||
| 			t.Errorf("[%s] Wrong value for ComplexExtension: got: %v want: %v\n", test.name, ext, test.ext) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestUnmarshalRepeatingNonRepeatedExtension(t *testing.T) { | ||||
| 	// We may see multiple instances of the same extension in the wire | ||||
| 	// format. For example, the proto compiler may encode custom options in | ||||
| 	// this way. Here, we verify that we merge the extensions together. | ||||
| 	tests := []struct { | ||||
| 		name string | ||||
| 		ext  []*pb.ComplexExtension | ||||
| 	}{ | ||||
| 		{ | ||||
| 			"two fields", | ||||
| 			[]*pb.ComplexExtension{ | ||||
| 				{First: proto.Int32(7)}, | ||||
| 				{Second: proto.Int32(11)}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			"repeated field", | ||||
| 			[]*pb.ComplexExtension{ | ||||
| 				{Third: []int32{1000}}, | ||||
| 				{Third: []int32{2000}}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			"two fields and repeated field", | ||||
| 			[]*pb.ComplexExtension{ | ||||
| 				{Third: []int32{1000}}, | ||||
| 				{First: proto.Int32(9)}, | ||||
| 				{Second: proto.Int32(21)}, | ||||
| 				{Third: []int32{2000}}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	for _, test := range tests { | ||||
| 		var buf bytes.Buffer | ||||
| 		var want pb.ComplexExtension | ||||
|  | ||||
| 		// Generate a serialized representation of a repeated extension | ||||
| 		// by catenating bytes together. | ||||
| 		for i, e := range test.ext { | ||||
| 			// Merge to create the wanted proto. | ||||
| 			proto.Merge(&want, e) | ||||
|  | ||||
| 			// serialize the message | ||||
| 			msg := new(pb.OtherMessage) | ||||
| 			err := proto.SetExtension(msg, pb.E_Complex, e) | ||||
| 			if err != nil { | ||||
| 				t.Fatalf("[%s] Error setting extension %d: %v", test.name, i, err) | ||||
| 			} | ||||
| 			b, err := proto.Marshal(msg) | ||||
| 			if err != nil { | ||||
| 				t.Fatalf("[%s] Error marshaling message %d: %v", test.name, i, err) | ||||
| 			} | ||||
| 			buf.Write(b) | ||||
| 		} | ||||
|  | ||||
| 		// Unmarshal and read the merged proto. | ||||
| 		msg2 := new(pb.OtherMessage) | ||||
| 		err := proto.Unmarshal(buf.Bytes(), msg2) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("[%s] Error unmarshaling message: %v", test.name, err) | ||||
| 		} | ||||
| 		e, err := proto.GetExtension(msg2, pb.E_Complex) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("[%s] Error getting extension: %v", test.name, err) | ||||
| 		} | ||||
| 		ext := e.(*pb.ComplexExtension) | ||||
| 		if ext == nil { | ||||
| 			t.Fatalf("[%s] Invalid extension", test.name) | ||||
| 		} | ||||
| 		if !reflect.DeepEqual(*ext, want) { | ||||
| 			t.Errorf("[%s] Wrong value for ComplexExtension: got: %v want: %v\n", test.name, ext, want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestClearAllExtensions(t *testing.T) { | ||||
| 	// unregistered extension | ||||
| 	desc := &proto.ExtensionDesc{ | ||||
| 		ExtendedType:  (*pb.MyMessage)(nil), | ||||
| 		ExtensionType: (*bool)(nil), | ||||
| 		Field:         101010100, | ||||
| 		Name:          "emptyextension", | ||||
| 		Tag:           "varint,0,opt", | ||||
| 	} | ||||
| 	m := &pb.MyMessage{} | ||||
| 	if proto.HasExtension(m, desc) { | ||||
| 		t.Errorf("proto.HasExtension(%s): got true, want false", proto.MarshalTextString(m)) | ||||
| 	} | ||||
| 	if err := proto.SetExtension(m, desc, proto.Bool(true)); err != nil { | ||||
| 		t.Errorf("proto.SetExtension(m, desc, true): got error %q, want nil", err) | ||||
| 	} | ||||
| 	if !proto.HasExtension(m, desc) { | ||||
| 		t.Errorf("proto.HasExtension(%s): got false, want true", proto.MarshalTextString(m)) | ||||
| 	} | ||||
| 	proto.ClearAllExtensions(m) | ||||
| 	if proto.HasExtension(m, desc) { | ||||
| 		t.Errorf("proto.HasExtension(%s): got true, want false", proto.MarshalTextString(m)) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMarshalRace(t *testing.T) { | ||||
| 	// unregistered extension | ||||
| 	desc := &proto.ExtensionDesc{ | ||||
| 		ExtendedType:  (*pb.MyMessage)(nil), | ||||
| 		ExtensionType: (*bool)(nil), | ||||
| 		Field:         101010100, | ||||
| 		Name:          "emptyextension", | ||||
| 		Tag:           "varint,0,opt", | ||||
| 	} | ||||
|  | ||||
| 	m := &pb.MyMessage{Count: proto.Int32(4)} | ||||
| 	if err := proto.SetExtension(m, desc, proto.Bool(true)); err != nil { | ||||
| 		t.Errorf("proto.SetExtension(m, desc, true): got error %q, want nil", err) | ||||
| 	} | ||||
|  | ||||
| 	errChan := make(chan error, 3) | ||||
| 	for n := 3; n > 0; n-- { | ||||
| 		go func() { | ||||
| 			_, err := proto.Marshal(m) | ||||
| 			errChan <- err | ||||
| 		}() | ||||
| 	} | ||||
| 	for i := 0; i < 3; i++ { | ||||
| 		err := <-errChan | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										46
									
								
								vendor/github.com/gogo/protobuf/proto/map_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										46
									
								
								vendor/github.com/gogo/protobuf/proto/map_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,46 +0,0 @@ | ||||
| package proto_test | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/gogo/protobuf/proto" | ||||
| 	ppb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| ) | ||||
|  | ||||
| func marshalled() []byte { | ||||
| 	m := &ppb.IntMaps{} | ||||
| 	for i := 0; i < 1000; i++ { | ||||
| 		m.Maps = append(m.Maps, &ppb.IntMap{ | ||||
| 			Rtt: map[int32]int32{1: 2}, | ||||
| 		}) | ||||
| 	} | ||||
| 	b, err := proto.Marshal(m) | ||||
| 	if err != nil { | ||||
| 		panic(fmt.Sprintf("Can't marshal %+v: %v", m, err)) | ||||
| 	} | ||||
| 	return b | ||||
| } | ||||
|  | ||||
| func BenchmarkConcurrentMapUnmarshal(b *testing.B) { | ||||
| 	in := marshalled() | ||||
| 	b.RunParallel(func(pb *testing.PB) { | ||||
| 		for pb.Next() { | ||||
| 			var out ppb.IntMaps | ||||
| 			if err := proto.Unmarshal(in, &out); err != nil { | ||||
| 				b.Errorf("Can't unmarshal ppb.IntMaps: %v", err) | ||||
| 			} | ||||
| 		} | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func BenchmarkSequentialMapUnmarshal(b *testing.B) { | ||||
| 	in := marshalled() | ||||
| 	b.ResetTimer() | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		var out ppb.IntMaps | ||||
| 		if err := proto.Unmarshal(in, &out); err != nil { | ||||
| 			b.Errorf("Can't unmarshal ppb.IntMaps: %v", err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										66
									
								
								vendor/github.com/gogo/protobuf/proto/message_set_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										66
									
								
								vendor/github.com/gogo/protobuf/proto/message_set_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,66 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2014 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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 | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestUnmarshalMessageSetWithDuplicate(t *testing.T) { | ||||
| 	// Check that a repeated message set entry will be concatenated. | ||||
| 	in := &messageSet{ | ||||
| 		Item: []*_MessageSet_Item{ | ||||
| 			{TypeId: Int32(12345), Message: []byte("hoo")}, | ||||
| 			{TypeId: Int32(12345), Message: []byte("hah")}, | ||||
| 		}, | ||||
| 	} | ||||
| 	b, err := Marshal(in) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Marshal: %v", err) | ||||
| 	} | ||||
| 	t.Logf("Marshaled bytes: %q", b) | ||||
|  | ||||
| 	var extensions XXX_InternalExtensions | ||||
| 	if err := UnmarshalMessageSet(b, &extensions); err != nil { | ||||
| 		t.Fatalf("UnmarshalMessageSet: %v", err) | ||||
| 	} | ||||
| 	ext, ok := extensions.p.extensionMap[12345] | ||||
| 	if !ok { | ||||
| 		t.Fatalf("Didn't retrieve extension 12345; map is %v", extensions.p.extensionMap) | ||||
| 	} | ||||
| 	// Skip wire type/field number and length varints. | ||||
| 	got := skipVarint(skipVarint(ext.enc)) | ||||
| 	if want := []byte("hoohah"); !bytes.Equal(got, want) { | ||||
| 		t.Errorf("Combined extension is %q, want %q", got, want) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										135
									
								
								vendor/github.com/gogo/protobuf/proto/proto3_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										135
									
								
								vendor/github.com/gogo/protobuf/proto/proto3_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,135 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2014 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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/gogo/protobuf/proto" | ||||
| 	pb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| 	tpb "github.com/gogo/protobuf/proto/testdata" | ||||
| ) | ||||
|  | ||||
| func TestProto3ZeroValues(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		desc string | ||||
| 		m    proto.Message | ||||
| 	}{ | ||||
| 		{"zero message", &pb.Message{}}, | ||||
| 		{"empty bytes field", &pb.Message{Data: []byte{}}}, | ||||
| 	} | ||||
| 	for _, test := range tests { | ||||
| 		b, err := proto.Marshal(test.m) | ||||
| 		if err != nil { | ||||
| 			t.Errorf("%s: proto.Marshal: %v", test.desc, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if len(b) > 0 { | ||||
| 			t.Errorf("%s: Encoding is non-empty: %q", test.desc, b) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestRoundTripProto3(t *testing.T) { | ||||
| 	m := &pb.Message{ | ||||
| 		Name:         "David",          // (2 | 1<<3): 0x0a 0x05 "David" | ||||
| 		Hilarity:     pb.Message_PUNS,  // (0 | 2<<3): 0x10 0x01 | ||||
| 		HeightInCm:   178,              // (0 | 3<<3): 0x18 0xb2 0x01 | ||||
| 		Data:         []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto" | ||||
| 		ResultCount:  47,               // (0 | 7<<3): 0x38 0x2f | ||||
| 		TrueScotsman: true,             // (0 | 8<<3): 0x40 0x01 | ||||
| 		Score:        8.1,              // (5 | 9<<3): 0x4d <8.1> | ||||
|  | ||||
| 		Key: []uint64{1, 0xdeadbeef}, | ||||
| 		Nested: &pb.Nested{ | ||||
| 			Bunny: "Monty", | ||||
| 		}, | ||||
| 	} | ||||
| 	t.Logf(" m: %v", m) | ||||
|  | ||||
| 	b, err := proto.Marshal(m) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("proto.Marshal: %v", err) | ||||
| 	} | ||||
| 	t.Logf(" b: %q", b) | ||||
|  | ||||
| 	m2 := new(pb.Message) | ||||
| 	if err := proto.Unmarshal(b, m2); err != nil { | ||||
| 		t.Fatalf("proto.Unmarshal: %v", err) | ||||
| 	} | ||||
| 	t.Logf("m2: %v", m2) | ||||
|  | ||||
| 	if !proto.Equal(m, m2) { | ||||
| 		t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestGettersForBasicTypesExist(t *testing.T) { | ||||
| 	var m pb.Message | ||||
| 	if got := m.GetNested().GetBunny(); got != "" { | ||||
| 		t.Errorf("m.GetNested().GetBunny() = %q, want empty string", got) | ||||
| 	} | ||||
| 	if got := m.GetNested().GetCute(); got { | ||||
| 		t.Errorf("m.GetNested().GetCute() = %t, want false", got) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestProto3SetDefaults(t *testing.T) { | ||||
| 	in := &pb.Message{ | ||||
| 		Terrain: map[string]*pb.Nested{ | ||||
| 			"meadow": new(pb.Nested), | ||||
| 		}, | ||||
| 		Proto2Field: new(tpb.SubDefaults), | ||||
| 		Proto2Value: map[string]*tpb.SubDefaults{ | ||||
| 			"badlands": new(tpb.SubDefaults), | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	got := proto.Clone(in).(*pb.Message) | ||||
| 	proto.SetDefaults(got) | ||||
|  | ||||
| 	// There are no defaults in proto3.  Everything should be the zero value, but | ||||
| 	// we need to remember to set defaults for nested proto2 messages. | ||||
| 	want := &pb.Message{ | ||||
| 		Terrain: map[string]*pb.Nested{ | ||||
| 			"meadow": new(pb.Nested), | ||||
| 		}, | ||||
| 		Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)}, | ||||
| 		Proto2Value: map[string]*tpb.SubDefaults{ | ||||
| 			"badlands": {N: proto.Int64(7)}, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	if !proto.Equal(got, want) { | ||||
| 		t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										63
									
								
								vendor/github.com/gogo/protobuf/proto/size2_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										63
									
								
								vendor/github.com/gogo/protobuf/proto/size2_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,63 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2012 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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 | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| // This is a separate file and package from size_test.go because that one uses | ||||
| // generated messages and thus may not be in package proto without having a circular | ||||
| // dependency, whereas this file tests unexported details of size.go. | ||||
|  | ||||
| func TestVarintSize(t *testing.T) { | ||||
| 	// Check the edge cases carefully. | ||||
| 	testCases := []struct { | ||||
| 		n    uint64 | ||||
| 		size int | ||||
| 	}{ | ||||
| 		{0, 1}, | ||||
| 		{1, 1}, | ||||
| 		{127, 1}, | ||||
| 		{128, 2}, | ||||
| 		{16383, 2}, | ||||
| 		{16384, 3}, | ||||
| 		{1<<63 - 1, 9}, | ||||
| 		{1 << 63, 10}, | ||||
| 	} | ||||
| 	for _, tc := range testCases { | ||||
| 		size := sizeVarint(tc.n) | ||||
| 		if size != tc.size { | ||||
| 			t.Errorf("sizeVarint(%d) = %d, want %d", tc.n, size, tc.size) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										164
									
								
								vendor/github.com/gogo/protobuf/proto/size_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										164
									
								
								vendor/github.com/gogo/protobuf/proto/size_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,164 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2012 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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 ( | ||||
| 	"log" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| 	. "github.com/gogo/protobuf/proto" | ||||
| 	proto3pb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| 	pb "github.com/gogo/protobuf/proto/testdata" | ||||
| ) | ||||
|  | ||||
| var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)} | ||||
|  | ||||
| // messageWithExtension2 is in equal_test.go. | ||||
| var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)} | ||||
|  | ||||
| func init() { | ||||
| 	if err := SetExtension(messageWithExtension1, pb.E_Ext_More, &pb.Ext{Data: String("Abbott")}); err != nil { | ||||
| 		log.Panicf("SetExtension: %v", err) | ||||
| 	} | ||||
| 	if err := SetExtension(messageWithExtension3, pb.E_Ext_More, &pb.Ext{Data: String("Costello")}); err != nil { | ||||
| 		log.Panicf("SetExtension: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	// Force messageWithExtension3 to have the extension encoded. | ||||
| 	Marshal(messageWithExtension3) | ||||
|  | ||||
| } | ||||
|  | ||||
| var SizeTests = []struct { | ||||
| 	desc string | ||||
| 	pb   Message | ||||
| }{ | ||||
| 	{"empty", &pb.OtherMessage{}}, | ||||
| 	// Basic types. | ||||
| 	{"bool", &pb.Defaults{F_Bool: Bool(true)}}, | ||||
| 	{"int32", &pb.Defaults{F_Int32: Int32(12)}}, | ||||
| 	{"negative int32", &pb.Defaults{F_Int32: Int32(-1)}}, | ||||
| 	{"small int64", &pb.Defaults{F_Int64: Int64(1)}}, | ||||
| 	{"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}}, | ||||
| 	{"negative int64", &pb.Defaults{F_Int64: Int64(-1)}}, | ||||
| 	{"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}}, | ||||
| 	{"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}}, | ||||
| 	{"uint32", &pb.Defaults{F_Uint32: Uint32(123)}}, | ||||
| 	{"uint64", &pb.Defaults{F_Uint64: Uint64(124)}}, | ||||
| 	{"float", &pb.Defaults{F_Float: Float32(12.6)}}, | ||||
| 	{"double", &pb.Defaults{F_Double: Float64(13.9)}}, | ||||
| 	{"string", &pb.Defaults{F_String: String("niles")}}, | ||||
| 	{"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}}, | ||||
| 	{"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}}, | ||||
| 	{"sint32", &pb.Defaults{F_Sint32: Int32(65)}}, | ||||
| 	{"sint64", &pb.Defaults{F_Sint64: Int64(67)}}, | ||||
| 	{"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}}, | ||||
| 	// Repeated. | ||||
| 	{"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}}, | ||||
| 	{"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}}, | ||||
| 	{"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}}, | ||||
| 	{"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}}, | ||||
| 	{"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}}, | ||||
| 	{"repeated int64 packed", &pb.MoreRepeated{Int64SPacked: []int64{ | ||||
| 		// Need enough large numbers to verify that the header is counting the number of bytes | ||||
| 		// for the field, not the number of elements. | ||||
| 		1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, | ||||
| 		1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, | ||||
| 	}}}, | ||||
| 	{"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}}, | ||||
| 	{"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}}, | ||||
| 	// Nested. | ||||
| 	{"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}}, | ||||
| 	{"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}}, | ||||
| 	// Other things. | ||||
| 	{"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}}, | ||||
| 	{"extension (unencoded)", messageWithExtension1}, | ||||
| 	{"extension (encoded)", messageWithExtension3}, | ||||
| 	// proto3 message | ||||
| 	{"proto3 empty", &proto3pb.Message{}}, | ||||
| 	{"proto3 bool", &proto3pb.Message{TrueScotsman: true}}, | ||||
| 	{"proto3 int64", &proto3pb.Message{ResultCount: 1}}, | ||||
| 	{"proto3 uint32", &proto3pb.Message{HeightInCm: 123}}, | ||||
| 	{"proto3 float", &proto3pb.Message{Score: 12.6}}, | ||||
| 	{"proto3 string", &proto3pb.Message{Name: "Snezana"}}, | ||||
| 	{"proto3 bytes", &proto3pb.Message{Data: []byte("wowsa")}}, | ||||
| 	{"proto3 bytes, empty", &proto3pb.Message{Data: []byte{}}}, | ||||
| 	{"proto3 enum", &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}}, | ||||
| 	{"proto3 map field with empty bytes", &proto3pb.MessageWithMap{ByteMapping: map[bool][]byte{false: {}}}}, | ||||
|  | ||||
| 	{"map field", &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}}, | ||||
| 	{"map field with message", &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{0x7001: {F: Float64(2.0)}}}}, | ||||
| 	{"map field with bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}}, | ||||
| 	{"map field with empty bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: {}}}}, | ||||
|  | ||||
| 	{"map field with big entry", &pb.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}}, | ||||
| 	{"map field with big key and val", &pb.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}}, | ||||
| 	{"map field with big numeric key", &pb.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}}, | ||||
|  | ||||
| 	{"oneof not set", &pb.Oneof{}}, | ||||
| 	{"oneof bool", &pb.Oneof{Union: &pb.Oneof_F_Bool{F_Bool: true}}}, | ||||
| 	{"oneof zero int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{F_Int32: 0}}}, | ||||
| 	{"oneof big int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{F_Int32: 1 << 20}}}, | ||||
| 	{"oneof int64", &pb.Oneof{Union: &pb.Oneof_F_Int64{F_Int64: 42}}}, | ||||
| 	{"oneof fixed32", &pb.Oneof{Union: &pb.Oneof_F_Fixed32{F_Fixed32: 43}}}, | ||||
| 	{"oneof fixed64", &pb.Oneof{Union: &pb.Oneof_F_Fixed64{F_Fixed64: 44}}}, | ||||
| 	{"oneof uint32", &pb.Oneof{Union: &pb.Oneof_F_Uint32{F_Uint32: 45}}}, | ||||
| 	{"oneof uint64", &pb.Oneof{Union: &pb.Oneof_F_Uint64{F_Uint64: 46}}}, | ||||
| 	{"oneof float", &pb.Oneof{Union: &pb.Oneof_F_Float{F_Float: 47.1}}}, | ||||
| 	{"oneof double", &pb.Oneof{Union: &pb.Oneof_F_Double{F_Double: 48.9}}}, | ||||
| 	{"oneof string", &pb.Oneof{Union: &pb.Oneof_F_String{F_String: "Rhythmic Fman"}}}, | ||||
| 	{"oneof bytes", &pb.Oneof{Union: &pb.Oneof_F_Bytes{F_Bytes: []byte("let go")}}}, | ||||
| 	{"oneof sint32", &pb.Oneof{Union: &pb.Oneof_F_Sint32{F_Sint32: 50}}}, | ||||
| 	{"oneof sint64", &pb.Oneof{Union: &pb.Oneof_F_Sint64{F_Sint64: 51}}}, | ||||
| 	{"oneof enum", &pb.Oneof{Union: &pb.Oneof_F_Enum{F_Enum: pb.MyMessage_BLUE}}}, | ||||
| 	{"message for oneof", &pb.GoTestField{Label: String("k"), Type: String("v")}}, | ||||
| 	{"oneof message", &pb.Oneof{Union: &pb.Oneof_F_Message{F_Message: &pb.GoTestField{Label: String("k"), Type: String("v")}}}}, | ||||
| 	{"oneof group", &pb.Oneof{Union: &pb.Oneof_FGroup{FGroup: &pb.Oneof_F_Group{X: Int32(52)}}}}, | ||||
| 	{"oneof largest tag", &pb.Oneof{Union: &pb.Oneof_F_Largest_Tag{F_Largest_Tag: 1}}}, | ||||
| 	{"multiple oneofs", &pb.Oneof{Union: &pb.Oneof_F_Int32{F_Int32: 1}, Tormato: &pb.Oneof_Value{Value: 2}}}, | ||||
| } | ||||
|  | ||||
| func TestSize(t *testing.T) { | ||||
| 	for _, tc := range SizeTests { | ||||
| 		size := Size(tc.pb) | ||||
| 		b, err := Marshal(tc.pb) | ||||
| 		if err != nil { | ||||
| 			t.Errorf("%v: Marshal failed: %v", tc.desc, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if size != len(b) { | ||||
| 			t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b)) | ||||
| 			t.Logf("%v: bytes: %#v", tc.desc, b) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										673
									
								
								vendor/github.com/gogo/protobuf/proto/text_parser_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										673
									
								
								vendor/github.com/gogo/protobuf/proto/text_parser_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,673 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2010 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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 ( | ||||
| 	"math" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
|  | ||||
| 	. "github.com/gogo/protobuf/proto" | ||||
| 	proto3pb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| 	. "github.com/gogo/protobuf/proto/testdata" | ||||
| ) | ||||
|  | ||||
| type UnmarshalTextTest struct { | ||||
| 	in  string | ||||
| 	err string // if "", no error expected | ||||
| 	out *MyMessage | ||||
| } | ||||
|  | ||||
| func buildExtStructTest(text string) UnmarshalTextTest { | ||||
| 	msg := &MyMessage{ | ||||
| 		Count: Int32(42), | ||||
| 	} | ||||
| 	SetExtension(msg, E_Ext_More, &Ext{ | ||||
| 		Data: String("Hello, world!"), | ||||
| 	}) | ||||
| 	return UnmarshalTextTest{in: text, out: msg} | ||||
| } | ||||
|  | ||||
| func buildExtDataTest(text string) UnmarshalTextTest { | ||||
| 	msg := &MyMessage{ | ||||
| 		Count: Int32(42), | ||||
| 	} | ||||
| 	SetExtension(msg, E_Ext_Text, String("Hello, world!")) | ||||
| 	SetExtension(msg, E_Ext_Number, Int32(1729)) | ||||
| 	return UnmarshalTextTest{in: text, out: msg} | ||||
| } | ||||
|  | ||||
| func buildExtRepStringTest(text string) UnmarshalTextTest { | ||||
| 	msg := &MyMessage{ | ||||
| 		Count: Int32(42), | ||||
| 	} | ||||
| 	if err := SetExtension(msg, E_Greeting, []string{"bula", "hola"}); err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	return UnmarshalTextTest{in: text, out: msg} | ||||
| } | ||||
|  | ||||
| var unMarshalTextTests = []UnmarshalTextTest{ | ||||
| 	// Basic | ||||
| 	{ | ||||
| 		in: " count:42\n  name:\"Dave\" ", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("Dave"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Empty quoted string | ||||
| 	{ | ||||
| 		in: `count:42 name:""`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String(""), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string concatenation with double quotes | ||||
| 	{ | ||||
| 		in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("My name is elsewhere"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string concatenation with single quotes | ||||
| 	{ | ||||
| 		in: "count:42 name: 'My name is '\n'elsewhere'", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("My name is elsewhere"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string concatenations with mixed quotes | ||||
| 	{ | ||||
| 		in: "count:42 name: 'My name is '\n\"elsewhere\"", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("My name is elsewhere"), | ||||
| 		}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		in: "count:42 name: \"My name is \"\n'elsewhere'", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("My name is elsewhere"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string with escaped apostrophe | ||||
| 	{ | ||||
| 		in: `count:42 name: "HOLIDAY - New Year\'s Day"`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("HOLIDAY - New Year's Day"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string with single quote | ||||
| 	{ | ||||
| 		in: `count:42 name: 'Roger "The Ramster" Ramjet'`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String(`Roger "The Ramster" Ramjet`), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string with all the accepted special characters from the C++ test | ||||
| 	{ | ||||
| 		in: `count:42 name: ` + "\"\\\"A string with \\' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and  multiple   spaces\"", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and  multiple   spaces"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string with quoted backslash | ||||
| 	{ | ||||
| 		in: `count:42 name: "\\'xyz"`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String(`\'xyz`), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string with UTF-8 bytes. | ||||
| 	{ | ||||
| 		in: "count:42 name: '\303\277\302\201\xAB'", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("\303\277\302\201\xAB"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Bad quoted string | ||||
| 	{ | ||||
| 		in:  `inner: < host: "\0" >` + "\n", | ||||
| 		err: `line 1.15: invalid quoted string "\0": \0 requires 2 following digits`, | ||||
| 	}, | ||||
|  | ||||
| 	// Number too large for int64 | ||||
| 	{ | ||||
| 		in:  "count: 1 others { key: 123456789012345678901 }", | ||||
| 		err: "line 1.23: invalid int64: 123456789012345678901", | ||||
| 	}, | ||||
|  | ||||
| 	// Number too large for int32 | ||||
| 	{ | ||||
| 		in:  "count: 1234567890123", | ||||
| 		err: "line 1.7: invalid int32: 1234567890123", | ||||
| 	}, | ||||
|  | ||||
| 	// Number in hexadecimal | ||||
| 	{ | ||||
| 		in: "count: 0x2beef", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(0x2beef), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Number in octal | ||||
| 	{ | ||||
| 		in: "count: 024601", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(024601), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Floating point number with "f" suffix | ||||
| 	{ | ||||
| 		in: "count: 4 others:< weight: 17.0f >", | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(4), | ||||
| 			Others: []*OtherMessage{ | ||||
| 				{ | ||||
| 					Weight: Float32(17), | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Floating point positive infinity | ||||
| 	{ | ||||
| 		in: "count: 4 bigfloat: inf", | ||||
| 		out: &MyMessage{ | ||||
| 			Count:    Int32(4), | ||||
| 			Bigfloat: Float64(math.Inf(1)), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Floating point negative infinity | ||||
| 	{ | ||||
| 		in: "count: 4 bigfloat: -inf", | ||||
| 		out: &MyMessage{ | ||||
| 			Count:    Int32(4), | ||||
| 			Bigfloat: Float64(math.Inf(-1)), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Number too large for float32 | ||||
| 	{ | ||||
| 		in:  "others:< weight: 12345678901234567890123456789012345678901234567890 >", | ||||
| 		err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890", | ||||
| 	}, | ||||
|  | ||||
| 	// Number posing as a quoted string | ||||
| 	{ | ||||
| 		in:  `inner: < host: 12 >` + "\n", | ||||
| 		err: `line 1.15: invalid string: 12`, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string posing as int32 | ||||
| 	{ | ||||
| 		in:  `count: "12"`, | ||||
| 		err: `line 1.7: invalid int32: "12"`, | ||||
| 	}, | ||||
|  | ||||
| 	// Quoted string posing a float32 | ||||
| 	{ | ||||
| 		in:  `others:< weight: "17.4" >`, | ||||
| 		err: `line 1.17: invalid float32: "17.4"`, | ||||
| 	}, | ||||
|  | ||||
| 	// Enum | ||||
| 	{ | ||||
| 		in: `count:42 bikeshed: BLUE`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count:    Int32(42), | ||||
| 			Bikeshed: MyMessage_BLUE.Enum(), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Repeated field | ||||
| 	{ | ||||
| 		in: `count:42 pet: "horsey" pet:"bunny"`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Pet:   []string{"horsey", "bunny"}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Repeated field with list notation | ||||
| 	{ | ||||
| 		in: `count:42 pet: ["horsey", "bunny"]`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Pet:   []string{"horsey", "bunny"}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Repeated message with/without colon and <>/{} | ||||
| 	{ | ||||
| 		in: `count:42 others:{} others{} others:<> others:{}`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Others: []*OtherMessage{ | ||||
| 				{}, | ||||
| 				{}, | ||||
| 				{}, | ||||
| 				{}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Missing colon for inner message | ||||
| 	{ | ||||
| 		in: `count:42 inner < host: "cauchy.syd" >`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host: String("cauchy.syd"), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Missing colon for string field | ||||
| 	{ | ||||
| 		in:  `name "Dave"`, | ||||
| 		err: `line 1.5: expected ':', found "\"Dave\""`, | ||||
| 	}, | ||||
|  | ||||
| 	// Missing colon for int32 field | ||||
| 	{ | ||||
| 		in:  `count 42`, | ||||
| 		err: `line 1.6: expected ':', found "42"`, | ||||
| 	}, | ||||
|  | ||||
| 	// Missing required field | ||||
| 	{ | ||||
| 		in:  `name: "Pawel"`, | ||||
| 		err: `proto: required field "testdata.MyMessage.count" not set`, | ||||
| 		out: &MyMessage{ | ||||
| 			Name: String("Pawel"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Missing required field in a required submessage | ||||
| 	{ | ||||
| 		in:  `count: 42 we_must_go_deeper < leo_finally_won_an_oscar <> >`, | ||||
| 		err: `proto: required field "testdata.InnerMessage.host" not set`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count:          Int32(42), | ||||
| 			WeMustGoDeeper: &RequiredInnerMessage{LeoFinallyWonAnOscar: &InnerMessage{}}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Repeated non-repeated field | ||||
| 	{ | ||||
| 		in:  `name: "Rob" name: "Russ"`, | ||||
| 		err: `line 1.12: non-repeated field "name" was repeated`, | ||||
| 	}, | ||||
|  | ||||
| 	// Group | ||||
| 	{ | ||||
| 		in: `count: 17 SomeGroup { group_field: 12 }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(17), | ||||
| 			Somegroup: &MyMessage_SomeGroup{ | ||||
| 				GroupField: Int32(12), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Semicolon between fields | ||||
| 	{ | ||||
| 		in: `count:3;name:"Calvin"`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(3), | ||||
| 			Name:  String("Calvin"), | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Comma between fields | ||||
| 	{ | ||||
| 		in: `count:4,name:"Ezekiel"`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(4), | ||||
| 			Name:  String("Ezekiel"), | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Boolean false | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: false }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(false), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Boolean true | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: true }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(true), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Boolean 0 | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: 0 }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(false), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Boolean 1 | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: 1 }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(true), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Boolean f | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: f }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(false), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Boolean t | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: t }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(true), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Boolean False | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: False }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(false), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| 	// Boolean True | ||||
| 	{ | ||||
| 		in: `count:42 inner { host: "example.com" connected: True }`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("example.com"), | ||||
| 				Connected: Bool(true), | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
|  | ||||
| 	// Extension | ||||
| 	buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`), | ||||
| 	buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`), | ||||
| 	buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`), | ||||
| 	buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`), | ||||
|  | ||||
| 	// Big all-in-one | ||||
| 	{ | ||||
| 		in: "count:42  # Meaning\n" + | ||||
| 			`name:"Dave" ` + | ||||
| 			`quote:"\"I didn't want to go.\"" ` + | ||||
| 			`pet:"bunny" ` + | ||||
| 			`pet:"kitty" ` + | ||||
| 			`pet:"horsey" ` + | ||||
| 			`inner:<` + | ||||
| 			`  host:"footrest.syd" ` + | ||||
| 			`  port:7001 ` + | ||||
| 			`  connected:true ` + | ||||
| 			`> ` + | ||||
| 			`others:<` + | ||||
| 			`  key:3735928559 ` + | ||||
| 			`  value:"\x01A\a\f" ` + | ||||
| 			`> ` + | ||||
| 			`others:<` + | ||||
| 			"  weight:58.9  # Atomic weight of Co\n" + | ||||
| 			`  inner:<` + | ||||
| 			`    host:"lesha.mtv" ` + | ||||
| 			`    port:8002 ` + | ||||
| 			`  >` + | ||||
| 			`>`, | ||||
| 		out: &MyMessage{ | ||||
| 			Count: Int32(42), | ||||
| 			Name:  String("Dave"), | ||||
| 			Quote: String(`"I didn't want to go."`), | ||||
| 			Pet:   []string{"bunny", "kitty", "horsey"}, | ||||
| 			Inner: &InnerMessage{ | ||||
| 				Host:      String("footrest.syd"), | ||||
| 				Port:      Int32(7001), | ||||
| 				Connected: Bool(true), | ||||
| 			}, | ||||
| 			Others: []*OtherMessage{ | ||||
| 				{ | ||||
| 					Key:   Int64(3735928559), | ||||
| 					Value: []byte{0x1, 'A', '\a', '\f'}, | ||||
| 				}, | ||||
| 				{ | ||||
| 					Weight: Float32(58.9), | ||||
| 					Inner: &InnerMessage{ | ||||
| 						Host: String("lesha.mtv"), | ||||
| 						Port: Int32(8002), | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}, | ||||
| } | ||||
|  | ||||
| func TestUnmarshalText(t *testing.T) { | ||||
| 	for i, test := range unMarshalTextTests { | ||||
| 		pb := new(MyMessage) | ||||
| 		err := UnmarshalText(test.in, pb) | ||||
| 		if test.err == "" { | ||||
| 			// We don't expect failure. | ||||
| 			if err != nil { | ||||
| 				t.Errorf("Test %d: Unexpected error: %v", i, err) | ||||
| 			} else if !reflect.DeepEqual(pb, test.out) { | ||||
| 				t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v", | ||||
| 					i, pb, test.out) | ||||
| 			} | ||||
| 		} else { | ||||
| 			// We do expect failure. | ||||
| 			if err == nil { | ||||
| 				t.Errorf("Test %d: Didn't get expected error: %v", i, test.err) | ||||
| 			} else if err.Error() != test.err { | ||||
| 				t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v", | ||||
| 					i, err.Error(), test.err) | ||||
| 			} else if _, ok := err.(*RequiredNotSetError); ok && test.out != nil && !reflect.DeepEqual(pb, test.out) { | ||||
| 				t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v", | ||||
| 					i, pb, test.out) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestUnmarshalTextCustomMessage(t *testing.T) { | ||||
| 	msg := &textMessage{} | ||||
| 	if err := UnmarshalText("custom", msg); err != nil { | ||||
| 		t.Errorf("Unexpected error from custom unmarshal: %v", err) | ||||
| 	} | ||||
| 	if UnmarshalText("not custom", msg) == nil { | ||||
| 		t.Errorf("Didn't get expected error from custom unmarshal") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Regression test; this caused a panic. | ||||
| func TestRepeatedEnum(t *testing.T) { | ||||
| 	pb := new(RepeatedEnum) | ||||
| 	if err := UnmarshalText("color: RED", pb); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	exp := &RepeatedEnum{ | ||||
| 		Color: []RepeatedEnum_Color{RepeatedEnum_RED}, | ||||
| 	} | ||||
| 	if !Equal(pb, exp) { | ||||
| 		t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestProto3TextParsing(t *testing.T) { | ||||
| 	m := new(proto3pb.Message) | ||||
| 	const in = `name: "Wallace" true_scotsman: true` | ||||
| 	want := &proto3pb.Message{ | ||||
| 		Name:         "Wallace", | ||||
| 		TrueScotsman: true, | ||||
| 	} | ||||
| 	if err := UnmarshalText(in, m); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if !Equal(m, want) { | ||||
| 		t.Errorf("\n got %v\nwant %v", m, want) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMapParsing(t *testing.T) { | ||||
| 	m := new(MessageWithMap) | ||||
| 	const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` + | ||||
| 		`msg_mapping:<key:-4, value:<f: 2.0>,>` + // separating commas are okay | ||||
| 		`msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value" | ||||
| 		`msg_mapping:<value:<f: 5.0>>` + // omitted key | ||||
| 		`msg_mapping:<key:1>` + // omitted value | ||||
| 		`byte_mapping:<key:true value:"so be it">` + | ||||
| 		`byte_mapping:<>` // omitted key and value | ||||
| 	want := &MessageWithMap{ | ||||
| 		NameMapping: map[int32]string{ | ||||
| 			1:    "Beatles", | ||||
| 			1234: "Feist", | ||||
| 		}, | ||||
| 		MsgMapping: map[int64]*FloatingPoint{ | ||||
| 			-4: {F: Float64(2.0)}, | ||||
| 			-2: {F: Float64(4.0)}, | ||||
| 			0:  {F: Float64(5.0)}, | ||||
| 			1:  nil, | ||||
| 		}, | ||||
| 		ByteMapping: map[bool][]byte{ | ||||
| 			false: nil, | ||||
| 			true:  []byte("so be it"), | ||||
| 		}, | ||||
| 	} | ||||
| 	if err := UnmarshalText(in, m); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if !Equal(m, want) { | ||||
| 		t.Errorf("\n got %v\nwant %v", m, want) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestOneofParsing(t *testing.T) { | ||||
| 	const in = `name:"Shrek"` | ||||
| 	m := new(Communique) | ||||
| 	want := &Communique{Union: &Communique_Name{Name: "Shrek"}} | ||||
| 	if err := UnmarshalText(in, m); err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if !Equal(m, want) { | ||||
| 		t.Errorf("\n got %v\nwant %v", m, want) | ||||
| 	} | ||||
|  | ||||
| 	const inOverwrite = `name:"Shrek" number:42` | ||||
| 	m = new(Communique) | ||||
| 	testErr := "line 1.13: field 'number' would overwrite already parsed oneof 'Union'" | ||||
| 	if err := UnmarshalText(inOverwrite, m); err == nil { | ||||
| 		t.Errorf("TestOneofParsing: Didn't get expected error: %v", testErr) | ||||
| 	} else if err.Error() != testErr { | ||||
| 		t.Errorf("TestOneofParsing: Incorrect error.\nHave: %v\nWant: %v", | ||||
| 			err.Error(), testErr) | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| var benchInput string | ||||
|  | ||||
| func init() { | ||||
| 	benchInput = "count: 4\n" | ||||
| 	for i := 0; i < 1000; i++ { | ||||
| 		benchInput += "pet: \"fido\"\n" | ||||
| 	} | ||||
|  | ||||
| 	// Check it is valid input. | ||||
| 	pb := new(MyMessage) | ||||
| 	err := UnmarshalText(benchInput, pb) | ||||
| 	if err != nil { | ||||
| 		panic("Bad benchmark input: " + err.Error()) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func BenchmarkUnmarshalText(b *testing.B) { | ||||
| 	pb := new(MyMessage) | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		UnmarshalText(benchInput, pb) | ||||
| 	} | ||||
| 	b.SetBytes(int64(len(benchInput))) | ||||
| } | ||||
							
								
								
									
										474
									
								
								vendor/github.com/gogo/protobuf/proto/text_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										474
									
								
								vendor/github.com/gogo/protobuf/proto/text_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,474 +0,0 @@ | ||||
| // Go support for Protocol Buffers - Google's data interchange format | ||||
| // | ||||
| // Copyright 2010 The Go Authors.  All rights reserved. | ||||
| // https://github.com/golang/protobuf | ||||
| // | ||||
| // Redistribution and use in source and binary forms, with or without | ||||
| // modification, are permitted provided that the following conditions are | ||||
| // met: | ||||
| // | ||||
| //     * Redistributions of source code must retain the above copyright | ||||
| // notice, this list of conditions and the following disclaimer. | ||||
| //     * Redistributions in binary form must reproduce the above | ||||
| // copyright notice, this list of conditions and the following disclaimer | ||||
| // in the documentation and/or other materials provided with the | ||||
| // distribution. | ||||
| //     * Neither the name of Google Inc. nor the names of its | ||||
| // contributors may be used to endorse or promote products derived from | ||||
| // this software without specific prior written permission. | ||||
| // | ||||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| // 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 ( | ||||
| 	"bytes" | ||||
| 	"errors" | ||||
| 	"io/ioutil" | ||||
| 	"math" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/gogo/protobuf/proto" | ||||
|  | ||||
| 	proto3pb "github.com/gogo/protobuf/proto/proto3_proto" | ||||
| 	pb "github.com/gogo/protobuf/proto/testdata" | ||||
| ) | ||||
|  | ||||
| // textMessage implements the methods that allow it to marshal and unmarshal | ||||
| // itself as text. | ||||
| type textMessage struct { | ||||
| } | ||||
|  | ||||
| func (*textMessage) MarshalText() ([]byte, error) { | ||||
| 	return []byte("custom"), nil | ||||
| } | ||||
|  | ||||
| func (*textMessage) UnmarshalText(bytes []byte) error { | ||||
| 	if string(bytes) != "custom" { | ||||
| 		return errors.New("expected 'custom'") | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (*textMessage) Reset()         {} | ||||
| func (*textMessage) String() string { return "" } | ||||
| func (*textMessage) ProtoMessage()  {} | ||||
|  | ||||
| func newTestMessage() *pb.MyMessage { | ||||
| 	msg := &pb.MyMessage{ | ||||
| 		Count: proto.Int32(42), | ||||
| 		Name:  proto.String("Dave"), | ||||
| 		Quote: proto.String(`"I didn't want to go."`), | ||||
| 		Pet:   []string{"bunny", "kitty", "horsey"}, | ||||
| 		Inner: &pb.InnerMessage{ | ||||
| 			Host:      proto.String("footrest.syd"), | ||||
| 			Port:      proto.Int32(7001), | ||||
| 			Connected: proto.Bool(true), | ||||
| 		}, | ||||
| 		Others: []*pb.OtherMessage{ | ||||
| 			{ | ||||
| 				Key:   proto.Int64(0xdeadbeef), | ||||
| 				Value: []byte{1, 65, 7, 12}, | ||||
| 			}, | ||||
| 			{ | ||||
| 				Weight: proto.Float32(6.022), | ||||
| 				Inner: &pb.InnerMessage{ | ||||
| 					Host: proto.String("lesha.mtv"), | ||||
| 					Port: proto.Int32(8002), | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		Bikeshed: pb.MyMessage_BLUE.Enum(), | ||||
| 		Somegroup: &pb.MyMessage_SomeGroup{ | ||||
| 			GroupField: proto.Int32(8), | ||||
| 		}, | ||||
| 		// One normally wouldn't do this. | ||||
| 		// This is an undeclared tag 13, as a varint (wire type 0) with value 4. | ||||
| 		XXX_unrecognized: []byte{13<<3 | 0, 4}, | ||||
| 	} | ||||
| 	ext := &pb.Ext{ | ||||
| 		Data: proto.String("Big gobs for big rats"), | ||||
| 	} | ||||
| 	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	greetings := []string{"adg", "easy", "cow"} | ||||
| 	if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
|  | ||||
| 	// Add an unknown extension. We marshal a pb.Ext, and fake the ID. | ||||
| 	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")}) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...) | ||||
| 	proto.SetRawExtension(msg, 201, b) | ||||
|  | ||||
| 	// Extensions can be plain fields, too, so let's test that. | ||||
| 	b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19) | ||||
| 	proto.SetRawExtension(msg, 202, b) | ||||
|  | ||||
| 	return msg | ||||
| } | ||||
|  | ||||
| const text = `count: 42 | ||||
| name: "Dave" | ||||
| quote: "\"I didn't want to go.\"" | ||||
| pet: "bunny" | ||||
| pet: "kitty" | ||||
| pet: "horsey" | ||||
| inner: < | ||||
|   host: "footrest.syd" | ||||
|   port: 7001 | ||||
|   connected: true | ||||
| > | ||||
| others: < | ||||
|   key: 3735928559 | ||||
|   value: "\001A\007\014" | ||||
| > | ||||
| others: < | ||||
|   weight: 6.022 | ||||
|   inner: < | ||||
|     host: "lesha.mtv" | ||||
|     port: 8002 | ||||
|   > | ||||
| > | ||||
| bikeshed: BLUE | ||||
| SomeGroup { | ||||
|   group_field: 8 | ||||
| } | ||||
| /* 2 unknown bytes */ | ||||
| 13: 4 | ||||
| [testdata.Ext.more]: < | ||||
|   data: "Big gobs for big rats" | ||||
| > | ||||
| [testdata.greeting]: "adg" | ||||
| [testdata.greeting]: "easy" | ||||
| [testdata.greeting]: "cow" | ||||
| /* 13 unknown bytes */ | ||||
| 201: "\t3G skiing" | ||||
| /* 3 unknown bytes */ | ||||
| 202: 19 | ||||
| ` | ||||
|  | ||||
| func TestMarshalText(t *testing.T) { | ||||
| 	buf := new(bytes.Buffer) | ||||
| 	if err := proto.MarshalText(buf, newTestMessage()); err != nil { | ||||
| 		t.Fatalf("proto.MarshalText: %v", err) | ||||
| 	} | ||||
| 	s := buf.String() | ||||
| 	if s != text { | ||||
| 		t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMarshalTextCustomMessage(t *testing.T) { | ||||
| 	buf := new(bytes.Buffer) | ||||
| 	if err := proto.MarshalText(buf, &textMessage{}); err != nil { | ||||
| 		t.Fatalf("proto.MarshalText: %v", err) | ||||
| 	} | ||||
| 	s := buf.String() | ||||
| 	if s != "custom" { | ||||
| 		t.Errorf("Got %q, expected %q", s, "custom") | ||||
| 	} | ||||
| } | ||||
| func TestMarshalTextNil(t *testing.T) { | ||||
| 	want := "<nil>" | ||||
| 	tests := []proto.Message{nil, (*pb.MyMessage)(nil)} | ||||
| 	for i, test := range tests { | ||||
| 		buf := new(bytes.Buffer) | ||||
| 		if err := proto.MarshalText(buf, test); err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
| 		if got := buf.String(); got != want { | ||||
| 			t.Errorf("%d: got %q want %q", i, got, want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMarshalTextUnknownEnum(t *testing.T) { | ||||
| 	// The Color enum only specifies values 0-2. | ||||
| 	m := &pb.MyMessage{Bikeshed: pb.MyMessage_Color(3).Enum()} | ||||
| 	got := m.String() | ||||
| 	const want = `bikeshed:3 ` | ||||
| 	if got != want { | ||||
| 		t.Errorf("\n got %q\nwant %q", got, want) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestTextOneof(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		m    proto.Message | ||||
| 		want string | ||||
| 	}{ | ||||
| 		// zero message | ||||
| 		{&pb.Communique{}, ``}, | ||||
| 		// scalar field | ||||
| 		{&pb.Communique{Union: &pb.Communique_Number{Number: 4}}, `number:4`}, | ||||
| 		// message field | ||||
| 		{&pb.Communique{Union: &pb.Communique_Msg{ | ||||
| 			Msg: &pb.Strings{StringField: proto.String("why hello!")}, | ||||
| 		}}, `msg:<string_field:"why hello!" >`}, | ||||
| 		// bad oneof (should not panic) | ||||
| 		{&pb.Communique{Union: &pb.Communique_Msg{Msg: nil}}, `msg:/* nil */`}, | ||||
| 	} | ||||
| 	for _, test := range tests { | ||||
| 		got := strings.TrimSpace(test.m.String()) | ||||
| 		if got != test.want { | ||||
| 			t.Errorf("\n got %s\nwant %s", got, test.want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func BenchmarkMarshalTextBuffered(b *testing.B) { | ||||
| 	buf := new(bytes.Buffer) | ||||
| 	m := newTestMessage() | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		buf.Reset() | ||||
| 		proto.MarshalText(buf, m) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func BenchmarkMarshalTextUnbuffered(b *testing.B) { | ||||
| 	w := ioutil.Discard | ||||
| 	m := newTestMessage() | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		proto.MarshalText(w, m) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func compact(src string) string { | ||||
| 	// s/[ \n]+/ /g; s/ $//; | ||||
| 	dst := make([]byte, len(src)) | ||||
| 	space, comment := false, false | ||||
| 	j := 0 | ||||
| 	for i := 0; i < len(src); i++ { | ||||
| 		if strings.HasPrefix(src[i:], "/*") { | ||||
| 			comment = true | ||||
| 			i++ | ||||
| 			continue | ||||
| 		} | ||||
| 		if comment && strings.HasPrefix(src[i:], "*/") { | ||||
| 			comment = false | ||||
| 			i++ | ||||
| 			continue | ||||
| 		} | ||||
| 		if comment { | ||||
| 			continue | ||||
| 		} | ||||
| 		c := src[i] | ||||
| 		if c == ' ' || c == '\n' { | ||||
| 			space = true | ||||
| 			continue | ||||
| 		} | ||||
| 		if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') { | ||||
| 			space = false | ||||
| 		} | ||||
| 		if c == '{' { | ||||
| 			space = false | ||||
| 		} | ||||
| 		if space { | ||||
| 			dst[j] = ' ' | ||||
| 			j++ | ||||
| 			space = false | ||||
| 		} | ||||
| 		dst[j] = c | ||||
| 		j++ | ||||
| 	} | ||||
| 	if space { | ||||
| 		dst[j] = ' ' | ||||
| 		j++ | ||||
| 	} | ||||
| 	return string(dst[0:j]) | ||||
| } | ||||
|  | ||||
| var compactText = compact(text) | ||||
|  | ||||
| func TestCompactText(t *testing.T) { | ||||
| 	s := proto.CompactTextString(newTestMessage()) | ||||
| 	if s != compactText { | ||||
| 		t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v\n===\n", s, compactText) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestStringEscaping(t *testing.T) { | ||||
| 	testCases := []struct { | ||||
| 		in  *pb.Strings | ||||
| 		out string | ||||
| 	}{ | ||||
| 		{ | ||||
| 			// Test data from C++ test (TextFormatTest.StringEscape). | ||||
| 			// Single divergence: we don't escape apostrophes. | ||||
| 			&pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and  multiple   spaces")}, | ||||
| 			"string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and  multiple   spaces\"\n", | ||||
| 		}, | ||||
| 		{ | ||||
| 			// Test data from the same C++ test. | ||||
| 			&pb.Strings{StringField: proto.String("\350\260\267\346\255\214")}, | ||||
| 			"string_field: \"\\350\\260\\267\\346\\255\\214\"\n", | ||||
| 		}, | ||||
| 		{ | ||||
| 			// Some UTF-8. | ||||
| 			&pb.Strings{StringField: proto.String("\x00\x01\xff\x81")}, | ||||
| 			`string_field: "\000\001\377\201"` + "\n", | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	for i, tc := range testCases { | ||||
| 		var buf bytes.Buffer | ||||
| 		if err := proto.MarshalText(&buf, tc.in); err != nil { | ||||
| 			t.Errorf("proto.MarsalText: %v", err) | ||||
| 			continue | ||||
| 		} | ||||
| 		s := buf.String() | ||||
| 		if s != tc.out { | ||||
| 			t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// Check round-trip. | ||||
| 		pbStrings := new(pb.Strings) | ||||
| 		if err := proto.UnmarshalText(s, pbStrings); err != nil { | ||||
| 			t.Errorf("#%d: UnmarshalText: %v", i, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if !proto.Equal(pbStrings, tc.in) { | ||||
| 			t.Errorf("#%d: Round-trip failed:\nstart: %v\n  end: %v", i, tc.in, pbStrings) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // A limitedWriter accepts some output before it fails. | ||||
| // This is a proxy for something like a nearly-full or imminently-failing disk, | ||||
| // or a network connection that is about to die. | ||||
| type limitedWriter struct { | ||||
| 	b     bytes.Buffer | ||||
| 	limit int | ||||
| } | ||||
|  | ||||
| var outOfSpace = errors.New("proto: insufficient space") | ||||
|  | ||||
| func (w *limitedWriter) Write(p []byte) (n int, err error) { | ||||
| 	var avail = w.limit - w.b.Len() | ||||
| 	if avail <= 0 { | ||||
| 		return 0, outOfSpace | ||||
| 	} | ||||
| 	if len(p) <= avail { | ||||
| 		return w.b.Write(p) | ||||
| 	} | ||||
| 	n, _ = w.b.Write(p[:avail]) | ||||
| 	return n, outOfSpace | ||||
| } | ||||
|  | ||||
| func TestMarshalTextFailing(t *testing.T) { | ||||
| 	// Try lots of different sizes to exercise more error code-paths. | ||||
| 	for lim := 0; lim < len(text); lim++ { | ||||
| 		buf := new(limitedWriter) | ||||
| 		buf.limit = lim | ||||
| 		err := proto.MarshalText(buf, newTestMessage()) | ||||
| 		// We expect a certain error, but also some partial results in the buffer. | ||||
| 		if err != outOfSpace { | ||||
| 			t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", err, outOfSpace) | ||||
| 		} | ||||
| 		s := buf.b.String() | ||||
| 		x := text[:buf.limit] | ||||
| 		if s != x { | ||||
| 			t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, x) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestFloats(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		f    float64 | ||||
| 		want string | ||||
| 	}{ | ||||
| 		{0, "0"}, | ||||
| 		{4.7, "4.7"}, | ||||
| 		{math.Inf(1), "inf"}, | ||||
| 		{math.Inf(-1), "-inf"}, | ||||
| 		{math.NaN(), "nan"}, | ||||
| 	} | ||||
| 	for _, test := range tests { | ||||
| 		msg := &pb.FloatingPoint{F: &test.f} | ||||
| 		got := strings.TrimSpace(msg.String()) | ||||
| 		want := `f:` + test.want | ||||
| 		if got != want { | ||||
| 			t.Errorf("f=%f: got %q, want %q", test.f, got, want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestRepeatedNilText(t *testing.T) { | ||||
| 	m := &pb.MessageList{ | ||||
| 		Message: []*pb.MessageList_Message{ | ||||
| 			nil, | ||||
| 			{ | ||||
| 				Name: proto.String("Horse"), | ||||
| 			}, | ||||
| 			nil, | ||||
| 		}, | ||||
| 	} | ||||
| 	want := `Message <nil> | ||||
| Message { | ||||
|   name: "Horse" | ||||
| } | ||||
| Message <nil> | ||||
| ` | ||||
| 	if s := proto.MarshalTextString(m); s != want { | ||||
| 		t.Errorf(" got: %s\nwant: %s", s, want) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestProto3Text(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		m    proto.Message | ||||
| 		want string | ||||
| 	}{ | ||||
| 		// zero message | ||||
| 		{&proto3pb.Message{}, ``}, | ||||
| 		// zero message except for an empty byte slice | ||||
| 		{&proto3pb.Message{Data: []byte{}}, ``}, | ||||
| 		// trivial case | ||||
| 		{&proto3pb.Message{Name: "Rob", HeightInCm: 175}, `name:"Rob" height_in_cm:175`}, | ||||
| 		// empty map | ||||
| 		{&pb.MessageWithMap{}, ``}, | ||||
| 		// non-empty map; map format is the same as a repeated struct, | ||||
| 		// and they are sorted by key (numerically for numeric keys). | ||||
| 		{ | ||||
| 			&pb.MessageWithMap{NameMapping: map[int32]string{ | ||||
| 				-1:      "Negatory", | ||||
| 				7:       "Lucky", | ||||
| 				1234:    "Feist", | ||||
| 				6345789: "Otis", | ||||
| 			}}, | ||||
| 			`name_mapping:<key:-1 value:"Negatory" > ` + | ||||
| 				`name_mapping:<key:7 value:"Lucky" > ` + | ||||
| 				`name_mapping:<key:1234 value:"Feist" > ` + | ||||
| 				`name_mapping:<key:6345789 value:"Otis" >`, | ||||
| 		}, | ||||
| 		// map with nil value; not well-defined, but we shouldn't crash | ||||
| 		{ | ||||
| 			&pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{7: nil}}, | ||||
| 			`msg_mapping:<key:7 >`, | ||||
| 		}, | ||||
| 	} | ||||
| 	for _, test := range tests { | ||||
| 		got := strings.TrimSpace(test.m.String()) | ||||
| 		if got != test.want { | ||||
| 			t.Errorf("\n got %s\nwant %s", got, test.want) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										31
									
								
								vendor/github.com/gogo/protobuf/protoc-gen-gogo/descriptor/descriptor_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										31
									
								
								vendor/github.com/gogo/protobuf/protoc-gen-gogo/descriptor/descriptor_test.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -1,31 +0,0 @@ | ||||
| package descriptor_test | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
|  | ||||
| 	tpb "github.com/gogo/protobuf/proto/testdata" | ||||
| 	"github.com/gogo/protobuf/protoc-gen-gogo/descriptor" | ||||
| ) | ||||
|  | ||||
| func TestMessage(t *testing.T) { | ||||
| 	var msg *descriptor.DescriptorProto | ||||
| 	fd, md := descriptor.ForMessage(msg) | ||||
| 	if pkg, want := fd.GetPackage(), "google.protobuf"; pkg != want { | ||||
| 		t.Errorf("descriptor.ForMessage(%T).GetPackage() = %q; want %q", msg, pkg, want) | ||||
| 	} | ||||
| 	if name, want := md.GetName(), "DescriptorProto"; name != want { | ||||
| 		t.Fatalf("descriptor.ForMessage(%T).GetName() = %q; want %q", msg, name, want) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func Example_Options() { | ||||
| 	var msg *tpb.MyMessageSet | ||||
| 	_, md := descriptor.ForMessage(msg) | ||||
| 	if md.GetOptions().GetMessageSetWireFormat() { | ||||
| 		fmt.Printf("%v uses option message_set_wire_format.\n", md.GetName()) | ||||
| 	} | ||||
|  | ||||
| 	// Output: | ||||
| 	// MyMessageSet uses option message_set_wire_format. | ||||
| } | ||||
		Reference in New Issue
	
	Block a user