Ver Fonte

add eql? method (#5730)

Joe Bolinger há 6 anos atrás
pai
commit
bc929a3e82
2 ficheiros alterados com 22 adições e 0 exclusões
  1. 1 0
      ruby/ext/google/protobuf_c/message.c
  2. 21 0
      ruby/tests/common_tests.rb

+ 1 - 0
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);

+ 21 - 0
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