Created
October 25, 2016 13:22
-
-
Save johanbrandhorst/d7e0e92805c9a3b1a8916c5f0c2defbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install protoc | |
# Left as exercise to reader | |
# Install protoc-gen-gogo and friends | |
go get github.com/gogo/protobuf/proto | |
go get github.com/gogo/protobuf/jsonpb | |
go get github.com/gogo/protobuf/protoc-gen-gogo | |
go get github.com/gogo/protobuf/gogoproto | |
protoc ./my/my.proto --gogo_out=:./ -I./ -I.$GOPATH/src |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
import "github.com/gogo/protobuf/gogoproto/gogo.proto"; | |
option (gogoproto.compare_all) = true; | |
option (gogoproto.testgen_all) = true; | |
message Value { | |
oneof type { | |
int64 type_one = 1; | |
uint64 type_two = 2; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by protoc-gen-gogo. | |
// source: data/data.proto | |
// DO NOT EDIT! | |
/* | |
Package data is a generated protocol buffer package. | |
It is generated from these files: | |
data/data.proto | |
It has these top-level messages: | |
Value | |
*/ | |
package data | |
import testing "testing" | |
import math_rand "math/rand" | |
import time "time" | |
import github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" | |
import github_com_gogo_protobuf_jsonpb "github.com/gogo/protobuf/jsonpb" | |
import proto "github.com/gogo/protobuf/proto" | |
import fmt "fmt" | |
import math "math" | |
import _ "github.com/gogo/protobuf/gogoproto" | |
// Reference imports to suppress errors if they are not otherwise used. | |
var _ = proto.Marshal | |
var _ = fmt.Errorf | |
var _ = math.Inf | |
func TestValueProto(t *testing.T) { | |
seed := time.Now().UnixNano() | |
popr := math_rand.New(math_rand.NewSource(seed)) | |
p := NewPopulatedValue(popr, false) | |
dAtA, err := github_com_gogo_protobuf_proto.Marshal(p) | |
if err != nil { | |
t.Fatalf("seed = %d, err = %v", seed, err) | |
} | |
msg := &Value{} | |
if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil { | |
t.Fatalf("seed = %d, err = %v", seed, err) | |
} | |
littlefuzz := make([]byte, len(dAtA)) | |
copy(littlefuzz, dAtA) | |
for i := range dAtA { | |
dAtA[i] = byte(popr.Intn(256)) | |
} | |
if !p.Equal(msg) { | |
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |
} | |
if len(littlefuzz) > 0 { | |
fuzzamount := 100 | |
for i := 0; i < fuzzamount; i++ { | |
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) | |
littlefuzz = append(littlefuzz, byte(popr.Intn(256))) | |
} | |
// shouldn't panic | |
_ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg) | |
} | |
} | |
func TestValueJSON(t *testing.T) { | |
seed := time.Now().UnixNano() | |
popr := math_rand.New(math_rand.NewSource(seed)) | |
p := NewPopulatedValue(popr, true) | |
marshaler := github_com_gogo_protobuf_jsonpb.Marshaler{} | |
jsondata, err := marshaler.MarshalToString(p) | |
if err != nil { | |
t.Fatalf("seed = %d, err = %v", seed, err) | |
} | |
msg := &Value{} | |
err = github_com_gogo_protobuf_jsonpb.UnmarshalString(jsondata, msg) | |
if err != nil { | |
t.Fatalf("seed = %d, err = %v", seed, err) | |
} | |
if !p.Equal(msg) { | |
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) | |
} | |
} | |
func TestValueProtoText(t *testing.T) { | |
seed := time.Now().UnixNano() | |
popr := math_rand.New(math_rand.NewSource(seed)) | |
p := NewPopulatedValue(popr, true) | |
dAtA := github_com_gogo_protobuf_proto.MarshalTextString(p) | |
msg := &Value{} | |
if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil { | |
t.Fatalf("seed = %d, err = %v", seed, err) | |
} | |
if !p.Equal(msg) { | |
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |
} | |
} | |
func TestValueProtoCompactText(t *testing.T) { | |
seed := time.Now().UnixNano() | |
popr := math_rand.New(math_rand.NewSource(seed)) | |
p := NewPopulatedValue(popr, true) | |
dAtA := github_com_gogo_protobuf_proto.CompactTextString(p) | |
msg := &Value{} | |
if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil { | |
t.Fatalf("seed = %d, err = %v", seed, err) | |
} | |
if !p.Equal(msg) { | |
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |
} | |
} | |
func TestValueCompare(t *testing.T) { | |
popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) | |
p := NewPopulatedValue(popr, false) | |
dAtA, err := github_com_gogo_protobuf_proto.Marshal(p) | |
if err != nil { | |
panic(err) | |
} | |
msg := &Value{} | |
if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil { | |
panic(err) | |
} | |
if c := p.Compare(msg); c != 0 { | |
t.Fatalf("%#v !Compare %#v, since %d", msg, p, c) | |
} | |
p2 := NewPopulatedValue(popr, false) | |
c := p.Compare(p2) | |
c2 := p2.Compare(p) | |
if c != (-1 * c2) { | |
t.Errorf("p.Compare(p2) = %d", c) | |
t.Errorf("p2.Compare(p) = %d", c2) | |
t.Errorf("p = %#v", p) | |
t.Errorf("p2 = %#v", p2) | |
} | |
} | |
//These tests are generated by github.com/gogo/protobuf/plugin/testgen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment