From bc929a3e82b7a131f9760af96f1baec5b2e4bc9b Mon Sep 17 00:00:00 2001 From: Joe Bolinger Date: Sun, 24 Feb 2019 09:53:24 -0800 Subject: [PATCH] add eql? method (#5730) --- ruby/ext/google/protobuf_c/message.c | 1 + ruby/tests/common_tests.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ruby/ext/google/protobuf_c/message.c b/ruby/ext/google/protobuf_c/message.c index 40111e06cb..5a6a6bcaa0 100644 --- a/ruby/ext/google/protobuf_c/message.c +++ b/ruby/ext/google/protobuf_c/message.c @@ -622,6 +622,7 @@ VALUE build_class_from_descriptor(Descriptor* desc) { // Also define #clone so that we don't inherit Object#clone. rb_define_method(klass, "clone", Message_dup, 0); rb_define_method(klass, "==", Message_eq, 1); + rb_define_method(klass, "eql?", Message_eq, 1); rb_define_method(klass, "hash", Message_hash, 0); rb_define_method(klass, "to_h", Message_to_h, 0); rb_define_method(klass, "to_hash", Message_to_h, 0); diff --git a/ruby/tests/common_tests.rb b/ruby/tests/common_tests.rb index 6e25b4db52..a9c44f5a36 100644 --- a/ruby/tests/common_tests.rb +++ b/ruby/tests/common_tests.rb @@ -1119,4 +1119,25 @@ module CommonTests def test_comparison_with_arbitrary_object assert proto_module::TestMessage.new != nil end + + def test_eq + m1 = proto_module::TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + m2 = proto_module::TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + + h = {} + h[m1] = :yes + + assert m1 == m2 + assert m1.eql?(m2) + assert m1.hash == m2.hash + assert h[m1] == :yes + assert h[m2] == :yes + + m1.optional_int32 = 2 + + assert m1 != m2 + assert !m1.eql?(m2) + assert m1.hash != m2.hash + assert_nil h[m2] + end end