| 1 |
// Protocol Buffers for Go with Gadgets |
| 2 |
// |
| 3 |
// Copyright (c) 2016, The GoGo Authors. All rights reserved. |
| 4 |
// http://github.com/gogo/protobuf |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// * Redistributions in binary form must reproduce the above |
| 13 |
// copyright notice, this list of conditions and the following disclaimer |
| 14 |
// in the documentation and/or other materials provided with the |
| 15 |
// distribution. |
| 16 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
package types |
| 30 |
|
| 31 |
import ( |
| 32 |
math_rand "math/rand" |
| 33 |
"testing" |
| 34 |
"time" |
| 35 |
|
| 36 |
"github.com/gogo/protobuf/jsonpb" |
| 37 |
"github.com/gogo/protobuf/proto" |
| 38 |
) |
| 39 |
|
| 40 |
func TestFullCircleProtoToStd(t *testing.T) { |
| 41 |
seed := time.Now().UnixNano() |
| 42 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 43 |
protoMsg := NewPopulatedProtoTypes(popr, true) |
| 44 |
protoData, err := proto.Marshal(protoMsg) |
| 45 |
if err != nil { |
| 46 |
t.Fatal(err) |
| 47 |
} |
| 48 |
stdMsg := &StdTypes{} |
| 49 |
if err2 := proto.Unmarshal(protoData, stdMsg); err2 != nil { |
| 50 |
t.Fatal(err) |
| 51 |
} |
| 52 |
stdData, err := proto.Marshal(stdMsg) |
| 53 |
if err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} |
| 56 |
protoMsgOut := &ProtoTypes{} |
| 57 |
if err3 := proto.Unmarshal(stdData, protoMsgOut); err3 != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
if !protoMsg.Equal(protoMsgOut) { |
| 61 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestJsonFullCircleProtoToStd(t *testing.T) { |
| 66 |
seed := time.Now().UnixNano() |
| 67 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 68 |
protoMsg := NewPopulatedProtoTypes(popr, true) |
| 69 |
j := &jsonpb.Marshaler{} |
| 70 |
protoData, err := j.MarshalToString(protoMsg) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
stdMsg := &StdTypes{} |
| 75 |
if err2 := jsonpb.UnmarshalString(protoData, stdMsg); err2 != nil { |
| 76 |
t.Fatal(err) |
| 77 |
} |
| 78 |
stdData, err := j.MarshalToString(stdMsg) |
| 79 |
if err != nil { |
| 80 |
t.Fatal(err) |
| 81 |
} |
| 82 |
protoMsgOut := &ProtoTypes{} |
| 83 |
if err3 := jsonpb.UnmarshalString(stdData, protoMsgOut); err3 != nil { |
| 84 |
t.Fatal(err) |
| 85 |
} |
| 86 |
if !protoMsg.Equal(protoMsgOut) { |
| 87 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestFullCircleRepProtoToStd(t *testing.T) { |
| 92 |
seed := time.Now().UnixNano() |
| 93 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 94 |
protoMsg := NewPopulatedRepProtoTypes(popr, true) |
| 95 |
protoData, err := proto.Marshal(protoMsg) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
stdMsg := &RepStdTypes{} |
| 100 |
if err2 := proto.Unmarshal(protoData, stdMsg); err2 != nil { |
| 101 |
t.Fatal(err) |
| 102 |
} |
| 103 |
stdData, err := proto.Marshal(stdMsg) |
| 104 |
if err != nil { |
| 105 |
t.Fatal(err) |
| 106 |
} |
| 107 |
protoMsgOut := &RepProtoTypes{} |
| 108 |
if err3 := proto.Unmarshal(stdData, protoMsgOut); err3 != nil { |
| 109 |
t.Fatal(err) |
| 110 |
} |
| 111 |
if !protoMsg.Equal(protoMsgOut) { |
| 112 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func TestJsonFullCircleRepProtoToStd(t *testing.T) { |
| 117 |
seed := time.Now().UnixNano() |
| 118 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 119 |
protoMsg := NewPopulatedRepProtoTypes(popr, true) |
| 120 |
j := &jsonpb.Marshaler{} |
| 121 |
protoData, err := j.MarshalToString(protoMsg) |
| 122 |
if err != nil { |
| 123 |
t.Fatal(err) |
| 124 |
} |
| 125 |
stdMsg := &RepStdTypes{} |
| 126 |
if err2 := jsonpb.UnmarshalString(protoData, stdMsg); err2 != nil { |
| 127 |
t.Fatal(err) |
| 128 |
} |
| 129 |
stdData, err := j.MarshalToString(stdMsg) |
| 130 |
if err != nil { |
| 131 |
t.Fatal(err) |
| 132 |
} |
| 133 |
protoMsgOut := &RepProtoTypes{} |
| 134 |
if err3 := jsonpb.UnmarshalString(stdData, protoMsgOut); err3 != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
if !protoMsg.Equal(protoMsgOut) { |
| 138 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestFullCircleMapProtoToStd(t *testing.T) { |
| 143 |
seed := time.Now().UnixNano() |
| 144 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 145 |
protoMsg := NewPopulatedMapProtoTypes(popr, true) |
| 146 |
protoData, err := proto.Marshal(protoMsg) |
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
stdMsg := &MapStdTypes{} |
| 151 |
if err2 := proto.Unmarshal(protoData, stdMsg); err2 != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
stdData, err := proto.Marshal(stdMsg) |
| 155 |
if err != nil { |
| 156 |
t.Fatal(err) |
| 157 |
} |
| 158 |
protoMsgOut := &MapProtoTypes{} |
| 159 |
if err3 := proto.Unmarshal(stdData, protoMsgOut); err3 != nil { |
| 160 |
t.Fatal(err) |
| 161 |
} |
| 162 |
if !protoMsg.Equal(protoMsgOut) { |
| 163 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestJsonFullCircleMapProtoToStd(t *testing.T) { |
| 168 |
seed := time.Now().UnixNano() |
| 169 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 170 |
protoMsg := NewPopulatedMapProtoTypes(popr, true) |
| 171 |
j := &jsonpb.Marshaler{} |
| 172 |
protoData, err := j.MarshalToString(protoMsg) |
| 173 |
if err != nil { |
| 174 |
t.Fatal(err) |
| 175 |
} |
| 176 |
stdMsg := &MapStdTypes{} |
| 177 |
if err2 := jsonpb.UnmarshalString(protoData, stdMsg); err2 != nil { |
| 178 |
t.Fatal(err) |
| 179 |
} |
| 180 |
stdData, err := j.MarshalToString(stdMsg) |
| 181 |
if err != nil { |
| 182 |
t.Fatal(err) |
| 183 |
} |
| 184 |
protoMsgOut := &MapProtoTypes{} |
| 185 |
if err3 := jsonpb.UnmarshalString(stdData, protoMsgOut); err3 != nil { |
| 186 |
t.Fatal(err) |
| 187 |
} |
| 188 |
if !protoMsg.Equal(protoMsgOut) { |
| 189 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func TestFullCircleOneofProtoToStd(t *testing.T) { |
| 194 |
seed := time.Now().UnixNano() |
| 195 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 196 |
protoMsg := NewPopulatedOneofProtoTypes(popr, true) |
| 197 |
protoData, err := proto.Marshal(protoMsg) |
| 198 |
if err != nil { |
| 199 |
t.Fatal(err) |
| 200 |
} |
| 201 |
stdMsg := &OneofStdTypes{} |
| 202 |
if err2 := proto.Unmarshal(protoData, stdMsg); err2 != nil { |
| 203 |
t.Fatal(err) |
| 204 |
} |
| 205 |
stdData, err := proto.Marshal(stdMsg) |
| 206 |
if err != nil { |
| 207 |
t.Fatal(err) |
| 208 |
} |
| 209 |
protoMsgOut := &OneofProtoTypes{} |
| 210 |
if err3 := proto.Unmarshal(stdData, protoMsgOut); err3 != nil { |
| 211 |
t.Fatal(err) |
| 212 |
} |
| 213 |
if !protoMsg.Equal(protoMsgOut) { |
| 214 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func TestJsonFullCircleOneofProtoToStd(t *testing.T) { |
| 219 |
seed := time.Now().UnixNano() |
| 220 |
popr := math_rand.New(math_rand.NewSource(seed)) |
| 221 |
protoMsg := NewPopulatedOneofProtoTypes(popr, true) |
| 222 |
j := &jsonpb.Marshaler{} |
| 223 |
protoData, err := j.MarshalToString(protoMsg) |
| 224 |
if err != nil { |
| 225 |
t.Fatal(err) |
| 226 |
} |
| 227 |
stdMsg := &OneofStdTypes{} |
| 228 |
if err2 := jsonpb.UnmarshalString(protoData, stdMsg); err2 != nil { |
| 229 |
t.Fatal(err) |
| 230 |
} |
| 231 |
stdData, err := j.MarshalToString(stdMsg) |
| 232 |
if err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
protoMsgOut := &OneofProtoTypes{} |
| 236 |
if err3 := jsonpb.UnmarshalString(stdData, protoMsgOut); err3 != nil { |
| 237 |
t.Fatal(err) |
| 238 |
} |
| 239 |
if !protoMsg.Equal(protoMsgOut) { |
| 240 |
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut) |
| 241 |
} |
| 242 |
} |