aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby/tests
diff options
context:
space:
mode:
authorGravatar Ewout <e@ewout.name>2017-03-09 20:47:56 +0100
committerGravatar Ewout <e@ewout.name>2017-03-09 20:47:56 +0100
commit008dc92c9d32abcf608883ae44d5a78f59a4e3c2 (patch)
tree5bd658854e0c3c079164fe5bf5e82c76c310e1fb /ruby/tests
parentfa1788026cf7c7310f068dbee7fbf71958ec1290 (diff)
Ruby version optionally emits default values in JSON encoding.
Usage: Message.encode_json(m, emit_defaults: true) Message fields that are nil will still not appear in the encoded JSON.
Diffstat (limited to 'ruby/tests')
-rw-r--r--ruby/tests/basic.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb
index ca81e3a5..367be167 100644
--- a/ruby/tests/basic.rb
+++ b/ruby/tests/basic.rb
@@ -1174,6 +1174,36 @@ 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":[]}'
+
+ assert TestMessage.encode_json(m, :emit_defaults => 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":[]}'
+
+ assert TestMessage.encode_json(m, :emit_defaults => 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":[]}'
+
+ assert TestMessage.encode_json(m, :emit_defaults => true) == expected
+ end
+
def test_json_maps
# TODO: Fix JSON in JRuby version.
return if RUBY_PLATFORM == "java"
@@ -1189,6 +1219,14 @@ module BasicTest
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}}}'
+ assert MapMessage.encode_json(m, :emit_defaults => true) == expected
+ end
+
def test_comparison_with_arbitrary_object
assert MapMessage.new != nil
end