Skip to content

Instantly share code, notes, and snippets.

@joshuaflanagan
Created May 5, 2012 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaflanagan/2605925 to your computer and use it in GitHub Desktop.
Save joshuaflanagan/2605925 to your computer and use it in GitHub Desktop.
DelegateClass instance doesn't eql? itself
require 'test/unit'
require 'delegate'
# These tests demonstrate some unexpected (to me)
# behavior of a class created by DelegateClass.
# An instance of a DelegateClass created class does not eql? itself.
#
# The WidgetTests all pass.
# The comparison and eql WidgetWrapperTests fail.
# ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
class Widget
end
class WidgetWrapper < DelegateClass(Widget)
end
# no surprises here
class WidgetTests < Test::Unit::TestCase
def setup
@widget = Widget.new
end
def test_equal_operator
assert @widget == @widget
end
def test_equal
assert @widget.equal?(@widget)
end
def test_comparison
assert_equal 0, (@widget <=> @widget)
end
def test_eql
assert @widget.eql?(@widget)
end
end
# interesting results...
class WidgetWrapperTests < Test::Unit::TestCase
def setup
@wrapper = WidgetWrapper.new(Widget.new)
end
def test_equal_operator
assert @wrapper == @wrapper
end
def test_equal
assert @wrapper.equal?(@wrapper)
end
def test_comparison
assert_equal 0, (@wrapper <=> @wrapper)
end
def test_eql
assert @wrapper.eql?(@wrapper)
end
end
@greyblake
Copy link

Thanks. It was useful for us to find out why our spec fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment