Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 5 | package prototext_test |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 8 | "bytes" |
Herbie Ong | a94f78c | 2019-01-03 15:39:58 -0800 | [diff] [blame] | 9 | "encoding/hex" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 10 | "math" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 11 | "testing" |
| 12 | |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 13 | "github.com/google/go-cmp/cmp" |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 14 | "google.golang.org/protobuf/encoding/prototext" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 15 | "google.golang.org/protobuf/internal/detrand" |
| 16 | "google.golang.org/protobuf/internal/encoding/pack" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 17 | pimpl "google.golang.org/protobuf/internal/impl" |
| 18 | "google.golang.org/protobuf/internal/scalar" |
| 19 | "google.golang.org/protobuf/proto" |
| 20 | preg "google.golang.org/protobuf/reflect/protoregistry" |
| 21 | "google.golang.org/protobuf/runtime/protoiface" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 22 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 23 | "google.golang.org/protobuf/encoding/testprotos/pb2" |
| 24 | "google.golang.org/protobuf/encoding/testprotos/pb3" |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 25 | "google.golang.org/protobuf/types/known/anypb" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | // Disable detrand to enable direct comparisons on outputs. |
| 30 | detrand.Disable() |
| 31 | } |
| 32 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 33 | func pb2Enum(i int32) *pb2.Enum { |
| 34 | p := new(pb2.Enum) |
| 35 | *p = pb2.Enum(i) |
| 36 | return p |
| 37 | } |
| 38 | |
| 39 | func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum { |
| 40 | p := new(pb2.Enums_NestedEnum) |
| 41 | *p = pb2.Enums_NestedEnum(i) |
| 42 | return p |
| 43 | } |
| 44 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 45 | // TODO: Use proto.SetExtension when available. |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 46 | func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 47 | m.ProtoReflect().Set(xd.Type, xd.Type.ValueOf(val)) |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Herbie Ong | a94f78c | 2019-01-03 15:39:58 -0800 | [diff] [blame] | 50 | // dhex decodes a hex-string and returns the bytes and panics if s is invalid. |
| 51 | func dhex(s string) []byte { |
| 52 | b, err := hex.DecodeString(s) |
| 53 | if err != nil { |
| 54 | panic(err) |
| 55 | } |
| 56 | return b |
| 57 | } |
| 58 | |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 59 | func TestMarshal(t *testing.T) { |
| 60 | tests := []struct { |
| 61 | desc string |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 62 | mo prototext.MarshalOptions |
Herbie Ong | 7065195 | 2018-12-13 14:19:50 -0800 | [diff] [blame] | 63 | input proto.Message |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 64 | want string |
Herbie Ong | 300cff0 | 2019-03-20 18:05:16 -0700 | [diff] [blame] | 65 | wantErr bool // TODO: Verify error message content. |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 66 | }{{ |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 67 | desc: "proto2 optional scalars not set", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 68 | input: &pb2.Scalars{}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 69 | want: "\n", |
| 70 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 71 | desc: "proto3 scalars not set", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 72 | input: &pb3.Scalars{}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 73 | want: "\n", |
| 74 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 75 | desc: "proto2 optional scalars set to zero values", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 76 | input: &pb2.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 77 | OptBool: scalar.Bool(false), |
| 78 | OptInt32: scalar.Int32(0), |
| 79 | OptInt64: scalar.Int64(0), |
| 80 | OptUint32: scalar.Uint32(0), |
| 81 | OptUint64: scalar.Uint64(0), |
| 82 | OptSint32: scalar.Int32(0), |
| 83 | OptSint64: scalar.Int64(0), |
| 84 | OptFixed32: scalar.Uint32(0), |
| 85 | OptFixed64: scalar.Uint64(0), |
| 86 | OptSfixed32: scalar.Int32(0), |
| 87 | OptSfixed64: scalar.Int64(0), |
| 88 | OptFloat: scalar.Float32(0), |
| 89 | OptDouble: scalar.Float64(0), |
| 90 | OptBytes: []byte{}, |
| 91 | OptString: scalar.String(""), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 92 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 93 | want: `opt_bool: false |
| 94 | opt_int32: 0 |
| 95 | opt_int64: 0 |
| 96 | opt_uint32: 0 |
| 97 | opt_uint64: 0 |
| 98 | opt_sint32: 0 |
| 99 | opt_sint64: 0 |
| 100 | opt_fixed32: 0 |
| 101 | opt_fixed64: 0 |
| 102 | opt_sfixed32: 0 |
| 103 | opt_sfixed64: 0 |
| 104 | opt_float: 0 |
| 105 | opt_double: 0 |
| 106 | opt_bytes: "" |
| 107 | opt_string: "" |
| 108 | `, |
| 109 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 110 | desc: "proto3 scalars set to zero values", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 111 | input: &pb3.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 112 | SBool: false, |
| 113 | SInt32: 0, |
| 114 | SInt64: 0, |
| 115 | SUint32: 0, |
| 116 | SUint64: 0, |
| 117 | SSint32: 0, |
| 118 | SSint64: 0, |
| 119 | SFixed32: 0, |
| 120 | SFixed64: 0, |
| 121 | SSfixed32: 0, |
| 122 | SSfixed64: 0, |
| 123 | SFloat: 0, |
| 124 | SDouble: 0, |
| 125 | SBytes: []byte{}, |
| 126 | SString: "", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 127 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 128 | want: "\n", |
| 129 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 130 | desc: "proto2 optional scalars set to some values", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 131 | input: &pb2.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 132 | OptBool: scalar.Bool(true), |
| 133 | OptInt32: scalar.Int32(0xff), |
| 134 | OptInt64: scalar.Int64(0xdeadbeef), |
| 135 | OptUint32: scalar.Uint32(47), |
| 136 | OptUint64: scalar.Uint64(0xdeadbeef), |
| 137 | OptSint32: scalar.Int32(-1001), |
| 138 | OptSint64: scalar.Int64(-0xffff), |
| 139 | OptFixed64: scalar.Uint64(64), |
| 140 | OptSfixed32: scalar.Int32(-32), |
Herbie Ong | 84f0960 | 2019-01-17 19:31:47 -0800 | [diff] [blame] | 141 | OptFloat: scalar.Float32(1.02), |
| 142 | OptDouble: scalar.Float64(1.0199999809265137), |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 143 | OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"), |
| 144 | OptString: scalar.String("è°·æŒ"), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 145 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 146 | want: `opt_bool: true |
| 147 | opt_int32: 255 |
| 148 | opt_int64: 3735928559 |
| 149 | opt_uint32: 47 |
| 150 | opt_uint64: 3735928559 |
| 151 | opt_sint32: -1001 |
| 152 | opt_sint64: -65535 |
| 153 | opt_fixed64: 64 |
| 154 | opt_sfixed32: -32 |
Herbie Ong | 84f0960 | 2019-01-17 19:31:47 -0800 | [diff] [blame] | 155 | opt_float: 1.02 |
| 156 | opt_double: 1.0199999809265137 |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 157 | opt_bytes: "è°·æŒ" |
| 158 | opt_string: "è°·æŒ" |
| 159 | `, |
| 160 | }, { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 161 | desc: "string with invalid UTF-8", |
| 162 | input: &pb3.Scalars{ |
| 163 | SString: "abc\xff", |
| 164 | }, |
| 165 | want: `s_string: "abc\xff" |
| 166 | `, |
| 167 | wantErr: true, |
| 168 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 169 | desc: "float nan", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 170 | input: &pb3.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 171 | SFloat: float32(math.NaN()), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 172 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 173 | want: "s_float: nan\n", |
| 174 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 175 | desc: "float positive infinity", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 176 | input: &pb3.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 177 | SFloat: float32(math.Inf(1)), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 178 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 179 | want: "s_float: inf\n", |
| 180 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 181 | desc: "float negative infinity", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 182 | input: &pb3.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 183 | SFloat: float32(math.Inf(-1)), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 184 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 185 | want: "s_float: -inf\n", |
| 186 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 187 | desc: "double nan", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 188 | input: &pb3.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 189 | SDouble: math.NaN(), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 190 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 191 | want: "s_double: nan\n", |
| 192 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 193 | desc: "double positive infinity", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 194 | input: &pb3.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 195 | SDouble: math.Inf(1), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 196 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 197 | want: "s_double: inf\n", |
| 198 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 199 | desc: "double negative infinity", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 200 | input: &pb3.Scalars{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 201 | SDouble: math.Inf(-1), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 202 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 203 | want: "s_double: -inf\n", |
| 204 | }, { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 205 | desc: "proto2 enum not set", |
| 206 | input: &pb2.Enums{}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 207 | want: "\n", |
| 208 | }, { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 209 | desc: "proto2 enum set to zero value", |
| 210 | input: &pb2.Enums{ |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 211 | OptEnum: pb2Enum(0), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 212 | OptNestedEnum: pb2Enums_NestedEnum(0), |
| 213 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 214 | want: `opt_enum: 0 |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 215 | opt_nested_enum: 0 |
| 216 | `, |
| 217 | }, { |
| 218 | desc: "proto2 enum", |
| 219 | input: &pb2.Enums{ |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 220 | OptEnum: pb2.Enum_ONE.Enum(), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 221 | OptNestedEnum: pb2.Enums_UNO.Enum(), |
| 222 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 223 | want: `opt_enum: ONE |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 224 | opt_nested_enum: UNO |
| 225 | `, |
| 226 | }, { |
| 227 | desc: "proto2 enum set to numeric values", |
| 228 | input: &pb2.Enums{ |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 229 | OptEnum: pb2Enum(2), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 230 | OptNestedEnum: pb2Enums_NestedEnum(2), |
| 231 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 232 | want: `opt_enum: TWO |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 233 | opt_nested_enum: DOS |
| 234 | `, |
| 235 | }, { |
| 236 | desc: "proto2 enum set to unnamed numeric values", |
| 237 | input: &pb2.Enums{ |
| 238 | OptEnum: pb2Enum(101), |
| 239 | OptNestedEnum: pb2Enums_NestedEnum(-101), |
| 240 | }, |
| 241 | want: `opt_enum: 101 |
| 242 | opt_nested_enum: -101 |
| 243 | `, |
| 244 | }, { |
| 245 | desc: "proto3 enum not set", |
| 246 | input: &pb3.Enums{}, |
| 247 | want: "\n", |
| 248 | }, { |
| 249 | desc: "proto3 enum set to zero value", |
| 250 | input: &pb3.Enums{ |
| 251 | SEnum: pb3.Enum_ZERO, |
| 252 | SNestedEnum: pb3.Enums_CERO, |
| 253 | }, |
| 254 | want: "\n", |
| 255 | }, { |
| 256 | desc: "proto3 enum", |
| 257 | input: &pb3.Enums{ |
| 258 | SEnum: pb3.Enum_ONE, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 259 | SNestedEnum: pb3.Enums_UNO, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 260 | }, |
| 261 | want: `s_enum: ONE |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 262 | s_nested_enum: UNO |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 263 | `, |
| 264 | }, { |
| 265 | desc: "proto3 enum set to numeric values", |
| 266 | input: &pb3.Enums{ |
| 267 | SEnum: 2, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 268 | SNestedEnum: 2, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 269 | }, |
| 270 | want: `s_enum: TWO |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 271 | s_nested_enum: DOS |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 272 | `, |
| 273 | }, { |
| 274 | desc: "proto3 enum set to unnamed numeric values", |
| 275 | input: &pb3.Enums{ |
| 276 | SEnum: -47, |
| 277 | SNestedEnum: 47, |
| 278 | }, |
| 279 | want: `s_enum: -47 |
| 280 | s_nested_enum: 47 |
| 281 | `, |
| 282 | }, { |
| 283 | desc: "proto2 nested message not set", |
| 284 | input: &pb2.Nests{}, |
| 285 | want: "\n", |
| 286 | }, { |
| 287 | desc: "proto2 nested message set to empty", |
| 288 | input: &pb2.Nests{ |
| 289 | OptNested: &pb2.Nested{}, |
| 290 | Optgroup: &pb2.Nests_OptGroup{}, |
| 291 | }, |
| 292 | want: `opt_nested: {} |
Herbie Ong | 0dcfb9a | 2019-01-14 15:32:26 -0800 | [diff] [blame] | 293 | OptGroup: {} |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 294 | `, |
| 295 | }, { |
| 296 | desc: "proto2 nested messages", |
| 297 | input: &pb2.Nests{ |
| 298 | OptNested: &pb2.Nested{ |
| 299 | OptString: scalar.String("nested message"), |
| 300 | OptNested: &pb2.Nested{ |
| 301 | OptString: scalar.String("another nested message"), |
| 302 | }, |
| 303 | }, |
| 304 | }, |
| 305 | want: `opt_nested: { |
| 306 | opt_string: "nested message" |
| 307 | opt_nested: { |
| 308 | opt_string: "another nested message" |
| 309 | } |
| 310 | } |
| 311 | `, |
| 312 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 313 | desc: "proto2 groups", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 314 | input: &pb2.Nests{ |
| 315 | Optgroup: &pb2.Nests_OptGroup{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 316 | OptString: scalar.String("inside a group"), |
| 317 | OptNested: &pb2.Nested{ |
| 318 | OptString: scalar.String("nested message inside a group"), |
| 319 | }, |
| 320 | Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{ |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 321 | OptFixed32: scalar.Uint32(47), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 322 | }, |
| 323 | }, |
| 324 | }, |
Herbie Ong | 0dcfb9a | 2019-01-14 15:32:26 -0800 | [diff] [blame] | 325 | want: `OptGroup: { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 326 | opt_string: "inside a group" |
| 327 | opt_nested: { |
| 328 | opt_string: "nested message inside a group" |
| 329 | } |
Herbie Ong | 0dcfb9a | 2019-01-14 15:32:26 -0800 | [diff] [blame] | 330 | OptNestedGroup: { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 331 | opt_fixed32: 47 |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | `, |
| 335 | }, { |
| 336 | desc: "proto3 nested message not set", |
| 337 | input: &pb3.Nests{}, |
| 338 | want: "\n", |
| 339 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 340 | desc: "proto3 nested message set to empty", |
| 341 | input: &pb3.Nests{ |
| 342 | SNested: &pb3.Nested{}, |
| 343 | }, |
| 344 | want: "s_nested: {}\n", |
| 345 | }, { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 346 | desc: "proto3 nested message", |
| 347 | input: &pb3.Nests{ |
| 348 | SNested: &pb3.Nested{ |
| 349 | SString: "nested message", |
| 350 | SNested: &pb3.Nested{ |
| 351 | SString: "another nested message", |
| 352 | }, |
| 353 | }, |
| 354 | }, |
| 355 | want: `s_nested: { |
| 356 | s_string: "nested message" |
| 357 | s_nested: { |
| 358 | s_string: "another nested message" |
| 359 | } |
| 360 | } |
| 361 | `, |
| 362 | }, { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 363 | desc: "proto3 nested message contains invalid UTF-8", |
| 364 | input: &pb3.Nests{ |
| 365 | SNested: &pb3.Nested{ |
| 366 | SString: "abc\xff", |
| 367 | }, |
| 368 | }, |
| 369 | want: `s_nested: { |
| 370 | s_string: "abc\xff" |
| 371 | } |
| 372 | `, |
| 373 | wantErr: true, |
| 374 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 375 | desc: "oneof not set", |
| 376 | input: &pb3.Oneofs{}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 377 | want: "\n", |
| 378 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 379 | desc: "oneof set to empty string", |
| 380 | input: &pb3.Oneofs{ |
| 381 | Union: &pb3.Oneofs_OneofString{}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 382 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 383 | want: `oneof_string: "" |
| 384 | `, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 385 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 386 | desc: "oneof set to string", |
| 387 | input: &pb3.Oneofs{ |
| 388 | Union: &pb3.Oneofs_OneofString{ |
| 389 | OneofString: "hello", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 390 | }, |
| 391 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 392 | want: `oneof_string: "hello" |
| 393 | `, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 394 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 395 | desc: "oneof set to enum", |
| 396 | input: &pb3.Oneofs{ |
| 397 | Union: &pb3.Oneofs_OneofEnum{ |
| 398 | OneofEnum: pb3.Enum_ZERO, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 399 | }, |
| 400 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 401 | want: `oneof_enum: ZERO |
| 402 | `, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 403 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 404 | desc: "oneof set to empty message", |
| 405 | input: &pb3.Oneofs{ |
| 406 | Union: &pb3.Oneofs_OneofNested{ |
| 407 | OneofNested: &pb3.Nested{}, |
| 408 | }, |
| 409 | }, |
| 410 | want: "oneof_nested: {}\n", |
| 411 | }, { |
| 412 | desc: "oneof set to message", |
| 413 | input: &pb3.Oneofs{ |
| 414 | Union: &pb3.Oneofs_OneofNested{ |
| 415 | OneofNested: &pb3.Nested{ |
| 416 | SString: "nested message", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 417 | }, |
| 418 | }, |
| 419 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 420 | want: `oneof_nested: { |
| 421 | s_string: "nested message" |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 422 | } |
| 423 | `, |
| 424 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 425 | desc: "repeated fields not set", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 426 | input: &pb2.Repeats{}, |
| 427 | want: "\n", |
| 428 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 429 | desc: "repeated fields set to empty slices", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 430 | input: &pb2.Repeats{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 431 | RptBool: []bool{}, |
| 432 | RptInt32: []int32{}, |
| 433 | RptInt64: []int64{}, |
| 434 | RptUint32: []uint32{}, |
| 435 | RptUint64: []uint64{}, |
| 436 | RptFloat: []float32{}, |
| 437 | RptDouble: []float64{}, |
| 438 | RptBytes: [][]byte{}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 439 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 440 | want: "\n", |
| 441 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 442 | desc: "repeated fields set to some values", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 443 | input: &pb2.Repeats{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 444 | RptBool: []bool{true, false, true, true}, |
| 445 | RptInt32: []int32{1, 6, 0, 0}, |
| 446 | RptInt64: []int64{-64, 47}, |
| 447 | RptUint32: []uint32{0xff, 0xffff}, |
| 448 | RptUint64: []uint64{0xdeadbeef}, |
Herbie Ong | 84f0960 | 2019-01-17 19:31:47 -0800 | [diff] [blame] | 449 | RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 450 | RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308}, |
| 451 | RptString: []string{"hello", "世界"}, |
| 452 | RptBytes: [][]byte{ |
| 453 | []byte("hello"), |
| 454 | []byte("\xe4\xb8\x96\xe7\x95\x8c"), |
| 455 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 456 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 457 | want: `rpt_bool: true |
| 458 | rpt_bool: false |
| 459 | rpt_bool: true |
| 460 | rpt_bool: true |
| 461 | rpt_int32: 1 |
| 462 | rpt_int32: 6 |
| 463 | rpt_int32: 0 |
| 464 | rpt_int32: 0 |
| 465 | rpt_int64: -64 |
| 466 | rpt_int64: 47 |
| 467 | rpt_uint32: 255 |
| 468 | rpt_uint32: 65535 |
| 469 | rpt_uint64: 3735928559 |
Herbie Ong | 84f0960 | 2019-01-17 19:31:47 -0800 | [diff] [blame] | 470 | rpt_float: nan |
| 471 | rpt_float: inf |
| 472 | rpt_float: -inf |
| 473 | rpt_float: 1.034 |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 474 | rpt_double: nan |
| 475 | rpt_double: inf |
| 476 | rpt_double: -inf |
| 477 | rpt_double: 1.23e-308 |
| 478 | rpt_string: "hello" |
| 479 | rpt_string: "世界" |
| 480 | rpt_bytes: "hello" |
| 481 | rpt_bytes: "世界" |
| 482 | `, |
| 483 | }, { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 484 | desc: "repeated contains invalid UTF-8", |
| 485 | input: &pb2.Repeats{ |
| 486 | RptString: []string{"abc\xff"}, |
| 487 | }, |
| 488 | want: `rpt_string: "abc\xff" |
| 489 | `, |
| 490 | wantErr: true, |
| 491 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 492 | desc: "repeated enums", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 493 | input: &pb2.Enums{ |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 494 | RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 495 | RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 496 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 497 | want: `rpt_enum: ONE |
| 498 | rpt_enum: TWO |
| 499 | rpt_enum: TEN |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 500 | rpt_enum: 42 |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 501 | rpt_nested_enum: DOS |
| 502 | rpt_nested_enum: 47 |
| 503 | rpt_nested_enum: DIEZ |
| 504 | `, |
| 505 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 506 | desc: "repeated messages set to empty", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 507 | input: &pb2.Nests{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 508 | RptNested: []*pb2.Nested{}, |
| 509 | Rptgroup: []*pb2.Nests_RptGroup{}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 510 | }, |
| 511 | want: "\n", |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 512 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 513 | desc: "repeated messages", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 514 | input: &pb2.Nests{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 515 | RptNested: []*pb2.Nested{ |
| 516 | { |
| 517 | OptString: scalar.String("repeat nested one"), |
| 518 | }, |
| 519 | { |
| 520 | OptString: scalar.String("repeat nested two"), |
| 521 | OptNested: &pb2.Nested{ |
| 522 | OptString: scalar.String("inside repeat nested two"), |
| 523 | }, |
| 524 | }, |
| 525 | {}, |
| 526 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 527 | }, |
| 528 | want: `rpt_nested: { |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 529 | opt_string: "repeat nested one" |
| 530 | } |
| 531 | rpt_nested: { |
| 532 | opt_string: "repeat nested two" |
| 533 | opt_nested: { |
| 534 | opt_string: "inside repeat nested two" |
| 535 | } |
| 536 | } |
| 537 | rpt_nested: {} |
| 538 | `, |
| 539 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 540 | desc: "repeated messages contains nil value", |
Herbie Ong | f5db2df | 2019-02-07 20:17:45 -0800 | [diff] [blame] | 541 | input: &pb2.Nests{ |
| 542 | RptNested: []*pb2.Nested{nil, {}}, |
| 543 | }, |
| 544 | want: `rpt_nested: {} |
| 545 | rpt_nested: {} |
| 546 | `, |
| 547 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 548 | desc: "repeated groups", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 549 | input: &pb2.Nests{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 550 | Rptgroup: []*pb2.Nests_RptGroup{ |
| 551 | { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 552 | RptString: []string{"hello", "world"}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 553 | }, |
| 554 | {}, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 555 | nil, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 556 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 557 | }, |
Herbie Ong | de7313b | 2019-01-14 19:26:50 -0800 | [diff] [blame] | 558 | want: `RptGroup: { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 559 | rpt_string: "hello" |
| 560 | rpt_string: "world" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 561 | } |
Herbie Ong | de7313b | 2019-01-14 19:26:50 -0800 | [diff] [blame] | 562 | RptGroup: {} |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 563 | RptGroup: {} |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 564 | `, |
| 565 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 566 | desc: "map fields not set", |
| 567 | input: &pb3.Maps{}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 568 | want: "\n", |
| 569 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 570 | desc: "map fields set to empty", |
| 571 | input: &pb3.Maps{ |
| 572 | Int32ToStr: map[int32]string{}, |
| 573 | BoolToUint32: map[bool]uint32{}, |
| 574 | Uint64ToEnum: map[uint64]pb3.Enum{}, |
| 575 | StrToNested: map[string]*pb3.Nested{}, |
| 576 | StrToOneofs: map[string]*pb3.Oneofs{}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 577 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 578 | want: "\n", |
| 579 | }, { |
| 580 | desc: "map fields 1", |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 581 | input: &pb3.Maps{ |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 582 | Int32ToStr: map[int32]string{ |
| 583 | -101: "-101", |
| 584 | 0xff: "0xff", |
| 585 | 0: "zero", |
| 586 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 587 | BoolToUint32: map[bool]uint32{ |
| 588 | true: 42, |
| 589 | false: 101, |
| 590 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 591 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 592 | want: `int32_to_str: { |
| 593 | key: -101 |
| 594 | value: "-101" |
| 595 | } |
| 596 | int32_to_str: { |
| 597 | key: 0 |
| 598 | value: "zero" |
| 599 | } |
| 600 | int32_to_str: { |
| 601 | key: 255 |
| 602 | value: "0xff" |
| 603 | } |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 604 | bool_to_uint32: { |
| 605 | key: false |
| 606 | value: 101 |
| 607 | } |
| 608 | bool_to_uint32: { |
| 609 | key: true |
| 610 | value: 42 |
| 611 | } |
| 612 | `, |
| 613 | }, { |
| 614 | desc: "map fields 2", |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 615 | input: &pb3.Maps{ |
| 616 | Uint64ToEnum: map[uint64]pb3.Enum{ |
| 617 | 1: pb3.Enum_ONE, |
| 618 | 2: pb3.Enum_TWO, |
| 619 | 10: pb3.Enum_TEN, |
| 620 | 47: 47, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 621 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 622 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 623 | want: `uint64_to_enum: { |
| 624 | key: 1 |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 625 | value: ONE |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 626 | } |
| 627 | uint64_to_enum: { |
| 628 | key: 2 |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 629 | value: TWO |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 630 | } |
| 631 | uint64_to_enum: { |
| 632 | key: 10 |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 633 | value: TEN |
| 634 | } |
| 635 | uint64_to_enum: { |
| 636 | key: 47 |
| 637 | value: 47 |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 638 | } |
| 639 | `, |
| 640 | }, { |
| 641 | desc: "map fields 3", |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 642 | input: &pb3.Maps{ |
| 643 | StrToNested: map[string]*pb3.Nested{ |
| 644 | "nested": &pb3.Nested{ |
| 645 | SString: "nested in a map", |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 646 | }, |
| 647 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 648 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 649 | want: `str_to_nested: { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 650 | key: "nested" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 651 | value: { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 652 | s_string: "nested in a map" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | `, |
| 656 | }, { |
| 657 | desc: "map fields 4", |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 658 | input: &pb3.Maps{ |
| 659 | StrToOneofs: map[string]*pb3.Oneofs{ |
| 660 | "string": &pb3.Oneofs{ |
| 661 | Union: &pb3.Oneofs_OneofString{ |
| 662 | OneofString: "hello", |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 663 | }, |
| 664 | }, |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 665 | "nested": &pb3.Oneofs{ |
| 666 | Union: &pb3.Oneofs_OneofNested{ |
| 667 | OneofNested: &pb3.Nested{ |
| 668 | SString: "nested oneof in map field value", |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 669 | }, |
| 670 | }, |
| 671 | }, |
| 672 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 673 | }, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 674 | want: `str_to_oneofs: { |
| 675 | key: "nested" |
| 676 | value: { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 677 | oneof_nested: { |
| 678 | s_string: "nested oneof in map field value" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
| 682 | str_to_oneofs: { |
| 683 | key: "string" |
| 684 | value: { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 685 | oneof_string: "hello" |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | `, |
| 689 | }, { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 690 | desc: "map field value contains invalid UTF-8", |
| 691 | input: &pb3.Maps{ |
| 692 | Int32ToStr: map[int32]string{ |
| 693 | 101: "abc\xff", |
| 694 | }, |
| 695 | }, |
| 696 | want: `int32_to_str: { |
| 697 | key: 101 |
| 698 | value: "abc\xff" |
| 699 | } |
| 700 | `, |
| 701 | wantErr: true, |
| 702 | }, { |
| 703 | desc: "map field key contains invalid UTF-8", |
| 704 | input: &pb3.Maps{ |
| 705 | StrToNested: map[string]*pb3.Nested{ |
| 706 | "abc\xff": {}, |
| 707 | }, |
| 708 | }, |
| 709 | want: `str_to_nested: { |
| 710 | key: "abc\xff" |
| 711 | value: {} |
| 712 | } |
| 713 | `, |
| 714 | wantErr: true, |
| 715 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 716 | desc: "map field contains nil value", |
| 717 | input: &pb3.Maps{ |
| 718 | StrToNested: map[string]*pb3.Nested{ |
Herbie Ong | f5db2df | 2019-02-07 20:17:45 -0800 | [diff] [blame] | 719 | "nil": nil, |
| 720 | }, |
| 721 | }, |
| 722 | want: `str_to_nested: { |
| 723 | key: "nil" |
| 724 | value: {} |
| 725 | } |
| 726 | `, |
| 727 | }, { |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 728 | desc: "required fields not set", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 729 | input: &pb2.Requireds{}, |
| 730 | want: "\n", |
| 731 | wantErr: true, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 732 | }, { |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 733 | desc: "required fields partially set", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 734 | input: &pb2.Requireds{ |
| 735 | ReqBool: scalar.Bool(false), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 736 | ReqSfixed64: scalar.Int64(0xbeefcafe), |
| 737 | ReqDouble: scalar.Float64(math.NaN()), |
| 738 | ReqString: scalar.String("hello"), |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 739 | ReqEnum: pb2.Enum_ONE.Enum(), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 740 | }, |
| 741 | want: `req_bool: false |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 742 | req_sfixed64: 3203386110 |
| 743 | req_double: nan |
| 744 | req_string: "hello" |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 745 | req_enum: ONE |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 746 | `, |
| 747 | wantErr: true, |
| 748 | }, { |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 749 | desc: "required fields not set with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 750 | mo: prototext.MarshalOptions{AllowPartial: true}, |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 751 | input: &pb2.Requireds{ |
| 752 | ReqBool: scalar.Bool(false), |
| 753 | ReqSfixed64: scalar.Int64(0xbeefcafe), |
| 754 | ReqDouble: scalar.Float64(math.NaN()), |
| 755 | ReqString: scalar.String("hello"), |
| 756 | ReqEnum: pb2.Enum_ONE.Enum(), |
| 757 | }, |
| 758 | want: `req_bool: false |
| 759 | req_sfixed64: 3203386110 |
| 760 | req_double: nan |
| 761 | req_string: "hello" |
| 762 | req_enum: ONE |
| 763 | `, |
| 764 | }, { |
| 765 | desc: "required fields all set", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 766 | input: &pb2.Requireds{ |
| 767 | ReqBool: scalar.Bool(false), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 768 | ReqSfixed64: scalar.Int64(0), |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 769 | ReqDouble: scalar.Float64(1.23), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 770 | ReqString: scalar.String(""), |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 771 | ReqEnum: pb2.Enum_ONE.Enum(), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 772 | ReqNested: &pb2.Nested{}, |
| 773 | }, |
| 774 | want: `req_bool: false |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 775 | req_sfixed64: 0 |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 776 | req_double: 1.23 |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 777 | req_string: "" |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 778 | req_enum: ONE |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 779 | req_nested: {} |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 780 | `, |
| 781 | }, { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 782 | desc: "indirect required field", |
| 783 | input: &pb2.IndirectRequired{ |
| 784 | OptNested: &pb2.NestedWithRequired{}, |
| 785 | }, |
| 786 | want: "opt_nested: {}\n", |
| 787 | wantErr: true, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 788 | }, { |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 789 | desc: "indirect required field with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 790 | mo: prototext.MarshalOptions{AllowPartial: true}, |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 791 | input: &pb2.IndirectRequired{ |
| 792 | OptNested: &pb2.NestedWithRequired{}, |
| 793 | }, |
| 794 | want: "opt_nested: {}\n", |
| 795 | }, { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 796 | desc: "indirect required field in empty repeated", |
| 797 | input: &pb2.IndirectRequired{ |
| 798 | RptNested: []*pb2.NestedWithRequired{}, |
| 799 | }, |
| 800 | want: "\n", |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 801 | }, { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 802 | desc: "indirect required field in repeated", |
| 803 | input: &pb2.IndirectRequired{ |
| 804 | RptNested: []*pb2.NestedWithRequired{ |
| 805 | &pb2.NestedWithRequired{}, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 806 | }, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 807 | }, |
| 808 | want: "rpt_nested: {}\n", |
| 809 | wantErr: true, |
| 810 | }, { |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 811 | desc: "indirect required field in repeated with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 812 | mo: prototext.MarshalOptions{AllowPartial: true}, |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 813 | input: &pb2.IndirectRequired{ |
| 814 | RptNested: []*pb2.NestedWithRequired{ |
| 815 | &pb2.NestedWithRequired{}, |
| 816 | }, |
| 817 | }, |
| 818 | want: "rpt_nested: {}\n", |
| 819 | }, { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 820 | desc: "indirect required field in empty map", |
| 821 | input: &pb2.IndirectRequired{ |
| 822 | StrToNested: map[string]*pb2.NestedWithRequired{}, |
| 823 | }, |
| 824 | want: "\n", |
| 825 | }, { |
| 826 | desc: "indirect required field in map", |
| 827 | input: &pb2.IndirectRequired{ |
| 828 | StrToNested: map[string]*pb2.NestedWithRequired{ |
| 829 | "fail": &pb2.NestedWithRequired{}, |
| 830 | }, |
| 831 | }, |
| 832 | want: `str_to_nested: { |
| 833 | key: "fail" |
| 834 | value: {} |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 835 | } |
| 836 | `, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 837 | wantErr: true, |
Herbie Ong | 20a1d31 | 2018-12-11 21:08:58 -0800 | [diff] [blame] | 838 | }, { |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 839 | desc: "indirect required field in map with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 840 | mo: prototext.MarshalOptions{AllowPartial: true}, |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 841 | input: &pb2.IndirectRequired{ |
| 842 | StrToNested: map[string]*pb2.NestedWithRequired{ |
| 843 | "fail": &pb2.NestedWithRequired{}, |
| 844 | }, |
| 845 | }, |
| 846 | want: `str_to_nested: { |
| 847 | key: "fail" |
| 848 | value: {} |
| 849 | } |
| 850 | `, |
| 851 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 852 | desc: "indirect required field in oneof", |
| 853 | input: &pb2.IndirectRequired{ |
| 854 | Union: &pb2.IndirectRequired_OneofNested{ |
| 855 | OneofNested: &pb2.NestedWithRequired{}, |
| 856 | }, |
| 857 | }, |
| 858 | want: "oneof_nested: {}\n", |
| 859 | wantErr: true, |
| 860 | }, { |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 861 | desc: "indirect required field in oneof with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 862 | mo: prototext.MarshalOptions{AllowPartial: true}, |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 863 | input: &pb2.IndirectRequired{ |
| 864 | Union: &pb2.IndirectRequired_OneofNested{ |
| 865 | OneofNested: &pb2.NestedWithRequired{}, |
| 866 | }, |
| 867 | }, |
| 868 | want: "oneof_nested: {}\n", |
| 869 | }, { |
Herbie Ong | 20a1d31 | 2018-12-11 21:08:58 -0800 | [diff] [blame] | 870 | desc: "unknown varint and fixed types", |
| 871 | input: &pb2.Scalars{ |
| 872 | OptString: scalar.String("this message contains unknown fields"), |
| 873 | XXX_unrecognized: pack.Message{ |
| 874 | pack.Tag{101, pack.VarintType}, pack.Bool(true), |
| 875 | pack.Tag{102, pack.VarintType}, pack.Varint(0xff), |
| 876 | pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47), |
| 877 | pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef), |
| 878 | }.Marshal(), |
| 879 | }, |
| 880 | want: `opt_string: "this message contains unknown fields" |
| 881 | 101: 1 |
| 882 | 102: 255 |
| 883 | 103: 47 |
| 884 | 104: 3735928559 |
| 885 | `, |
| 886 | }, { |
| 887 | desc: "unknown length-delimited", |
| 888 | input: &pb2.Scalars{ |
| 889 | XXX_unrecognized: pack.Message{ |
| 890 | pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)}, |
| 891 | pack.Tag{102, pack.BytesType}, pack.String("hello world"), |
| 892 | pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"), |
| 893 | }.Marshal(), |
| 894 | }, |
| 895 | want: `101: "\x01\x00" |
| 896 | 102: "hello world" |
| 897 | 103: "世界" |
| 898 | `, |
| 899 | }, { |
| 900 | desc: "unknown group type", |
| 901 | input: &pb2.Scalars{ |
| 902 | XXX_unrecognized: pack.Message{ |
| 903 | pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType}, |
| 904 | pack.Tag{102, pack.StartGroupType}, |
| 905 | pack.Tag{101, pack.VarintType}, pack.Bool(false), |
| 906 | pack.Tag{102, pack.BytesType}, pack.String("inside a group"), |
| 907 | pack.Tag{102, pack.EndGroupType}, |
| 908 | }.Marshal(), |
| 909 | }, |
| 910 | want: `101: {} |
| 911 | 102: { |
| 912 | 101: 0 |
| 913 | 102: "inside a group" |
| 914 | } |
| 915 | `, |
| 916 | }, { |
| 917 | desc: "unknown unpack repeated field", |
| 918 | input: &pb2.Scalars{ |
| 919 | XXX_unrecognized: pack.Message{ |
| 920 | pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)}, |
| 921 | pack.Tag{102, pack.BytesType}, pack.String("hello"), |
| 922 | pack.Tag{101, pack.VarintType}, pack.Bool(true), |
| 923 | pack.Tag{102, pack.BytesType}, pack.String("世界"), |
| 924 | }.Marshal(), |
| 925 | }, |
| 926 | want: `101: "\x01\x00\x01" |
Herbie Ong | 20a1d31 | 2018-12-11 21:08:58 -0800 | [diff] [blame] | 927 | 102: "hello" |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 928 | 101: 1 |
Herbie Ong | 20a1d31 | 2018-12-11 21:08:58 -0800 | [diff] [blame] | 929 | 102: "世界" |
| 930 | `, |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 931 | }, { |
| 932 | desc: "extensions of non-repeated fields", |
| 933 | input: func() proto.Message { |
| 934 | m := &pb2.Extensions{ |
| 935 | OptString: scalar.String("non-extension field"), |
| 936 | OptBool: scalar.Bool(true), |
| 937 | OptInt32: scalar.Int32(42), |
| 938 | } |
| 939 | setExtension(m, pb2.E_OptExtBool, true) |
| 940 | setExtension(m, pb2.E_OptExtString, "extension field") |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 941 | setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN) |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 942 | setExtension(m, pb2.E_OptExtNested, &pb2.Nested{ |
| 943 | OptString: scalar.String("nested in an extension"), |
| 944 | OptNested: &pb2.Nested{ |
| 945 | OptString: scalar.String("another nested in an extension"), |
| 946 | }, |
| 947 | }) |
| 948 | return m |
| 949 | }(), |
| 950 | want: `opt_string: "non-extension field" |
| 951 | opt_bool: true |
| 952 | opt_int32: 42 |
| 953 | [pb2.opt_ext_bool]: true |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 954 | [pb2.opt_ext_enum]: TEN |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 955 | [pb2.opt_ext_nested]: { |
| 956 | opt_string: "nested in an extension" |
| 957 | opt_nested: { |
| 958 | opt_string: "another nested in an extension" |
| 959 | } |
| 960 | } |
| 961 | [pb2.opt_ext_string]: "extension field" |
| 962 | `, |
| 963 | }, { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 964 | desc: "extension field contains invalid UTF-8", |
| 965 | input: func() proto.Message { |
| 966 | m := &pb2.Extensions{} |
| 967 | setExtension(m, pb2.E_OptExtString, "abc\xff") |
| 968 | return m |
| 969 | }(), |
| 970 | want: `[pb2.opt_ext_string]: "abc\xff" |
| 971 | `, |
| 972 | wantErr: true, |
| 973 | }, { |
Herbie Ong | 09b28a9 | 2019-04-03 15:42:41 -0700 | [diff] [blame] | 974 | desc: "extension partial returns error", |
| 975 | input: func() proto.Message { |
| 976 | m := &pb2.Extensions{} |
| 977 | setExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{ |
| 978 | OptString: scalar.String("partial1"), |
| 979 | }) |
| 980 | setExtension(m, pb2.E_ExtensionsContainer_OptExtPartial, &pb2.PartialRequired{ |
| 981 | OptString: scalar.String("partial2"), |
| 982 | }) |
| 983 | return m |
| 984 | }(), |
| 985 | want: `[pb2.ExtensionsContainer.opt_ext_partial]: { |
| 986 | opt_string: "partial2" |
| 987 | } |
| 988 | [pb2.opt_ext_partial]: { |
| 989 | opt_string: "partial1" |
| 990 | } |
| 991 | `, |
| 992 | wantErr: true, |
| 993 | }, { |
| 994 | desc: "extension partial with AllowPartial", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 995 | mo: prototext.MarshalOptions{AllowPartial: true}, |
Herbie Ong | 09b28a9 | 2019-04-03 15:42:41 -0700 | [diff] [blame] | 996 | input: func() proto.Message { |
| 997 | m := &pb2.Extensions{} |
| 998 | setExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{ |
| 999 | OptString: scalar.String("partial1"), |
| 1000 | }) |
| 1001 | return m |
| 1002 | }(), |
| 1003 | want: `[pb2.opt_ext_partial]: { |
| 1004 | opt_string: "partial1" |
| 1005 | } |
| 1006 | `, |
| 1007 | }, { |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1008 | desc: "extensions of repeated fields", |
| 1009 | input: func() proto.Message { |
| 1010 | m := &pb2.Extensions{} |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1011 | setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE}) |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1012 | setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47}) |
| 1013 | setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{ |
| 1014 | &pb2.Nested{OptString: scalar.String("one")}, |
| 1015 | &pb2.Nested{OptString: scalar.String("two")}, |
| 1016 | &pb2.Nested{OptString: scalar.String("three")}, |
| 1017 | }) |
| 1018 | return m |
| 1019 | }(), |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1020 | want: `[pb2.rpt_ext_enum]: TEN |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1021 | [pb2.rpt_ext_enum]: 101 |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1022 | [pb2.rpt_ext_enum]: ONE |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1023 | [pb2.rpt_ext_fixed32]: 42 |
| 1024 | [pb2.rpt_ext_fixed32]: 47 |
| 1025 | [pb2.rpt_ext_nested]: { |
| 1026 | opt_string: "one" |
| 1027 | } |
| 1028 | [pb2.rpt_ext_nested]: { |
| 1029 | opt_string: "two" |
| 1030 | } |
| 1031 | [pb2.rpt_ext_nested]: { |
| 1032 | opt_string: "three" |
| 1033 | } |
| 1034 | `, |
| 1035 | }, { |
| 1036 | desc: "extensions of non-repeated fields in another message", |
| 1037 | input: func() proto.Message { |
| 1038 | m := &pb2.Extensions{} |
| 1039 | setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true) |
| 1040 | setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field") |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1041 | setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN) |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1042 | setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{ |
| 1043 | OptString: scalar.String("nested in an extension"), |
| 1044 | OptNested: &pb2.Nested{ |
| 1045 | OptString: scalar.String("another nested in an extension"), |
| 1046 | }, |
| 1047 | }) |
| 1048 | return m |
| 1049 | }(), |
| 1050 | want: `[pb2.ExtensionsContainer.opt_ext_bool]: true |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1051 | [pb2.ExtensionsContainer.opt_ext_enum]: TEN |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1052 | [pb2.ExtensionsContainer.opt_ext_nested]: { |
| 1053 | opt_string: "nested in an extension" |
| 1054 | opt_nested: { |
| 1055 | opt_string: "another nested in an extension" |
| 1056 | } |
| 1057 | } |
| 1058 | [pb2.ExtensionsContainer.opt_ext_string]: "extension field" |
| 1059 | `, |
| 1060 | }, { |
| 1061 | desc: "extensions of repeated fields in another message", |
| 1062 | input: func() proto.Message { |
| 1063 | m := &pb2.Extensions{ |
| 1064 | OptString: scalar.String("non-extension field"), |
| 1065 | OptBool: scalar.Bool(true), |
| 1066 | OptInt32: scalar.Int32(42), |
| 1067 | } |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1068 | setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE}) |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1069 | setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"}) |
| 1070 | setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{ |
| 1071 | &pb2.Nested{OptString: scalar.String("one")}, |
| 1072 | &pb2.Nested{OptString: scalar.String("two")}, |
| 1073 | &pb2.Nested{OptString: scalar.String("three")}, |
| 1074 | }) |
| 1075 | return m |
| 1076 | }(), |
| 1077 | want: `opt_string: "non-extension field" |
| 1078 | opt_bool: true |
| 1079 | opt_int32: 42 |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1080 | [pb2.ExtensionsContainer.rpt_ext_enum]: TEN |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1081 | [pb2.ExtensionsContainer.rpt_ext_enum]: 101 |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1082 | [pb2.ExtensionsContainer.rpt_ext_enum]: ONE |
Herbie Ong | cf25308 | 2018-12-17 17:13:07 -0800 | [diff] [blame] | 1083 | [pb2.ExtensionsContainer.rpt_ext_nested]: { |
| 1084 | opt_string: "one" |
| 1085 | } |
| 1086 | [pb2.ExtensionsContainer.rpt_ext_nested]: { |
| 1087 | opt_string: "two" |
| 1088 | } |
| 1089 | [pb2.ExtensionsContainer.rpt_ext_nested]: { |
| 1090 | opt_string: "three" |
| 1091 | } |
| 1092 | [pb2.ExtensionsContainer.rpt_ext_string]: "hello" |
| 1093 | [pb2.ExtensionsContainer.rpt_ext_string]: "world" |
| 1094 | `, |
Herbie Ong | 6470ea6 | 2019-01-07 18:56:57 -0800 | [diff] [blame] | 1095 | }, { |
| 1096 | desc: "MessageSet", |
| 1097 | input: func() proto.Message { |
| 1098 | m := &pb2.MessageSet{} |
| 1099 | setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{ |
| 1100 | OptString: scalar.String("a messageset extension"), |
| 1101 | }) |
| 1102 | setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{ |
| 1103 | OptString: scalar.String("not a messageset extension"), |
| 1104 | }) |
| 1105 | setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{ |
| 1106 | OptString: scalar.String("just a regular extension"), |
| 1107 | }) |
| 1108 | return m |
| 1109 | }(), |
| 1110 | want: `[pb2.MessageSetExtension]: { |
| 1111 | opt_string: "a messageset extension" |
| 1112 | } |
| 1113 | [pb2.MessageSetExtension.ext_nested]: { |
| 1114 | opt_string: "just a regular extension" |
| 1115 | } |
| 1116 | [pb2.MessageSetExtension.not_message_set_extension]: { |
| 1117 | opt_string: "not a messageset extension" |
| 1118 | } |
| 1119 | `, |
| 1120 | }, { |
| 1121 | desc: "not real MessageSet 1", |
| 1122 | input: func() proto.Message { |
| 1123 | m := &pb2.FakeMessageSet{} |
| 1124 | setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{ |
| 1125 | OptString: scalar.String("not a messageset extension"), |
| 1126 | }) |
| 1127 | return m |
| 1128 | }(), |
| 1129 | want: `[pb2.FakeMessageSetExtension.message_set_extension]: { |
| 1130 | opt_string: "not a messageset extension" |
| 1131 | } |
| 1132 | `, |
| 1133 | }, { |
| 1134 | desc: "not real MessageSet 2", |
| 1135 | input: func() proto.Message { |
| 1136 | m := &pb2.MessageSet{} |
| 1137 | setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{ |
| 1138 | OptString: scalar.String("another not a messageset extension"), |
| 1139 | }) |
| 1140 | return m |
| 1141 | }(), |
| 1142 | want: `[pb2.message_set_extension]: { |
| 1143 | opt_string: "another not a messageset extension" |
| 1144 | } |
| 1145 | `, |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1146 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1147 | desc: "Any not expanded", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1148 | mo: prototext.MarshalOptions{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1149 | Resolver: preg.NewTypes(), |
| 1150 | }, |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1151 | input: func() proto.Message { |
| 1152 | m := &pb2.Nested{ |
| 1153 | OptString: scalar.String("embedded inside Any"), |
| 1154 | OptNested: &pb2.Nested{ |
| 1155 | OptString: scalar.String("inception"), |
| 1156 | }, |
| 1157 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 1158 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1159 | if err != nil { |
| 1160 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1161 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1162 | return &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1163 | TypeUrl: "pb2.Nested", |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1164 | Value: b, |
Herbie Ong | 300cff0 | 2019-03-20 18:05:16 -0700 | [diff] [blame] | 1165 | } |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1166 | }(), |
| 1167 | want: `type_url: "pb2.Nested" |
| 1168 | value: "\n\x13embedded inside Any\x12\x0b\n\tinception" |
| 1169 | `, |
| 1170 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1171 | desc: "Any expanded", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1172 | mo: prototext.MarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1173 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1174 | }, |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1175 | input: func() proto.Message { |
| 1176 | m := &pb2.Nested{ |
| 1177 | OptString: scalar.String("embedded inside Any"), |
| 1178 | OptNested: &pb2.Nested{ |
| 1179 | OptString: scalar.String("inception"), |
| 1180 | }, |
| 1181 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 1182 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1183 | if err != nil { |
| 1184 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1185 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1186 | return &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1187 | TypeUrl: "foo/pb2.Nested", |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1188 | Value: b, |
Herbie Ong | 300cff0 | 2019-03-20 18:05:16 -0700 | [diff] [blame] | 1189 | } |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1190 | }(), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1191 | want: `[foo/pb2.Nested]: { |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1192 | opt_string: "embedded inside Any" |
| 1193 | opt_nested: { |
| 1194 | opt_string: "inception" |
| 1195 | } |
| 1196 | } |
| 1197 | `, |
| 1198 | }, { |
Damien Neil | 0c9f0a9 | 2019-06-19 10:41:09 -0700 | [diff] [blame^] | 1199 | desc: "Any expanded with missing required", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1200 | mo: prototext.MarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1201 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1202 | }, |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1203 | input: func() proto.Message { |
| 1204 | m := &pb2.PartialRequired{ |
| 1205 | OptString: scalar.String("embedded inside Any"), |
| 1206 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1207 | b, err := proto.MarshalOptions{ |
| 1208 | AllowPartial: true, |
| 1209 | Deterministic: true, |
| 1210 | }.Marshal(m) |
Herbie Ong | 300cff0 | 2019-03-20 18:05:16 -0700 | [diff] [blame] | 1211 | if err != nil { |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1212 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1213 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1214 | return &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1215 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1216 | Value: b, |
Herbie Ong | 300cff0 | 2019-03-20 18:05:16 -0700 | [diff] [blame] | 1217 | } |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1218 | }(), |
| 1219 | want: `[pb2.PartialRequired]: { |
| 1220 | opt_string: "embedded inside Any" |
| 1221 | } |
| 1222 | `, |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1223 | }, { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 1224 | desc: "Any with invalid UTF-8", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1225 | mo: prototext.MarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1226 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb3.Nested{})), |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 1227 | }, |
| 1228 | input: func() proto.Message { |
| 1229 | m := &pb3.Nested{ |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 1230 | SString: "abcd", |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 1231 | } |
| 1232 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1233 | if err != nil { |
| 1234 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1235 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1236 | return &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1237 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 1238 | Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1), |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 1239 | } |
| 1240 | }(), |
| 1241 | want: `[pb3.Nested]: { |
| 1242 | s_string: "abc\xff" |
| 1243 | } |
| 1244 | `, |
| 1245 | wantErr: true, |
| 1246 | }, { |
Herbie Ong | 8170d69 | 2019-02-13 14:13:21 -0800 | [diff] [blame] | 1247 | desc: "Any with invalid value", |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1248 | mo: prototext.MarshalOptions{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 1249 | Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1250 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 1251 | input: &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1252 | TypeUrl: "foo/pb2.Nested", |
| 1253 | Value: dhex("80"), |
Herbie Ong | 300cff0 | 2019-03-20 18:05:16 -0700 | [diff] [blame] | 1254 | }, |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 1255 | want: `type_url: "foo/pb2.Nested" |
Herbie Ong | a94f78c | 2019-01-03 15:39:58 -0800 | [diff] [blame] | 1256 | value: "\x80" |
| 1257 | `, |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 1258 | }} |
| 1259 | |
| 1260 | for _, tt := range tests { |
| 1261 | tt := tt |
| 1262 | t.Run(tt.desc, func(t *testing.T) { |
Herbie Ong | 3a38591 | 2019-03-20 14:04:24 -0700 | [diff] [blame] | 1263 | // Use 2-space indentation on all MarshalOptions. |
| 1264 | tt.mo.Indent = " " |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1265 | b, err := tt.mo.Marshal(tt.input) |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 1266 | if err != nil && !tt.wantErr { |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1267 | t.Errorf("Marshal() returned error: %v\n", err) |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 1268 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 1269 | if err == nil && tt.wantErr { |
Herbie Ong | f42b55f | 2019-01-02 15:46:07 -0800 | [diff] [blame] | 1270 | t.Error("Marshal() got nil error, want error\n") |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 1271 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 1272 | got := string(b) |
| 1273 | if tt.want != "" && got != tt.want { |
| 1274 | t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 1275 | if diff := cmp.Diff(tt.want, got); diff != "" { |
Herbie Ong | cddf819 | 2018-11-28 18:25:20 -0800 | [diff] [blame] | 1276 | t.Errorf("Marshal() diff -want +got\n%v\n", diff) |
| 1277 | } |
| 1278 | } |
| 1279 | }) |
| 1280 | } |
| 1281 | } |