Permalink
Browse files

Response#eql? should compare instance of the same class

  • Loading branch information...
1 parent 9164b96 commit 92f9bbd76efe16d8236ead920e61c151947bf427 @weppos committed Aug 24, 2009
Showing with 34 additions and 10 deletions.
  1. +19 −3 lib/whois/response.rb
  2. +15 −7 test/response_test.rb
View
@@ -46,14 +46,30 @@ def match(pattern)
# or is a string and has the same content.
def ==(other)
(other.equal?(self)) ||
- (other.instance_of?(String) && other == self.to_s)
+ # This option should be deprecated
+ (other.instance_of?(String) && other == self.to_s) ||
+ (other.instance_of?(Response) && other.to_s == self.to_s)
end
# Delegates to ==.
def eql?(other)
self == other
end
-
+
+ # Returns whether this response is equal to <tt>other</tt>.
+ #
+ # Comparing the Response contents is not always as trivial as it seems.
+ # Whois servers sometimes inject dynamic method into the whois response such as
+ # the timestamp the request was generated.
+ # This causes two responses to be different even if they actually should be considered equal
+ # because the registry data didn't change.
+ #
+ # This method should provide a bulletproof way to detect whether this response
+ # changed if compared with <tt>other</tt>.
+ # def equals?
+ #
+ # end
+
# Invokes <tt>match</tt> and returns <tt>true</tt> if <tt>pattern</tt>
# matches <tt>@content</tt>, <tt>false</tt> otherwise.
@@ -83,7 +99,7 @@ def i_m_feeling_lucky(pattern, &block)
def parser
@parser ||= self.class.parser_klass(@server).new(self)
end
-
+
protected
# Delegates all method calls to the internal parser.
View
@@ -37,19 +37,27 @@ def test_match
assert_equal @content.match(/domain\.foo/).to_a, @response.match(/domain\.foo/).to_a
assert_equal @content.match(/google/), @response.match(/google/)
end
-
- def test_equality
+
+ def test_equality_with_self
+ assert_equal @response, @response
+ assert @response.eql?(@response)
+ end
+
+ def test_equality_with_string
assert_equal @response, @content
assert_not_equal @content, @response
assert @response.eql?(@content)
assert !@content.eql?(@response)
end
-
- def test_equality_with_self
- assert_equal @response, @response
- assert @response.eql?(@response)
+
+ def test_equality
+ other = @klass.new(@content, @server)
+ assert_equal @response, other
+ assert_equal other, @response
+ assert @response.eql?(other)
+ assert other.eql?(@response)
end
-
+
def test_match?
assert @response.match?(/domain\.foo/)

0 comments on commit 92f9bbd

Please sign in to comment.