diff options
author | Josh Haberman <jhaberman@gmail.com> | 2016-07-27 17:31:16 -0700 |
---|---|---|
committer | Josh Haberman <jhaberman@gmail.com> | 2016-07-28 17:02:33 -0700 |
commit | e3094a8d803c0b9df33a8a506cce28a89c5fd6e8 (patch) | |
tree | 67cd5cecd7a495f18c441163355e9d04c2bcfa17 /ruby/tests | |
parent | ba52f2b6780fa5e6bee86cf7e8ee6f6ba617862c (diff) | |
download | protobuf-e3094a8d803c0b9df33a8a506cce28a89c5fd6e8.tar.gz protobuf-e3094a8d803c0b9df33a8a506cce28a89c5fd6e8.tar.bz2 protobuf-e3094a8d803c0b9df33a8a506cce28a89c5fd6e8.zip |
Ruby: added API support for well-known types.
Diffstat (limited to 'ruby/tests')
-rw-r--r-- | ruby/tests/well_known_types_test.rb | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb new file mode 100644 index 00000000..cf9a3aa0 --- /dev/null +++ b/ruby/tests/well_known_types_test.rb @@ -0,0 +1,98 @@ +#!/usr/bin/ruby + +require 'test/unit' +require 'google/protobuf/well_known_types' + +class TestWellKnownTypes < Test::Unit::TestCase + def test_timestamp + ts = Google::Protobuf::Timestamp.new + + assert_equal Time.at(0), ts.to_time + + ts.seconds = 12345 + assert_equal Time.at(12345), ts.to_time + assert_equal 12345, ts.to_i + + ts.from_time(Time.at(123456, 654321)) + assert_equal 123456, ts.seconds + assert_equal 654321000, ts.nanos + assert_equal Time.at(123456.654321), ts.to_time + end + + def test_duration + duration = Google::Protobuf::Duration.new(seconds: 123, nanos: 456) + assert_equal 123.000000456, duration.to_f + end + + def test_struct + struct = Google::Protobuf::Struct.new + + substruct = { + "subkey" => 999, + "subkey2" => false + } + + sublist = ["abc", 123, {"deepkey" => "deepval"}] + + struct["number"] = 12345 + struct["boolean-true"] = true + struct["boolean-false"] = false + struct["null"] = nil + struct["string"] = "abcdef" + struct["substruct"] = substruct + struct["sublist"] = sublist + + assert_equal 12345, struct["number"] + assert_equal true, struct["boolean-true"] + assert_equal false, struct["boolean-false"] + assert_equal nil, struct["null"] + assert_equal "abcdef", struct["string"] + assert_equal(Google::Protobuf::Struct.from_hash(substruct), + struct["substruct"]) + assert_equal(Google::Protobuf::ListValue.from_a(sublist), + struct["sublist"]) + + should_equal = { + "number" => 12345, + "boolean-true" => true, + "boolean-false" => false, + "null" => nil, + "string" => "abcdef", + "substruct" => { + "subkey" => 999, + "subkey2" => false + }, + "sublist" => ["abc", 123, {"deepkey" => "deepval"}] + } + + list = struct["sublist"] + list.is_a?(Google::Protobuf::ListValue) + assert_equal "abc", list[0] + assert_equal 123, list[1] + assert_equal({"deepkey" => "deepval"}, list[2].to_h) + + # to_h returns a fully-flattened Ruby structure (Hash and Array). + assert_equal(should_equal, struct.to_h) + + # Test that we can assign Struct and ListValue directly. + struct["substruct"] = Google::Protobuf::Struct.from_hash(substruct) + struct["sublist"] = Google::Protobuf::ListValue.from_a(sublist) + + assert_equal(should_equal, struct.to_h) + + struct["sublist"] << nil + should_equal["sublist"] << nil + + assert_equal(should_equal, struct.to_h) + assert_equal(should_equal["sublist"].length, struct["sublist"].length) + end + + def test_any + any = Google::Protobuf::Any.new + ts = Google::Protobuf::Timestamp.new(seconds: 12345, nanos: 6789) + any.pack(ts) + + assert_true any.is(Google::Protobuf::Timestamp) + assert_equal ts, any.unpack(Google::Protobuf::Timestamp) + end +end |