Skip to content

Commit

Permalink
make tests work on older rubies (protocolbuffers#5823)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbolinger authored and TeBoring committed Mar 7, 2019
1 parent 1568dea commit f4f32a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
11 changes: 7 additions & 4 deletions ruby/tests/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ def test_file_descriptor
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
Expand All @@ -369,10 +372,10 @@ def test_map_freeze
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
57 changes: 26 additions & 31 deletions ruby/tests/common_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -204,15 +207,7 @@ def test_string_encoding

# 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
Expand Down Expand Up @@ -1274,32 +1269,32 @@ def test_freeze
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
Expand Down
9 changes: 6 additions & 3 deletions ruby/tests/type_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
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,
Expand Down Expand Up @@ -151,7 +154,7 @@ def test_bad_bytes

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,
Expand Down

0 comments on commit f4f32a5

Please sign in to comment.