Add well_known_types_test.rb to BUILD.bazel.

pull/13156/head
Jason Lunn 1 year ago
parent 52c9557055
commit 503d3c7745
  1. 12
      ruby/compatibility_tests/v3.0.0/tests/basic.rb
  2. 69
      ruby/tests/BUILD.bazel
  3. 30
      ruby/tests/well_known_types_test.rb

@ -882,30 +882,22 @@ module BasicTest
def test_def_errors
s = Google::Protobuf::DescriptorPool.new
success = false
begin
assert_raises Google::Protobuf::TypeError do
s.build do
# enum with no default (integer value 0)
add_enum "MyEnum" do
value :A, 1
end
end
rescue TypeError
success = true
end
assert(success)
success = false
begin
assert_raises Google::Protobuf::TypeError do
s.build do
# message with required field (unsupported in proto3)
add_message "MyMessage" do
required :foo, :int32, 1
end
end
rescue TypeError
success = true
end
assert(success)
end
def test_corecursive

@ -7,121 +7,123 @@ ruby_library(
)
filegroup(
name = "test_protos",
srcs = glob(["*.proto"]),
visibility = [
"//ruby:__subpackages__",
],
name = "test_protos",
srcs = glob(["*.proto"]),
visibility = [
"//ruby:__subpackages__",
],
)
ruby_test(
name = "basic",
srcs = ["basic.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["basic.rb"],
)
ruby_test(
name = "basic_proto2",
srcs = ["basic_proto2.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["basic_proto2.rb"],
)
ruby_test(
name = "encode_decode_test",
srcs = ["encode_decode_test.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["encode_decode_test.rb"],
)
ruby_test(
name = "gc_test",
srcs = ["gc_test.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["gc_test.rb"],
)
ruby_test(
name = "generated_code_test",
srcs = ["generated_code_test.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["generated_code_test.rb"],
)
ruby_test(
name = "multi_level_nesting_test",
srcs = ["multi_level_nesting_test.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["multi_level_nesting_test.rb"],
)
ruby_test(
name = "repeated_field_test",
srcs = ["repeated_field_test.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["repeated_field_test.rb"],
)
ruby_test(
name = "ruby_version",
srcs = ["ruby_version.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["ruby_version.rb"],
)
ruby_test(
name = "stress",
srcs = ["stress.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
srcs = ["stress.rb"],
)
ruby_test(
name = "type_errors",
srcs = ["type_errors.rb"],
deps = [
":common_tests",
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit"
"@protobuf_bundle//:test-unit",
],
)
ruby_test(
name = "well_known_types_test",
srcs = ["well_known_types_test.rb"],
deps = [
"//ruby:protobuf",
"//ruby:test_ruby_protos",
"@protobuf_bundle//:test-unit",
],
srcs = ["type_errors.rb"],
)
pkg_files(
@ -135,4 +137,3 @@ pkg_files(
strip_prefix = strip_prefix.from_root(""),
visibility = ["//ruby:__pkg__"],
)

@ -74,8 +74,8 @@ class TestWellKnownTypes < Test::Unit::TestCase
assert_equal(Google::Protobuf::ListValue.from_a(sublist),
struct["sublist"])
assert_includes struct, "null"
refute_includes struct, "missing_key"
assert struct.has_key? "null"
refute struct.has_key? "missing_key"
should_equal = {
"number" => 12345,
"boolean-true" => true,
@ -90,28 +90,28 @@ class TestWellKnownTypes < Test::Unit::TestCase
}
list = struct["sublist"]
list.is_a?(Google::Protobuf::ListValue)
assert_instance_of Google::Protobuf::ListValue, list
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)
assert_equal should_equal, struct.to_h
# Test that we can safely access a missing key
assert_equal(nil, struct["missing_key"])
assert_nil struct["missing_key"]
# 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)
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)
assert_equal should_equal, struct.to_h
assert_equal should_equal["sublist"].length, struct["sublist"].length
assert_raises Google::Protobuf::UnexpectedStructType do
struct[123] = 5
@ -129,9 +129,9 @@ class TestWellKnownTypes < Test::Unit::TestCase
struct[5] = {123 => 456}
end
struct = Google::Protobuf::Struct.new
struct.fields["foo"] = Google::Protobuf::Value.new
assert_raises Google::Protobuf::UnexpectedStructType do
struct = Google::Protobuf::Struct.new
struct.fields["foo"] = Google::Protobuf::Value.new
# Tries to return a Ruby value for a Value class whose type
# hasn't been filled in.
struct["foo"]
@ -223,20 +223,20 @@ class TestWellKnownTypes < Test::Unit::TestCase
pb = Google::Protobuf::Value.from_ruby('1.23')
assert_equal '1.23', pb.string_value
pb = Google::Protobuf::Value.from_ruby(true)
assert_equal pb.bool_value, true
assert pb.bool_value
pb = Google::Protobuf::Value.from_ruby(false)
refute pb.bool_value
pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true }))
assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true })
assert_equal Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true }), pb.struct_value
pb = Google::Protobuf::Value.from_ruby({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true })
assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true })
assert_equal Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true }), pb.struct_value
pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::ListValue.from_a([1, 2, 3]))
assert_equal pb.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3])
assert_equal Google::Protobuf::ListValue.from_a([1, 2, 3]), pb.list_value
pb = Google::Protobuf::Value.from_ruby([1, 2, 3])
assert_equal pb.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3])
assert_equal Google::Protobuf::ListValue.from_a([1, 2, 3]), pb.list_value
end
end

Loading…
Cancel
Save