Class: SwissKnife::RSpec::Matchers::HaveTag
- Inherits:
-
Object
- Object
- SwissKnife::RSpec::Matchers::HaveTag
- Defined in:
- lib/swiss_knife/rspec/have_tag.rb
Instance Attribute Summary collapse
-
#actual ⇒ Object
readonly
Returns the value of attribute actual.
-
#actual_count ⇒ Object
readonly
Returns the value of attribute actual_count.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #description ⇒ Object
- #doc_for(input) ⇒ Object
- #failure_message ⇒ Object
-
#initialize(type, selector, options = {}, &block) ⇒ HaveTag
constructor
A new instance of HaveTag.
- #matches?(actual, &block) ⇒ Boolean
- #negative_failure_message ⇒ Object
Constructor Details
#initialize(type, selector, options = {}, &block) ⇒ HaveTag
Returns a new instance of HaveTag.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 15 def initialize(type, selector, = {}, &block) @selector = selector.to_s @type = type case when Hash @options = when Numeric @options = {:count => } else @options = {:text => } end end |
Instance Attribute Details
#actual ⇒ Object (readonly)
Returns the value of attribute actual.
13 14 15 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13 def actual @actual end |
#actual_count ⇒ Object (readonly)
Returns the value of attribute actual_count.
13 14 15 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13 def actual_count @actual_count end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
13 14 15 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13 def doc @doc end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13 def @options end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
13 14 15 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13 def selector @selector end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13 def type @type end |
Instance Method Details
#description ⇒ Object
58 59 60 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 58 def description "have tag #{selector.inspect} with #{.inspect}" end |
#doc_for(input) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 29 def doc_for(input) engine = type == :xml ? Nokogiri::XML : Nokogiri::HTML if input.respond_to?(:body) engine.parse(input.body.to_s) elsif Nokogiri::XML::Element === input input else engine.parse(input.to_s) end end |
#failure_message ⇒ Object
62 63 64 65 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 62 def explanation = actual_count ? "but found #{actual_count}" : "but did not" "expected\n#{doc.to_s}\nto have #{failure_count_phrase} #{failure_selector_phrase}, #{explanation}" end |
#matches?(actual, &block) ⇒ Boolean
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 41 def matches?(actual, &block) @actual = actual @doc = doc_for(actual) matches = doc.css(selector) return [:count] == 0 if matches.empty? matches = filter_on_inner_text(matches) if [:text] matches = filter_on_nested_expectations(matches, block) if block @actual_count = matches.size return false if not acceptable_count?(actual_count) !matches.empty? end |
#negative_failure_message ⇒ Object
67 68 69 70 |
# File 'lib/swiss_knife/rspec/have_tag.rb', line 67 def explanation = actual_count ? "but found #{actual_count}" : "but did" "expected\n#{doc.to_s}\nnot to have #{failure_count_phrase} #{failure_selector_phrase}, #{explanation}" end |