Commit f4f32a5b authored by Joe Bolinger's avatar Joe Bolinger Committed by Paul Yang

make tests work on older rubies (#5823)

parent 1568deab
......@@ -358,6 +358,9 @@ module BasicTest
assert_equal :proto3, file_descriptor.syntax
end
# Ruby 2.5 changed to raise FrozenError instead of RuntimeError
FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
def test_map_freeze
m = proto_module::MapMessage.new
m.map_string_int32['a'] = 5
......@@ -369,10 +372,10 @@ module BasicTest
assert m.map_string_int32.frozen?
assert m.map_string_msg.frozen?
assert_raise(FrozenError) { m.map_string_int32['foo'] = 1 }
assert_raise(FrozenError) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
assert_raise(FrozenError) { m.map_string_int32.delete('a') }
assert_raise(FrozenError) { m.map_string_int32.clear }
assert_raise(FrozenErrorType) { m.map_string_int32['foo'] = 1 }
assert_raise(FrozenErrorType) { m.map_string_msg['bar'] = proto_module::TestMessage2.new }
assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
assert_raise(FrozenErrorType) { m.map_string_int32.clear }
end
end
end
......@@ -4,6 +4,9 @@
# and that the including class should define a 'proto_module' method which returns
# the enclosing module of the proto message classes.
module CommonTests
# Ruby 2.5 changed to raise FrozenError instead of RuntimeError
FrozenErrorType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5') ? RuntimeError : FrozenError
def test_defaults
m = proto_module::TestMessage.new
assert m.optional_int32 == 0
......@@ -204,15 +207,7 @@ module CommonTests
# strings are immutable so we can't do this, but serialize should catch it.
m.optional_string = "asdf".encode!('UTF-8')
# Ruby 2.5 changed to raise FrozenError. However, assert_raise don't
# accept subclass.
ok = false
begin
m.optional_string.encode!('ASCII-8BIT')
rescue RuntimeError
ok = true
end
assert ok
assert_raise(FrozenErrorType) { m.optional_string.encode!('ASCII-8BIT') }
end
def test_rptfield_int32
......@@ -1274,32 +1269,32 @@ module CommonTests
m.optional_int32 = 10
m.freeze
frozen_error = assert_raise(FrozenError) { m.optional_int32 = 20 }
frozen_error = assert_raise(FrozenErrorType) { m.optional_int32 = 20 }
assert_equal "can't modify frozen #{proto_module}::TestMessage", frozen_error.message
assert_equal 10, m.optional_int32
assert_equal true, m.frozen?
assert_raise(FrozenError) { m.optional_int64 = 2 }
assert_raise(FrozenError) { m.optional_uint32 = 3 }
assert_raise(FrozenError) { m.optional_uint64 = 4 }
assert_raise(FrozenError) { m.optional_bool = true }
assert_raise(FrozenError) { m.optional_float = 6.0 }
assert_raise(FrozenError) { m.optional_double = 7.0 }
assert_raise(FrozenError) { m.optional_string = '8' }
assert_raise(FrozenError) { m.optional_bytes = nil }
assert_raise(FrozenError) { m.optional_msg = proto_module::TestMessage2.new }
assert_raise(FrozenError) { m.optional_enum = :A }
assert_raise(FrozenError) { m.repeated_int32 = 1 }
assert_raise(FrozenError) { m.repeated_int64 = 2 }
assert_raise(FrozenError) { m.repeated_uint32 = 3 }
assert_raise(FrozenError) { m.repeated_uint64 = 4 }
assert_raise(FrozenError) { m.repeated_bool = true }
assert_raise(FrozenError) { m.repeated_float = 6.0 }
assert_raise(FrozenError) { m.repeated_double = 7.0 }
assert_raise(FrozenError) { m.repeated_string = '8' }
assert_raise(FrozenError) { m.repeated_bytes = nil }
assert_raise(FrozenError) { m.repeated_msg = proto_module::TestMessage2.new }
assert_raise(FrozenError) { m.repeated_enum = :A }
assert_raise(FrozenErrorType) { m.optional_int64 = 2 }
assert_raise(FrozenErrorType) { m.optional_uint32 = 3 }
assert_raise(FrozenErrorType) { m.optional_uint64 = 4 }
assert_raise(FrozenErrorType) { m.optional_bool = true }
assert_raise(FrozenErrorType) { m.optional_float = 6.0 }
assert_raise(FrozenErrorType) { m.optional_double = 7.0 }
assert_raise(FrozenErrorType) { m.optional_string = '8' }
assert_raise(FrozenErrorType) { m.optional_bytes = nil }
assert_raise(FrozenErrorType) { m.optional_msg = proto_module::TestMessage2.new }
assert_raise(FrozenErrorType) { m.optional_enum = :A }
assert_raise(FrozenErrorType) { m.repeated_int32 = 1 }
assert_raise(FrozenErrorType) { m.repeated_int64 = 2 }
assert_raise(FrozenErrorType) { m.repeated_uint32 = 3 }
assert_raise(FrozenErrorType) { m.repeated_uint64 = 4 }
assert_raise(FrozenErrorType) { m.repeated_bool = true }
assert_raise(FrozenErrorType) { m.repeated_float = 6.0 }
assert_raise(FrozenErrorType) { m.repeated_double = 7.0 }
assert_raise(FrozenErrorType) { m.repeated_string = '8' }
assert_raise(FrozenErrorType) { m.repeated_bytes = nil }
assert_raise(FrozenErrorType) { m.repeated_msg = proto_module::TestMessage2.new }
assert_raise(FrozenErrorType) { m.repeated_enum = :A }
end
def test_eq
......
......@@ -8,13 +8,16 @@ require 'google/protobuf/well_known_types'
require 'generated_code_pb'
class TestTypeErrors < Test::Unit::TestCase
# Ruby 2.4 unified Fixnum with Integer
IntegerType = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4') ? Fixnum : Integer
def test_bad_string
check_error Google::Protobuf::TypeError,
"Invalid argument for string field 'optional_string' (given Integer)." do
"Invalid argument for string field 'optional_string' (given #{IntegerType.name})." do
A::B::C::TestMessage.new(optional_string: 4)
end
check_error Google::Protobuf::TypeError,
"Invalid argument for string field 'oneof_string' (given Integer)." do
"Invalid argument for string field 'oneof_string' (given #{IntegerType.name})." do
A::B::C::TestMessage.new(oneof_string: 4)
end
check_error ArgumentError,
......@@ -151,7 +154,7 @@ class TestTypeErrors < Test::Unit::TestCase
def test_bad_msg
check_error Google::Protobuf::TypeError,
"Invalid type Integer to assign to submessage field 'optional_msg'." do
"Invalid type #{IntegerType.name} to assign to submessage field 'optional_msg'." do
A::B::C::TestMessage.new(optional_msg: 2)
end
check_error Google::Protobuf::TypeError,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment