diff options
Diffstat (limited to 'ruby/tests/basic.rb')
-rw-r--r-- | ruby/tests/basic.rb | 167 |
1 files changed, 163 insertions, 4 deletions
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index e0dba8bd..94071ca0 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 -------------- @@ -95,8 +96,18 @@ module BasicTest optional :d, :enum, 4, "TestEnum" end end + + add_message "repro.Outer" do + map :items, :int32, :message, 1, "repro.Inner" + end + + add_message "repro.Inner" do + end end + + Outer = pool.lookup("repro.Outer").msgclass + Inner = pool.lookup("repro.Inner").msgclass Foo = pool.lookup("Foo").msgclass Bar = pool.lookup("Bar").msgclass Baz = pool.lookup("Baz").msgclass @@ -667,6 +678,28 @@ module BasicTest end end + def test_map_corruption + # This pattern led to a crash in a previous version of upb/protobuf. + m = MapMessage.new(map_string_int32: { "aaa" => 1 }) + m.map_string_int32['podid'] = 2 + m.map_string_int32['aaa'] = 3 + end + + def test_concurrent_decoding + o = Outer.new + o.items[0] = Inner.new + raw = Outer.encode(o) + + thds = 2.times.map do + Thread.new do + 100000.times do + assert_equal o, Outer.decode(raw) + end + end + end + thds.map(&:join) + end + def test_map_encode_decode m = MapMessage.new( :map_string_int32 => {"a" => 1, "b" => 2}, @@ -920,6 +953,16 @@ module BasicTest :repeated_uint64=>[] } assert_equal expected_result, m.to_h + + m = MapMessage.new( + :map_string_int32 => {"a" => 1, "b" => 2}, + :map_string_msg => {"a" => TestMessage2.new(:foo => 1), + "b" => TestMessage2.new(:foo => 2)}) + expected_result = { + :map_string_int32 => {"a" => 1, "b" => 2}, + :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}} + } + assert_equal expected_result, m.to_h end @@ -1156,6 +1199,8 @@ module BasicTest json_text = TestMessage.encode_json(m) m2 = TestMessage.decode_json(json_text) + puts m.inspect + puts m2.inspect assert m == m2 # Crash case from GitHub issue 283. @@ -1167,21 +1212,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 |