Skip to content
Browse files

Converted #changed? and #unchanged? properties to methods and normali…

…zed their behavior.
  • Loading branch information...
1 parent 387ebe4 commit 7ba3de955f642b275ebb7f5554c51f76b030f9e9 @weppos committed Nov 27, 2010
View
6 CHANGELOG.rdoc
@@ -13,12 +13,16 @@
* FIXED: whois.dns.pt must support :reserved status.
+* FIXED: Converted #changed? and #unchanged? properties to methods
+ and normalized their behavior.
+
== Release 1.6.4
* FIXED: With some .pl domains, whois.dns.pl parser returns invalid values for nameservers.
-* FIXED: The whois.pnina.ps server injects a NODNS.NS string when a nameserver is not set. The parser should ignore it.
+* FIXED: The whois.pnina.ps server injects a NODNS.NS string when a nameserver is not set.
+ The parser should ignore it.
* FIXED: whois.nic.ve must support :suspended status.
View
124 lib/whois/answer.rb
@@ -60,104 +60,116 @@ def ==(other)
(other.is_a?(Answer) && other.to_s == self.to_s)
end
- # Delegates to ==.
- def eql?(other)
- self == other
- end
+ alias_method :eql?, :==
- # This method joins and returns all answer parts into a single string
+ # Joins and returns all answer parts into a single string
# and separates each response with a newline character.
#
+ # @example Answer with one part
# answer = Whois::Answer.new([Whois::Answer::Part.new("First answer.")])
# answer.content
# # => "First answer."
#
+ # @example Answer with multiple parts
# answer = Whois::Answer.new([Whois::Answer::Part.new("First answer."), Whois::Answer::Part.new("Second answer.")])
# answer.content
# # => "First answer.\nSecond answer."
#
- # ==== Returns
- #
- # String:: The content of this answer.
+ # @return [String] The content of this answer.
#
def content
@content ||= parts.map(&:body).join("\n")
end
+ # Lazy-loads and returns the parser proxy for current answer.
+ #
+ # @return [Whois::Answer::Parser]
+ def parser
+ @parser ||= Parser.new(self)
+ end
+
- # Returns whether this answer changed compared to <tt>other</tt>.
+ # Returns a Hash containing all supported properties for this answer
+ # along with corresponding values.
#
- # Comparing the Answer contents is not always as trivial as it seems.
- # Whois servers sometimes inject dynamic method into the whois answer such as
- # the timestamp the request was generated.
- # This causes two answers to be different even if they actually should be considered equal
- # because the registry data didn't change.
+ # @return [Hash]
+ def properties
+ hash = {}
+ Parser::PROPERTIES.each { |property| hash[property] = send(property) }
+ hash
+ end
+
+ # Returns <tt>true</tt> if the <tt>property</tt> passed as symbol
+ # is supported by any available parser for this answer.
+ # See also <tt>Whois::Answer::Parser.supported?</tt>.
#
- # This method should provide a bulletproof way to detect whether this answer
- # changed if compared with <tt>other</tt>.
+ # @param [Symbol] property - The name of the property to check.
+ # @return [Boolean]
#
- # ==== Parameters
+ # @see Whois::Answer::Parser#property_supported?
#
- # other:: The Whois::Answer to compare.
+ def property_supported?(property)
+ parser.property_supported?(property)
+ end
+
+
+ # Checks whether this Answer is different than +other+.
+ #
+ # Comparing the Answer content is not as trivial as you may think.
+ # WHOIS servers can inject into the WHOIS response strings that changes at every request,
+ # such as the timestamp the request was generated or the number of requests left
+ # for your current IP.
#
- # ==== Returns
+ # These strings causes a simple equal comparison to fail even if
+ # the registry data is the same.
+ #
+ # This method should provide a bulletproof way to detect whether this answer
+ # changed compared with +other+.
#
- # Boolean
+ # @param [Whois::Answer] other The other answer instance to compare.
+ # @return [Boolean]
+ #
+ # @see Whois::Answer::Parser#changed?
#
def changed?(other)
!unchanged?(other)
end
- # The opposite of <tt>changed?</tt>.
- def unchanged?(other)
- self == other ||
- parser.unchanged?(other.parser)
- end
-
- def contacts
- parser.contacts
- end
-
-
- # Lazy-loads and returns the parser proxy for current answer.
+ # The opposite of {#changed?}.
#
- # ==== Returns
+ # @param [Whois::Answer] other The other answer instance to compare.
+ # @return [Boolean]
#
- # Whois::Answer::Parser
+ # @see Whois::Answer::Parser#unchanged?
#
- def parser
- @parser ||= Parser.new(self)
- end
+ def unchanged?(other)
+ unless other.is_a?(self.class)
+ raise(ArgumentError, "Can't compare `#{self.class}' with `#{other.class}'")
+ end
+ equal?(other) ||
+ parser.unchanged?(other.parser)
+ end
- # Returns a Hash containing all supported properties for this answer
- # along with corresponding values.
+ # Collects and returns all the contacts.
#
- # ==== Returns
+ # @return [Array<Whois::Aswer::Contact>]
#
- # Hash
+ # @see Whois::Answer::Parser#contacts?
#
- def properties
- hash = {}
- Parser::PROPERTIES.each { |property| hash[property] = send(property) }
- hash
+ def contacts
+ parser.contacts
end
- # Returns <tt>true</tt> if the <tt>property</tt> passed as symbol
- # is supported by any available parser for this answer.
- # See also <tt>Whois::Answer::Parser.supported?</tt>.
- #
- # ==== Parameters
+ # Checks whether this is a throttle response.
#
- # property:: A Symbol with the property name to check.
+ # @return [Boolean]
#
- # ==== Returns
+ # @see Whois::Answer::Parser#throttle?
#
- # Boolean
- #
- def property_supported?(property)
- parser.property_supported?(property)
+ def throttle?
+ parser.throttle?
end
View
68 lib/whois/answer/parser.rb
@@ -23,9 +23,9 @@ class Answer
class Parser
METHODS = [
- :contacts, :throttle?,
- # deprecated methods
- :registrant, :admin, :technical,
+ :changed?, :unchanged?,
+ :contacts,
+ # :throttle?,
]
PROPERTIES = [
@@ -61,11 +61,60 @@ def property_supported?(property)
end
- # Collects and returns all the contacts from all parsers.
+ # Loop through all the answer parts to check if at least
+ # one part changed.
+ #
+ # @param [Whois::Answer::Parser] other The other parser instance to compare.
+ # @return [Boolean]
+ #
+ # @see Whois::Answer#changed?
+ # @see Whois::Answer::Parser::Base#changed?
+ #
+ def changed?(other)
+ !unchanged?(other)
+ end
+
+ # The opposite of {#changed?}.
+ #
+ # @param [Whois::Answer::Parser] other The other parser instance to compare.
+ # @return [Boolean]
+ #
+ # @see Whois::Answer#unchanged?
+ # @see Whois::Answer::Parser::Base#unchanged?
+ #
+ def unchanged?(other)
+ unless other.is_a?(self.class)
+ raise(ArgumentError, "Can't compare `#{self.class}' with `#{other.class}'")
+ end
+
+ equal?(other) ||
+ parsers.size == other.parsers.size &&
+ all_with_args?(parsers, other.parsers) { |one, two| one.unchanged?(two) }
+ end
+
+ # Collects and returns all the contacts from all the answer parts.
+ #
+ # @return [Array<Whois::Aswer::Contact>]
+ #
+ # @see Whois::Answer#contacts?
+ # @see Whois::Answer::Parser::Base#contacts?
+ #
def contacts
parsers.inject([]) { |all, parser| all.concat(parser.contacts) }
end
+ # Loop through all the answer parts to check if at least
+ # one part is a throttle response.
+ #
+ # @return [Boolean]
+ #
+ # @see Whois::Answer#throttle?
+ # @see Whois::Answer::Parser::Base#throttle?
+ #
+ def throttle?
+ parsers.any?(&:throttle?)
+ end
+
private
@@ -156,6 +205,17 @@ def select_parser(property, status = :any)
nil
end
+ def all_with_args?(*args, &block)
+ count = args.first.size
+ index = 0
+
+ while index < count
+ return false unless yield(*args.map { |arg| arg[index] })
+ index += 1
+ end
+ true
+ end
+
# Returns the proper parser instance for given <tt>part</tt>.
# The parser class is selected according to the
View
95 lib/whois/answer/parser/base.rb
@@ -60,7 +60,7 @@ def self.property_registry(klass = nil)
end
end
- # Returns the status for <tt>property</tt> passed as symbol.
+ # Returns the status for the <tt>property</tt> passed as symbol.
#
# property_status(:disclaimer)
# # => nil
@@ -114,15 +114,6 @@ def self.property_registered?(property, status = :any)
#
# property_registered?(:disclaimer)
# # => true
- #
- #
- # register_property(:changed?, :implemented) do |other|
- # ...
- # end
- #
- # # def changed?(other)
- # # ...
- # # end
#
def self.register_property(property, status, &block)
property = property.to_s.to_sym
@@ -184,8 +175,21 @@ def self.property_supported(property, &block)
register_property(property, :supported, &block)
end
+ # Returns <tt>true</tt> if the <tt>property</tt> passed as symbol
+ # is supported by the current parser.
+ #
+ # @param [Symbol] property The name of the property to check.
+ # @return [Boolean]
+ #
+ def property_supported?(property)
+ self.class.property_registered?(property, :supported)
+ end
+ # Initializes a new parser with given +part+.
+ #
+ # @param [Whois::Answer::Part] part
+ #
def initialize(part)
@part = part
end
@@ -194,7 +198,7 @@ def part
@part
end
- # This is an internal method primaly used as a common access point
+ # This is an internal method primary used as a common access point
# to get the content to be parsed as a string.
#
# The main reason behind this method is because I changed the internal
@@ -210,21 +214,59 @@ def content
end
- # Returns <tt>true</tt> if the <tt>property</tt> passed as symbol
- # is supported by the current parser.
- def property_supported?(property)
- self.class.property_registered?(property, :supported)
- end
-
Whois::Answer::Parser::PROPERTIES.each do |property|
property_not_implemented(property)
end
- # Gets all supported contacts merged into a single Array.
+ # Checks whether the content of this part is different than +other+.
+ #
+ # Comparing a WHOIS response is not as trivial as you may think.
+ # WHOIS servers can inject into the WHOIS response strings that changes at every request,
+ # such as the timestamp the request was generated or the number of requests left
+ # for your current IP.
+ #
+ # These strings causes a simple equal comparison to fail even if
+ # the registry data is the same.
+ #
+ # This method should provide a bulletproof way to detect whether this answer
+ # changed compared with +other+.
+ #
+ # @param [Base] other The other parser instance to compare.
+ # @return [Boolean]
+ #
+ # @see Whois::Answer#changed?
+ # @see Whois::Answer::Parser#changed?
+ #
+ def changed?(other)
+ !unchanged?(other)
+ end
+
+ # The opposite of {#changed?}.
+ #
+ # @param [Base] other The other parser instance to compare.
+ # @return [Boolean]
+ #
+ # @see Whois::Answer#unchanged?
+ # @see Whois::Answer::Parser#unchanged?
+ #
+ def unchanged?(other)
+ unless other.is_a?(self.class)
+ raise(ArgumentError, "Can't compare `#{self.class}' with `#{other.class}'")
+ end
+
+ equal?(other) ||
+ content_for_scanner == other.content_for_scanner
+ end
+
+ # Collects and returns all the available contacts.
+ #
+ # @return [Array<Whois::Aswer::Contact>]
+ #
+ # @see Whois::Answer#contacts
+ # @see Whois::Answer::Parser#contacts
#
- # Returns an Array of Whois::Aswer::Contact.
def contacts
contacts = []
contacts.concat(registrant_contact.is_a?(Array) ? registrant_contact : [registrant_contact]) if property_supported?(:registrant_contact)
@@ -233,14 +275,16 @@ def contacts
contacts.compact
end
- # NEWPROPERTY
- #
# Checks whether this is a throttle response.
- # The default implementation always returns false.
+ # The default implementation always returns +false+.
#
- # This method is intended to be overrridden by child classes.
+ # @abstract This method returns false by default.
#
- # Returns false by default.
+ # @return [false]
+ #
+ # @see Whois::Answer#throttle?
+ # @see Whois::Answer::Parser#throttle?
+ #
def throttle?
false
end
@@ -252,9 +296,8 @@ def content_for_scanner
@content_for_scanner ||= content.to_s.gsub(/\r\n/, "\n")
end
-
end
end
end
-end
+end
View
24 lib/whois/answer/parser/example.rb
@@ -160,30 +160,6 @@ class WhoisExampleCom < Base
@nameservers ||= []
end
-
- # Public: Checks whether this answer is different than <tt>other</tt>.
- #
- # Comparing the Answer contents is not always as trivial as it seems.
- # Whois servers sometimes inject dynamic method into the whois answer such as
- # the timestamp the request was generated.
- # This causes two answers 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 answer
- # changed if compared with <tt>other</tt>.
- #
- # Returns true/false depending whether this answer is different than <tt>other</tt>.
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- # Public: The opposite of <tt>#changed?</tt>.
- #
- # Returns true/false depending whether this answer is not different than <tt>other</tt>.
- property_supported :unchanged? do |other|
- false
- end
-
end
end
View
12 lib/whois/answer/parser/whois.co.pl.rb
@@ -87,18 +87,6 @@ class WhoisCoPl < Base
@nameservers ||= content_for_scanner.scan(/nserver:\s+(.+)\n/).flatten
end
-
- # NEWPROPERTY
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- property_supported :unchanged? do |other|
- (self === other) ||
- (self.content == other.content)
- end
-
end
end
View
12 lib/whois/answer/parser/whois.coza.net.za.rb
@@ -78,18 +78,6 @@ class WhoisCozaNetZa < Base
property_not_supported :nameservers
- # NEWPROPERTY
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- property_supported :unchanged? do |other|
- (self === other) ||
- (self.content == other.content)
- end
-
-
protected
# Very often the .to server returns a partial response, which is a response
View
12 lib/whois/answer/parser/whois.nic.ac.rb
@@ -80,18 +80,6 @@ class WhoisNicAc < Base
property_not_supported :nameservers
-
- # NEWPROPERTY
- def changed?(other)
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- def unchanged?(other)
- self.equal?(other) ||
- self.content_for_scanner == other.content_for_scanner
- end
-
end
end
View
12 lib/whois/answer/parser/whois.nic.io.rb
@@ -80,18 +80,6 @@ class WhoisNicIo < Base
property_not_supported :nameservers
-
- # NEWPROPERTY
- def changed?(other)
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- def unchanged?(other)
- self.equal?(other) ||
- self.content_for_scanner == other.content_for_scanner
- end
-
end
end
View
10 lib/whois/answer/parser/whois.nic.it.rb
@@ -101,16 +101,6 @@ class WhoisNicIt < Base
end
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- property_supported :unchanged? do |other|
- (self === other) ||
- (self.content == other.content)
- end
-
-
protected
def parse
View
12 lib/whois/answer/parser/whois.nic.sh.rb
@@ -80,18 +80,6 @@ class WhoisNicSh < Base
property_not_supported :nameservers
-
- # NEWPROPERTY
- def changed?(other)
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- def unchanged?(other)
- self.equal?(other) ||
- self.content_for_scanner == other.content_for_scanner
- end
-
end
end
View
12 lib/whois/answer/parser/whois.nic.sn.rb
@@ -105,18 +105,6 @@ class WhoisNicSn < Base
end
- # NEWPROPERTY
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- property_supported :unchanged? do |other|
- (self.equal? other) ||
- (self.content == other.content)
- end
-
-
private
def contact(string)
View
12 lib/whois/answer/parser/whois.nic.tm.rb
@@ -80,18 +80,6 @@ class WhoisNicTm < Base
property_not_supported :nameservers
-
- # NEWPROPERTY
- def changed?(other)
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- def unchanged?(other)
- self.equal?(other) ||
- self.content_for_scanner == other.content_for_scanner
- end
-
end
end
View
12 lib/whois/answer/parser/whois.org.za.rb
@@ -77,18 +77,6 @@ class WhoisOrgZa < Base
property_not_supported :nameservers
-
- # NEWPROPERTY
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- property_supported :unchanged? do |other|
- (self === other) ||
- (self.content == other.content)
- end
-
end
end
View
11 lib/whois/answer/parser/whois.publicinterestregistry.net.rb
@@ -103,17 +103,6 @@ class WhoisPublicinterestregistryNet < Base
# NEWPROPERTY
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- property_supported :unchanged? do |other|
- self == other ||
- self.content.to_s == other.content.to_s
- end
-
- # NEWPROPERTY
def throttle?
!!node("status-throttle")
end
View
12 lib/whois/answer/parser/whois.tonic.to.rb
@@ -82,18 +82,6 @@ class WhoisTonicTo < Base
property_not_supported :nameservers
- # NEWPROPERTY
- property_supported :changed? do |other|
- !unchanged?(other)
- end
-
- # NEWPROPERTY
- property_supported :unchanged? do |other|
- (self === other) ||
- (self.content == other.content)
- end
-
-
protected
# Very often the .to server returns a partial response, which is a response
View
10 spec/spec_helper.rb
@@ -17,6 +17,16 @@ def with_definitions(&block)
Whois::Server.send :class_variable_set, :@@definitions, @_definitions
end
+ # Temporary resets parser @@registry
+ # to let the test setup a custom registry.
+ def with_registry(&block)
+ @_property_registry = Whois::Answer::Parser::Base.send :class_variable_get, :@@property_registry
+ Whois::Answer::Parser::Base.send :class_variable_set, :@@property_registry, {}
+ yield
+ ensure
+ Whois::Answer::Parser::Base.send :class_variable_set, :@@property_registry, @_property_registry
+ end
+
# Gets the currently described class.
# Conversely to +subject+, it returns the class
# instead of an instance.
View
156 spec/whois/answer/parser/base_spec.rb
@@ -0,0 +1,156 @@
+require "spec_helper"
+
+describe Whois::Answer::Parser::Base do
+
+ before(:each) do
+ @part = Whois::Answer::Part.new("This is the response.", "whois.example.test")
+ end
+
+
+ describe ".property_registry" do
+ it "returns the @@registry variable when no argument is passed" do
+ with_registry do
+ klass.property_registry.should == klass.send(:class_variable_get, :@@property_registry)
+ end
+ end
+
+ it "returns the hash for given class when class argument is passed" do
+ with_registry do
+ test_parser = Class.new(klass)
+ klass.send(:class_variable_set, :@@property_registry, { test_parser => {} })
+
+ klass.property_registry[test_parser].should == Hash.new
+ end
+ end
+
+ it "lazy initializes the hash for given class" do
+ with_registry do
+ test_parser = Class.new(klass)
+ klass.send(:class_variable_set, :@@property_registry, Hash.new)
+ klass.property_registry[test_parser]
+ klass.send(:class_variable_get, :@@property_registry).should == {}
+ end
+ end
+ end
+
+ describe "#property_supported?" do
+ it "returns false if the property is not supported" do
+ pclass = Class.new(klass) do
+ end
+ pclass.new(@part).property_supported?(:disclaimer).should be_false
+ pclass.new(@part).respond_to?(:disclaimer).should be_true
+ end
+
+ it "returns true if the property is supported" do
+ pclass = Class.new(klass) do
+ register_property(:disclaimer, :supported) {}
+ end
+ pclass.new(@part).property_supported?(:disclaimer).should be_true
+ pclass.new(@part).respond_to?(:disclaimer).should be_true
+ end
+ end
+
+
+ describe ".new" do
+ it "requires a part" do
+ lambda { klass.new }.should raise_error(ArgumentError)
+ lambda { klass.new(@part) }.should_not raise_error
+ end
+
+ it "sets the part" do
+ klass.new(@part).part.should be(@part)
+ end
+ end
+
+ describe "#content" do
+ it "returns the part body" do
+ instance = klass.new(@part)
+ instance.content.should be(@part.body)
+ end
+ end
+
+ describe "#content_for_scanner" do
+ it "returns the part body with line feed normalized" do
+ instance = klass.new(Whois::Answer::Part.new("This is\r\nthe response.", "whois.example.test"))
+ instance.send(:content_for_scanner).should == "This is\nthe response."
+ end
+
+ it "caches the result" do
+ instance = klass.new(Whois::Answer::Part.new("This is\r\nthe response.", "whois.example.test"))
+ instance.instance_eval { @content_for_scanner }.should be_nil
+ instance.send(:content_for_scanner)
+ instance.instance_eval { @content_for_scanner }.should == "This is\nthe response."
+ end
+ end
+
+
+ describe "#changed?" do
+ it "raises if the argument is not an instance of the same class" do
+ lambda do
+ klass.new(@part).changed?(Object.new)
+ end.should raise_error
+
+ lambda do
+ klass.new(@part).changed?(klass.new(@part))
+ end.should_not raise_error
+ end
+ end
+
+ describe "#unchanged?" do
+ it "raises if the argument is not an instance of the same class" do
+ lambda do
+ klass.new(@part).unchanged?(Object.new)
+ end.should raise_error
+
+ lambda do
+ klass.new(@part).unchanged?(klass.new(@part))
+ end.should_not raise_error
+ end
+
+ it "returns true if self and other references the same object" do
+ instance = klass.new(@part)
+ instance.unchanged?(instance).should be_true
+ end
+
+ it "returns true if the content_for_scanner is the same" do
+ instance = klass.new(Whois::Answer::Part.new("This is the\nresponse 1.", "whois.example.test"))
+ other = klass.new(Whois::Answer::Part.new("This is the\r\nresponse 1.", "whois.example.test"))
+ instance.unchanged?(other).should be_true
+ end
+
+ it "returns false if the content_for_scanner is not the same" do
+ instance = klass.new(Whois::Answer::Part.new("This is the response 1.", "whois.example.test"))
+ other = klass.new(Whois::Answer::Part.new("This is the response 2.", "whois.example.test"))
+ instance.unchanged?(other).should be_false
+ end
+ end
+
+ describe "#contacts" do
+ it "returns an array of contacts" do
+ c1 = Whois::Answer::Contact.new(:id => "1st", :name => "foo")
+ c2 = Whois::Answer::Contact.new(:id => "2nd", :name => "foo")
+ c3 = Whois::Answer::Contact.new(:id => "3rd", :name => "foo")
+ pclass = Class.new(klass) do
+ register_property(:registrant_contact, :supported) { [c1, c2] }
+ register_property(:admin_contact, :supported) { nil }
+ register_property(:technical_contact, :supported) { c3 }
+ end
+
+ instance = pclass.new(@part)
+ instance.contacts.should == [c1, c2, c3]
+ end
+
+ it "returns an empty array when no contact is supported" do
+ instance = klass.new(@part)
+ instance.contacts.should == []
+ end
+ end
+
+ describe "#throttle?" do
+ it "returns false" do
+ instance = klass.new(@part)
+ instance.throttle?.should be_false
+ end
+ end
+
+end
View
65 spec/whois/answer/parser_spec.rb
@@ -0,0 +1,65 @@
+require "spec_helper"
+
+describe Whois::Answer::Parser do
+
+ before(:each) do
+ @answer = Whois::Answer.new(nil, [])
+ end
+
+
+ describe "#changed?" do
+ it "raises if the argument is not an instance of the same class" do
+ lambda do
+ klass.new(@answer).changed?(Object.new)
+ end.should raise_error
+
+ lambda do
+ klass.new(@answer).changed?(klass.new(@answer))
+ end.should_not raise_error
+ end
+ end
+
+ describe "#unchanged?" do
+ it "raises if the argument is not an instance of the same class" do
+ lambda do
+ klass.new(@answer).unchanged?(Object.new)
+ end.should raise_error
+
+ lambda do
+ klass.new(@answer).unchanged?(klass.new(@answer))
+ end.should_not raise_error
+ end
+
+ it "returns true if self and other references the same object" do
+ instance = klass.new(@answer)
+ instance.unchanged?(instance).should be_true
+ end
+
+ it "returns false if parser and other.parser have different number of elements" do
+ instance = klass.new(Whois::Answer.new(nil, []))
+ other = klass.new(Whois::Answer.new(nil, [Whois::Answer::Part.new("", "foo.example.test")]))
+ instance.unchanged?(other).should be_false
+ end
+
+ it "returns true if parsers and other.parsers have 0 elements" do
+ instance = klass.new(Whois::Answer.new(nil, []))
+ other = klass.new(Whois::Answer.new(nil, []))
+ instance.unchanged?(other).should be_true
+ end
+ end
+
+ it "returns true if every parser in self marches the corresponding parser in other" do
+ instance = klass.new(Whois::Answer.new(nil, [Whois::Answer::Part.new("hello", "foo.example.test"), Whois::Answer::Part.new("world", "bar.example.test")]))
+ other = klass.new(Whois::Answer.new(nil, [Whois::Answer::Part.new("hello", "foo.example.test"), Whois::Answer::Part.new("world", "bar.example.test")]))
+
+ instance.unchanged?(other).should be_true
+ end
+
+ it "returns false unless every parser in self marches the corresponding parser in other" do
+ instance = klass.new(Whois::Answer.new(nil, [Whois::Answer::Part.new("hello", "foo.example.test"), Whois::Answer::Part.new("world", "bar.example.test")]))
+ other = klass.new(Whois::Answer.new(nil, [Whois::Answer::Part.new("hello", "foo.example.test"), Whois::Answer::Part.new("baby!", "bar.example.test")]))
+
+ instance.unchanged?(other).should be_false
+ end
+
+end
View
62 spec/whois/answer_spec.rb
@@ -0,0 +1,62 @@
+require "spec_helper"
+
+describe Whois::Answer do
+
+ before(:each) do
+ @server = Whois::Server.factory(:tld, ".foo", "whois.example.test")
+ @parts = [
+ Whois::Answer::Part.new("This is a answer from foo.", "foo.example.test"),
+ Whois::Answer::Part.new("This is a answer from bar.", "bar.example.test")
+ ]
+ @content = "This is a answer from foo.\nThis is a answer from bar."
+ @answer = klass.new(@server, @parts)
+ end
+
+
+ describe "#changed?" do
+ it "raises if the argument is not an instance of the same class" do
+ lambda do
+ klass.new(nil, []).changed?(Object.new)
+ end.should raise_error
+
+ lambda do
+ klass.new(nil, []).changed?(klass.new(nil, []))
+ end.should_not raise_error
+ end
+ end
+
+ describe "#unchanged?" do
+ it "raises if the argument is not an instance of the same class" do
+ lambda do
+ klass.new(nil, []).unchanged?(Object.new)
+ end.should raise_error
+
+ lambda do
+ klass.new(nil, []).unchanged?(klass.new(nil, []))
+ end.should_not raise_error
+ end
+
+ it "returns true if self and other references the same object" do
+ instance = klass.new(nil, [])
+ instance.unchanged?(instance).should be_true
+ end
+
+ it "delegates to #parser if self and other references different objects" do
+ other = klass.new(nil, @parts)
+ instance = klass.new(nil, @parts)
+ instance.parser.expects(:unchanged?).with(other.parser)
+
+ instance.unchanged?(other)
+ end
+ end
+
+ describe "#throttle?" do
+ it "delegates to #parser" do
+ instance = klass.new(nil, @parts)
+ instance.parser.expects(:throttle?)
+
+ instance.throttle?
+ end
+ end
+
+end
View
84 test/whois/answer/parser/base_test.rb
@@ -1,84 +0,0 @@
-require 'test_helper'
-require 'whois/answer/parser/base'
-
-class AnswerParserBaseTest < Test::Unit::TestCase
-
- def setup
- @klass = Whois::Answer::Parser::Base
- @part = Whois::Answer::Part.new("This is the response.", "whois.foo.com")
- end
-
-
- def test_initialize
- parser = @klass.new(@part)
- assert_instance_of @klass, parser
- end
-
- def test_initialize_should_require_part
- assert_raise(ArgumentError) { @klass.new }
- end
-
-
- def test_content
- parser = @klass.new(@part)
- assert_equal @part.body, parser.content
- end
-
- def test_content_for_scanner
- parser = @klass.new(Whois::Answer::Part.new("This is\r\nthe response.", "whois.foo.com"))
- assert_equal "This is\nthe response.", parser.send(:content_for_scanner)
- assert_equal "This is\nthe response.", parser.instance_variable_get(:"@content_for_scanner")
- end
-
-
- def test_property_supported?
- klass = Class.new(@klass) do
- end
- assert !klass.new(@part).property_supported?(:disclaimer)
- assert klass.new(@part).respond_to?(:disclaimer)
-
- klass = Class.new(@klass) do
- register_property(:disclaimer, :supported) {}
- end
- assert klass.new(@part).property_supported?(:disclaimer)
- assert klass.new(@part).respond_to?(:disclaimer)
- end
-
-
- def test_contacts_should_return_empty_array_with_all_unsupported
- parser = @klass.new(@part)
- assert_equal [], parser.contacts
- end
-
- def test_contacts_should_return_all_supported_contacts
- c1 = Whois::Answer::Contact.new(:id => "1st", :name => "foo")
- c2 = Whois::Answer::Contact.new(:id => "2nd", :name => "foo")
- c3 = Whois::Answer::Contact.new(:id => "3rd", :name => "foo")
- klass = Class.new(@klass) do
- register_property(:registrant_contact, :supported) { [c1, c2] }
- register_property(:admin_contact, :supported) { nil }
- register_property(:technical_contact, :supported) { c3 }
- end
- assert_equal ["1st", "2nd", "3rd"], klass.new(@part).contacts.map(&:id)
- end
-
-
- def test_throttle_question
- assert !@klass.new(@part).throttle?
- end
-
-
- def test_self_property_registry
- assert_instance_of Hash, @klass.property_registry
- end
-
- def test_self_property_registry_with_klass
- assert_instance_of Hash, @klass.property_registry(Whois::Answer::Parser::WhoisNicIt)
- end
-
- def test_self_property_registry_with_klass_should_initialize_to_empty_hash
- parser = Class.new
- assert_equal Hash.new, @klass.property_registry(parser)
- end
-
-end
View
31 test/whois/answer/parser/whois.nic.ac_test.rb
@@ -123,35 +123,4 @@ def test_nameservers
assert_raise(Whois::PropertyNotSupported) { @klass.new(load_part('available.txt')).nameservers }
end
-
- def test_changed?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert !parser_r1.changed?(parser_r1)
- assert !parser_r1.changed?(parser_r2)
- assert parser_r1.changed?(parser_a1)
-
- assert !parser_a1.changed?(parser_a1)
- assert !parser_a1.changed?(parser_a2)
- assert parser_a1.changed?(parser_r1)
- end
-
- def test_unchanged?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert parser_r1.unchanged?(parser_r1)
- assert parser_r1.unchanged?(parser_r2)
- assert !parser_r1.unchanged?(parser_a1)
-
- assert parser_a1.unchanged?(parser_a1)
- assert parser_a1.unchanged?(parser_a2)
- assert !parser_a1.unchanged?(parser_r1)
- end
-
end
View
31 test/whois/answer/parser/whois.nic.io_test.rb
@@ -123,35 +123,4 @@ def test_nameservers
assert_raise(Whois::PropertyNotSupported) { @klass.new(load_part('available.txt')).nameservers }
end
-
- def test_changed?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert !parser_r1.changed?(parser_r1)
- assert !parser_r1.changed?(parser_r2)
- assert parser_r1.changed?(parser_a1)
-
- assert !parser_a1.changed?(parser_a1)
- assert !parser_a1.changed?(parser_a2)
- assert parser_a1.changed?(parser_r1)
- end
-
- def test_unchanged?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert parser_r1.unchanged?(parser_r1)
- assert parser_r1.unchanged?(parser_r2)
- assert !parser_r1.unchanged?(parser_a1)
-
- assert parser_a1.unchanged?(parser_a1)
- assert parser_a1.unchanged?(parser_a2)
- assert !parser_a1.unchanged?(parser_r1)
- end
-
end
View
21 test/whois/answer/parser/whois.nic.it_test.rb
@@ -274,25 +274,4 @@ def test_nameservers
assert_equal expected, parser.instance_eval { @nameservers }
end
-
- def test_changed_with_self
- parser1 = @klass.new(load_part('registered.txt'))
- assert !parser1.changed?(parser1)
- assert parser1.unchanged?(parser1)
- end
-
- def test_changed_with_equal_content
- parser1 = @klass.new(load_part('registered.txt'))
- parser2 = @klass.new(load_part('registered.txt'))
- assert !parser1.changed?(parser2)
- assert parser1.unchanged?(parser2)
- end
-
- def test_changed_with_different_content
- parser1 = @klass.new(load_part('registered.txt'))
- parser2 = @klass.new(load_part('available.txt'))
- assert parser1.changed?(parser2)
- assert !parser1.unchanged?(parser2)
- end
-
end
View
31 test/whois/answer/parser/whois.nic.sh_test.rb
@@ -123,35 +123,4 @@ def test_nameservers
assert_raise(Whois::PropertyNotSupported) { @klass.new(load_part('available.txt')).nameservers }
end
-
- def test_changed?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert !parser_r1.changed?(parser_r1)
- assert !parser_r1.changed?(parser_r2)
- assert parser_r1.changed?(parser_a1)
-
- assert !parser_a1.changed?(parser_a1)
- assert !parser_a1.changed?(parser_a2)
- assert parser_a1.changed?(parser_r1)
- end
-
- def test_unchanged?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert parser_r1.unchanged?(parser_r1)
- assert parser_r1.unchanged?(parser_r2)
- assert !parser_r1.unchanged?(parser_a1)
-
- assert parser_a1.unchanged?(parser_a1)
- assert parser_a1.unchanged?(parser_a2)
- assert !parser_a1.unchanged?(parser_r1)
- end
-
end
View
21 test/whois/answer/parser/whois.nic.sn_test.rb
@@ -166,25 +166,4 @@ def test_nameservers
assert_equal expected, parser.instance_eval { @nameservers }
end
-
- def test_changed_with_self
- parser1 = @klass.new(load_part('registered.txt'))
- assert !parser1.changed?(parser1)
- assert parser1.unchanged?(parser1)
- end
-
- def test_changed_with_equal_content
- parser1 = @klass.new(load_part('registered.txt'))
- parser2 = @klass.new(load_part('registered.txt'))
- assert !parser1.changed?(parser2)
- assert parser1.unchanged?(parser2)
- end
-
- def test_changed_with_different_content
- parser1 = @klass.new(load_part('registered.txt'))
- parser2 = @klass.new(load_part('available.txt'))
- assert parser1.changed?(parser2)
- assert !parser1.unchanged?(parser2)
- end
-
end
View
31 test/whois/answer/parser/whois.nic.tm_test.rb
@@ -123,35 +123,4 @@ def test_nameservers
assert_raise(Whois::PropertyNotSupported) { @klass.new(load_part('available.txt')).nameservers }
end
-
- def test_changed?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert !parser_r1.changed?(parser_r1)
- assert !parser_r1.changed?(parser_r2)
- assert parser_r1.changed?(parser_a1)
-
- assert !parser_a1.changed?(parser_a1)
- assert !parser_a1.changed?(parser_a2)
- assert parser_a1.changed?(parser_r1)
- end
-
- def test_unchanged?
- parser_r1 = @klass.new(load_part('registered.txt'))
- parser_r2 = @klass.new(load_part('registered.txt'))
- parser_a1 = @klass.new(load_part('available.txt'))
- parser_a2 = @klass.new(load_part('available.txt'))
-
- assert parser_r1.unchanged?(parser_r1)
- assert parser_r1.unchanged?(parser_r2)
- assert !parser_r1.unchanged?(parser_a1)
-
- assert parser_a1.unchanged?(parser_a1)
- assert parser_a1.unchanged?(parser_a2)
- assert !parser_a1.unchanged?(parser_r1)
- end
-
end
View
6 test/whois/answer_test.rb
@@ -95,12 +95,6 @@ def test_contacts
assert_equal [], answer.contacts
end
- def test_throttle_question
- answer = @klass.new(@server, @parts)
- answer.parser.expects(:throttle?).returns(true)
- assert_equal true, answer.throttle?
- end
-
def test_parser
answer = @klass.new(nil, [Whois::Answer::Part.new("", "whois.nic.it")])

0 comments on commit 7ba3de9

Please sign in to comment.
Something went wrong with that request. Please try again.