aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby/tests
diff options
context:
space:
mode:
authorGravatar Joshua Haberman <jhaberman@gmail.com>2017-05-15 08:05:27 -0700
committerGravatar GitHub <noreply@github.com>2017-05-15 08:05:27 -0700
commitb28617b813b4ba4845164a1f5190f762f5b91832 (patch)
treee6cd363789bf163dd1dcfd6c605b7da1a107e0b2 /ruby/tests
parent455b61c6b0f39ac269b26969877dd3c6f3e32270 (diff)
parentaec07110750924790a939826f8ac3444f22f00bf (diff)
Merge pull request #2815 from devwout/ruby_json_emit_defaults
Ruby version optionally emits default values in JSON encoding.
Diffstat (limited to 'ruby/tests')
-rw-r--r--ruby/tests/basic.rb123
1 files changed, 119 insertions, 4 deletions
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb
index 34251234..02cc7a03 100644
--- a/ruby/tests/basic.rb
+++ b/ruby/tests/basic.rb
@@ -1,6 +1,7 @@
#!/usr/bin/ruby
require 'google/protobuf'
+require 'json'
require 'test/unit'
# ------------- generated code --------------
@@ -1184,21 +1185,135 @@ module BasicTest
Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2]))
end
+ def test_json_emit_defaults
+ # TODO: Fix JSON in JRuby version.
+ return if RUBY_PLATFORM == "java"
+ m = TestMessage.new
+
+ expected = {
+ optionalInt32: 0,
+ optionalInt64: 0,
+ optionalUint32: 0,
+ optionalUint64: 0,
+ optionalBool: false,
+ optionalFloat: 0,
+ optionalDouble: 0,
+ optionalString: "",
+ optionalBytes: "",
+ optionalEnum: "Default",
+ repeatedInt32: [],
+ repeatedInt64: [],
+ repeatedUint32: [],
+ repeatedUint64: [],
+ repeatedBool: [],
+ repeatedFloat: [],
+ repeatedDouble: [],
+ repeatedString: [],
+ repeatedBytes: [],
+ repeatedMsg: [],
+ repeatedEnum: []
+ }
+
+ actual = TestMessage.encode_json(m, :emit_defaults => true)
+
+ assert JSON.parse(actual, :symbolize_names => true) == expected
+ end
+
+ def test_json_emit_defaults_submsg
+ # TODO: Fix JSON in JRuby version.
+ return if RUBY_PLATFORM == "java"
+ m = TestMessage.new(optional_msg: TestMessage2.new)
+
+ expected = {
+ optionalInt32: 0,
+ optionalInt64: 0,
+ optionalUint32: 0,
+ optionalUint64: 0,
+ optionalBool: false,
+ optionalFloat: 0,
+ optionalDouble: 0,
+ optionalString: "",
+ optionalBytes: "",
+ optionalMsg: {foo: 0},
+ optionalEnum: "Default",
+ repeatedInt32: [],
+ repeatedInt64: [],
+ repeatedUint32: [],
+ repeatedUint64: [],
+ repeatedBool: [],
+ repeatedFloat: [],
+ repeatedDouble: [],
+ repeatedString: [],
+ repeatedBytes: [],
+ repeatedMsg: [],
+ repeatedEnum: []
+ }
+
+ actual = TestMessage.encode_json(m, :emit_defaults => true)
+
+ assert JSON.parse(actual, :symbolize_names => true) == expected
+ end
+
+ def test_json_emit_defaults_repeated_submsg
+ # TODO: Fix JSON in JRuby version.
+ return if RUBY_PLATFORM == "java"
+ m = TestMessage.new(repeated_msg: [TestMessage2.new])
+
+ expected = {
+ optionalInt32: 0,
+ optionalInt64: 0,
+ optionalUint32: 0,
+ optionalUint64: 0,
+ optionalBool: false,
+ optionalFloat: 0,
+ optionalDouble: 0,
+ optionalString: "",
+ optionalBytes: "",
+ optionalEnum: "Default",
+ repeatedInt32: [],
+ repeatedInt64: [],
+ repeatedUint32: [],
+ repeatedUint64: [],
+ repeatedBool: [],
+ repeatedFloat: [],
+ repeatedDouble: [],
+ repeatedString: [],
+ repeatedBytes: [],
+ repeatedMsg: [{foo: 0}],
+ repeatedEnum: []
+ }
+
+ actual = TestMessage.encode_json(m, :emit_defaults => true)
+
+ assert JSON.parse(actual, :symbolize_names => true) == expected
+ end
+
def test_json_maps
# TODO: Fix JSON in JRuby version.
return if RUBY_PLATFORM == "java"
m = MapMessage.new(:map_string_int32 => {"a" => 1})
- expected = '{"mapStringInt32":{"a":1},"mapStringMsg":{}}'
- expected_preserve = '{"map_string_int32":{"a":1},"map_string_msg":{}}'
- assert MapMessage.encode_json(m) == expected
+ expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
+ expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
+ assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
- assert json == expected_preserve
+ assert JSON.parse(json, :symbolize_names => true) == expected_preserve
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
assert m == m2
end
+ def test_json_maps_emit_defaults_submsg
+ # TODO: Fix JSON in JRuby version.
+ return if RUBY_PLATFORM == "java"
+ m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
+ expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
+
+ actual = MapMessage.encode_json(m, :emit_defaults => true)
+
+ assert JSON.parse(actual, :symbolize_names => true) == expected
+ end
+
def test_comparison_with_arbitrary_object
assert MapMessage.new != nil
end