diff options
Diffstat (limited to 'ruby/tests')
-rw-r--r-- | ruby/tests/basic.rb | 220 | ||||
-rw-r--r-- | ruby/tests/gc_test.rb | 58 | ||||
-rw-r--r-- | ruby/tests/well_known_types_test.rb | 12 |
3 files changed, 281 insertions, 9 deletions
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index e0dba8bd..ad34d53d 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 -------------- @@ -50,6 +51,17 @@ module BasicTest optional :foo, :int32, 1 end + add_message "TestEmbeddedMessageParent" do + optional :child_msg, :message, 1, "TestEmbeddedMessageChild" + optional :number, :int32, 2 + + repeated :repeated_msg, :message, 3, "TestEmbeddedMessageChild" + repeated :repeated_number, :int32, 4 + end + add_message "TestEmbeddedMessageChild" do + optional :sub_child, :message, 1, "TestMessage" + end + add_message "Recursive1" do optional :foo, :message, 1, "Recursive2" end @@ -95,13 +107,25 @@ 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 TestMessage = pool.lookup("TestMessage").msgclass TestMessage2 = pool.lookup("TestMessage2").msgclass + TestEmbeddedMessageParent = pool.lookup("TestEmbeddedMessageParent").msgclass + TestEmbeddedMessageChild = pool.lookup("TestEmbeddedMessageChild").msgclass Recursive1 = pool.lookup("Recursive1").msgclass Recursive2 = pool.lookup("Recursive2").msgclass TestEnum = pool.lookup("TestEnum").enummodule @@ -150,12 +174,18 @@ module BasicTest m.optional_double = 0.5 m.optional_string = "hello" assert m.optional_string == "hello" + m.optional_string = :hello + assert m.optional_string == "hello" m.optional_bytes = "world".encode!('ASCII-8BIT') assert m.optional_bytes == "world" m.optional_msg = TestMessage2.new(:foo => 42) assert m.optional_msg == TestMessage2.new(:foo => 42) m.optional_msg = nil assert m.optional_msg == nil + m.optional_enum = :C + assert m.optional_enum == :C + m.optional_enum = 'C' + assert m.optional_enum == :C end def test_ctor_args @@ -172,6 +202,34 @@ module BasicTest assert m.repeated_string[2] == "world" end + def test_ctor_string_symbol_args + m = TestMessage.new(:optional_enum => 'C', :repeated_enum => ['A', 'B']) + assert_equal :C, m.optional_enum + assert_equal [:A, :B], m.repeated_enum + + m = TestMessage.new(:optional_string => :foo, :repeated_string => [:foo, :bar]) + assert_equal 'foo', m.optional_string + assert_equal ['foo', 'bar'], m.repeated_string + end + + def test_embeddedmsg_hash_init + m = TestEmbeddedMessageParent.new(:child_msg => {sub_child: {optional_int32: 1}}, + :number => 2, + :repeated_msg => [{sub_child: {optional_int32: 3}}], + :repeated_number => [10, 20, 30]) + + assert_equal 2, m.number + assert_equal [10, 20, 30], m.repeated_number + + assert_not_nil m.child_msg + assert_not_nil m.child_msg.sub_child + assert_equal m.child_msg.sub_child.optional_int32, 1 + + assert_not_nil m.repeated_msg + assert_equal 1, m.repeated_msg.length + assert_equal 3, m.repeated_msg.first.sub_child.optional_int32 + end + def test_inspect m = TestMessage.new(:optional_int32 => -42, :optional_enum => :A, @@ -603,7 +661,7 @@ module BasicTest assert_raise RangeError do m["z"] = :Z end - assert_raise TypeError do + assert_raise RangeError do m["z"] = "z" end end @@ -667,6 +725,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}, @@ -894,7 +974,7 @@ module BasicTest end def test_to_h - m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)]) expected_result = { :optional_bool=>true, :optional_bytes=>"", @@ -914,12 +994,22 @@ module BasicTest :repeated_float=>[], :repeated_int32=>[], :repeated_int64=>[], - :repeated_msg=>[], + :repeated_msg=>[{:foo => 100}], :repeated_string=>["bar1", "bar2"], :repeated_uint32=>[], :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 +1246,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 +1259,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 diff --git a/ruby/tests/gc_test.rb b/ruby/tests/gc_test.rb new file mode 100644 index 00000000..f3470cca --- /dev/null +++ b/ruby/tests/gc_test.rb @@ -0,0 +1,58 @@ +#!/usr/bin/ruby +# +# generated_code.rb is in the same directory as this test. +$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) + +old_gc = GC.stress +GC.stress = 0x01 | 0x04 +require 'generated_code_pb' +GC.stress = old_gc + +require 'test/unit' + +class GCTest < Test::Unit::TestCase + def get_msg + A::B::C::TestMessage.new( + :optional_int32 => 1, + :optional_int64 => 1, + :optional_uint32 => 1, + :optional_uint64 => 1, + :optional_bool => true, + :optional_double => 1.0, + :optional_float => 1.0, + :optional_string => "a", + :optional_bytes => "b", + :optional_enum => A::B::C::TestEnum::A, + :optional_msg => A::B::C::TestMessage.new(), + :repeated_int32 => [1], + :repeated_int64 => [1], + :repeated_uint32 => [1], + :repeated_uint64 => [1], + :repeated_bool => [true], + :repeated_double => [1.0], + :repeated_float => [1.0], + :repeated_string => ["a"], + :repeated_bytes => ["b"], + :repeated_enum => [A::B::C::TestEnum::A], + :repeated_msg => [A::B::C::TestMessage.new()], + :map_int32_string => {1 => "a"}, + :map_int64_string => {1 => "a"}, + :map_uint32_string => {1 => "a"}, + :map_uint64_string => {1 => "a"}, + :map_bool_string => {true => "a"}, + :map_string_string => {"a" => "a"}, + :map_string_msg => {"a" => A::B::C::TestMessage.new()}, + :map_string_int32 => {"a" => 1}, + :map_string_bool => {"a" => true}, + ) + end + def test_generated_msg + old_gc = GC.stress + GC.stress = 0x01 | 0x04 + from = get_msg + data = A::B::C::TestMessage.encode(from) + to = A::B::C::TestMessage.decode(data) + GC.stress = old_gc + puts "passed" + end +end diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 9b46632b..bd24c328 100644 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -13,10 +13,18 @@ class TestWellKnownTypes < Test::Unit::TestCase assert_equal Time.at(12345), ts.to_time assert_equal 12345, ts.to_i - ts.from_time(Time.at(123456, 654321)) + # millisecond accuracy + time = Time.at(123456, 654321) + ts.from_time(time) assert_equal 123456, ts.seconds assert_equal 654321000, ts.nanos - assert_equal Time.at(123456.654321), ts.to_time + assert_equal time, ts.to_time + + # nanosecond accuracy + time = Time.at(123456, Rational(654321321, 1000)) + ts.from_time(time) + assert_equal 654321321, ts.nanos + assert_equal time, ts.to_time end def test_duration |