From 947a51a464ec9ecd3aa7098fe3abfcc4822c1fe5 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Tue, 26 Oct 2021 12:43:57 +1100 Subject: [PATCH] Test coverage: .from_ruby / #from_ruby --- ruby/lib/google/protobuf/well_known_types.rb | 4 +-- ruby/tests/well_known_types_test.rb | 29 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb index b58e815670..f86fe28b77 100755 --- a/ruby/lib/google/protobuf/well_known_types.rb +++ b/ruby/lib/google/protobuf/well_known_types.rb @@ -136,7 +136,7 @@ module Google raise UnexpectedStructType end end - + def self.from_ruby(value) Value.new.from_ruby(value) end @@ -164,7 +164,7 @@ module Google else raise UnexpectedStructType end - + self end end diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 996dbc30a5..3e94ca01f3 100755 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -215,4 +215,33 @@ class TestWellKnownTypes < Test::Unit::TestCase ) assert_equal '[]', value_field.get(proto).inspect end + + def test_from_ruby + pb = Google::Protobuf::Value.from_ruby(nil) + assert_equal pb.null_value, 0 + + pb = Google::Protobuf::Value.from_ruby(1.23) + assert_equal pb.number_value, 1.23 + + pb = Google::Protobuf::Value.from_ruby('1.23') + assert_equal pb.string_value, '1.23' + + pb = Google::Protobuf::Value.from_ruby(true) + assert_equal pb.bool_value, true + + pb = Google::Protobuf::Value.from_ruby(false) + assert_equal pb.bool_value, false + + pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] })) + assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] }) + + pb = Google::Protobuf::Value.from_ruby({ a: 1, b: '2', c: [1, 2, 3] }) + assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] }) + + pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::ListValue.from_a([1, 2, 3])) + assert_equal pb.struct_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) + + pb = Google::Protobuf::Value.from_ruby([1, 2, 3]) + assert_equal pb.struct_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) + end end