@@ -238,6 +238,102 @@ defmodule Protobuf.JSON.EncodeTest do
238
238
assert encode ( message , emit_unpopulated: true ) == % { decoded | "h" => [ bar , bar ] }
239
239
end
240
240
241
+ describe "Google.Protobuf.* value wrappers" do
242
+ test "Google.Protobuf.BoolValue" do
243
+ message =
244
+ TestAllTypesProto3 . new! (
245
+ optional_bool_wrapper: Google.Protobuf.BoolValue . new! ( value: true )
246
+ )
247
+
248
+ assert encode ( message ) == % { "optionalBoolWrapper" => true }
249
+ end
250
+
251
+ test "Google.Protobuf.StringValue" do
252
+ message =
253
+ TestAllTypesProto3 . new! (
254
+ optional_string_wrapper: Google.Protobuf.StringValue . new! ( value: "foo" )
255
+ )
256
+
257
+ assert encode ( message ) == % { "optionalStringWrapper" => "foo" }
258
+ end
259
+
260
+ test "Google.Protobuf.FloatValue" do
261
+ message =
262
+ TestAllTypesProto3 . new! (
263
+ optional_float_wrapper: Google.Protobuf.FloatValue . new! ( value: 3.14 )
264
+ )
265
+
266
+ assert encode ( message ) == % { "optionalFloatWrapper" => 3.14 }
267
+ end
268
+
269
+ test "Google.Protobuf.DoubleValue" do
270
+ message =
271
+ TestAllTypesProto3 . new! (
272
+ optional_double_wrapper: Google.Protobuf.DoubleValue . new! ( value: 3.14 )
273
+ )
274
+
275
+ assert encode ( message ) == % { "optionalDoubleWrapper" => 3.14 }
276
+ end
277
+
278
+ test "Google.Protobuf.Int32Value" do
279
+ message =
280
+ TestAllTypesProto3 . new! (
281
+ optional_int32_wrapper: Google.Protobuf.Int32Value . new! ( value: - 3 )
282
+ )
283
+
284
+ assert encode ( message ) == % { "optionalInt32Wrapper" => - 3 }
285
+ end
286
+
287
+ test "Google.Protobuf.UInt32Value" do
288
+ message =
289
+ TestAllTypesProto3 . new! (
290
+ optional_uint32_wrapper: Google.Protobuf.UInt32Value . new! ( value: 3 )
291
+ )
292
+
293
+ assert encode ( message ) == % { "optionalUint32Wrapper" => 3 }
294
+ end
295
+
296
+ test "Google.Protobuf.Int64Value" do
297
+ message =
298
+ TestAllTypesProto3 . new! (
299
+ optional_int64_wrapper: Google.Protobuf.Int64Value . new! ( value: - 3 )
300
+ )
301
+
302
+ assert encode ( message ) == % { "optionalInt64Wrapper" => - 3 }
303
+ end
304
+
305
+ test "Google.Protobuf.UInt64Value" do
306
+ message =
307
+ TestAllTypesProto3 . new! (
308
+ optional_uint64_wrapper: Google.Protobuf.UInt64Value . new! ( value: 3 )
309
+ )
310
+
311
+ assert encode ( message ) == % { "optionalUint64Wrapper" => 3 }
312
+ end
313
+ end
314
+
315
+ describe "Google.Protobuf.BytesValue" do
316
+ test "encodes with base 64" do
317
+ message =
318
+ TestAllTypesProto3 . new! (
319
+ optional_bytes_wrapper: Google.Protobuf.BytesValue . new! ( value: << 1 , 2 , 3 >> )
320
+ )
321
+
322
+ assert encode ( message ) == % { "optionalBytesWrapper" => Base . url_encode64 ( << 1 , 2 , 3 >> ) }
323
+ end
324
+ end
325
+
326
+ describe "Google.Protobuf.FieldMask" do
327
+ test "encodes with base 64" do
328
+ message =
329
+ TestAllTypesProto3 . new! (
330
+ optional_field_mask: Google.Protobuf.FieldMask . new! ( paths: [ "foo_bar" , "baz_bong" ] )
331
+ )
332
+
333
+ assert encode ( message ) == % { "optionalFieldMask" => "fooBar,bazBong" }
334
+ end
335
+ end
336
+
241
337
describe "Google.Protobuf.Duration" do
242
338
test "encodes as a string" do
243
339
cases = [
@@ -284,6 +380,16 @@ defmodule Protobuf.JSON.EncodeTest do
284
380
end
285
381
286
382
describe "Google.Protobuf.Timestamp" do
383
+ test "encodes the timestamp as a string" do
384
+ { :ok , datetime , _offset = 0 } = DateTime . from_iso8601 ( "2000-01-01T00:00:00Z" )
385
+ unix_seconds = DateTime . to_unix ( datetime , :second )
386
+
387
+ timestamp = Google.Protobuf.Timestamp . new! ( seconds: unix_seconds , nanos: 0 )
388
+ message = TestAllTypesProto3 . new! ( optional_timestamp: timestamp )
389
+
390
+ assert encode ( message ) == % { "optionalTimestamp" => "2000-01-01T00:00:00Z" }
391
+ end
392
+
287
393
test "returns an error if the timestamp is outside of the allowed range" do
288
394
{ :ok , datetime , _offset = 0 } = DateTime . from_iso8601 ( "0000-01-01T00:00:00Z" )
289
395
unix_seconds = DateTime . to_unix ( datetime , :second )
@@ -295,4 +401,38 @@ defmodule Protobuf.JSON.EncodeTest do
295
401
{ :invalid_timestamp , timestamp , "timestamp is outside of allowed range" }
296
402
end
297
403
end
404
+
405
+ describe "Google.Protobuf.Value" do
406
+ test "encodes correctly" do
407
+ cases = [
408
+ { { :string_value , "foo" } , "foo" } ,
409
+ { { :bool_value , true } , true } ,
410
+ { { :number_value , 3.14 } , 3.14 } ,
411
+ { { :number_value , 1 } , 1 } ,
412
+ { { :null_value , :NULL_VALUE } , nil }
413
+ ]
414
+
415
+ for { kind , json } <- cases do
416
+ value = Google.Protobuf.Value . new! ( kind: kind )
417
+ message = TestAllTypesProto3 . new! ( optional_value: value )
418
+ assert encode ( message ) == % { "optionalValue" => json }
419
+ end
420
+ end
421
+ end
422
+
423
+ describe "Google.Protobuf.ListValue" do
424
+ test "encodes correctly" do
425
+ value = Google.Protobuf.ListValue . new! ( values: [ ] )
426
+ message = TestAllTypesProto3 . new! ( repeated_list_value: [ value ] )
427
+ assert encode ( message ) == % { "repeatedListValue" => [ [ ] ] }
428
+ end
429
+ end
430
+
431
+ describe "Google.Protobuf.Struct" do
432
+ test "encodes correctly" do
433
+ value = Google.Protobuf.Struct . new! ( fields: % { "foo" => Google.Protobuf.Empty . new! ( [ ] ) } )
434
+ message = TestAllTypesProto3 . new! ( optional_struct: value )
435
+ assert encode ( message ) == % { "optionalStruct" => % { "foo" => % { } } }
436
+ end
437
+ end
298
438
end
0 commit comments