Test coverage: .from_ruby / #from_ruby
diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb
index b58e815..f86fe28 100755
--- a/ruby/lib/google/protobuf/well_known_types.rb
+++ b/ruby/lib/google/protobuf/well_known_types.rb
@@ -136,7 +136,7 @@
raise UnexpectedStructType
end
end
-
+
def self.from_ruby(value)
Value.new.from_ruby(value)
end
@@ -164,7 +164,7 @@
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 996dbc3..3e94ca0 100755
--- a/ruby/tests/well_known_types_test.rb
+++ b/ruby/tests/well_known_types_test.rb
@@ -215,4 +215,33 @@
)
assert_equal '[<Google::Protobuf::Value: string_value: "Hello">]', 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