Skip to content

Commit 8d8e168

Browse files
habermancopybara-github
authored andcommitted
Added a test that verifies existing enum behavior for Ruby.
Note that this is a behavior where JRuby "native" has different behavior than CRuby or JRuby FFI. The CRuby / FFI behavior is a longstanding nonconformance, which is to treat all enums as open. While this is nonconformant, we keep this behavior because it is more useful than respecting closed enum semantics. PiperOrigin-RevId: 850508608
1 parent 667f9ed commit 8d8e168

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ruby/tests/basic_proto2.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ def test_set_clear_defaults
191191
refute m.has_my_oneof?
192192
end
193193

194+
def test_enums_are_open
195+
# Normally proto2 enums would be closed, but Ruby treats all enums as
196+
# open.
197+
m = TestMessage.new
198+
m.optional_enum = 999 # Not a valid enum value.
199+
assert_equal 999, m.optional_enum
200+
201+
serialized = TestMessage.encode(m)
202+
m2 = TestMessage.decode(serialized)
203+
204+
if RUBY_PLATFORM == "java" && Google::Protobuf::IMPLEMENTATION == :NATIVE
205+
# When JRuby is using Java Protobuf, it does not have this special
206+
# behavior of treating all enums as open.
207+
refute m2.has_optional_enum?
208+
else
209+
assert m2.has_optional_enum?
210+
assert_equal 999, m2.optional_enum
211+
end
212+
end
213+
194214
def test_assign_nil
195215
m = TestMessageDefaults.new
196216
m.optional_msg = TestMessage2.new(:foo => 42)

0 commit comments

Comments
 (0)