Class: Spec::Rails::Matchers::TableMatcher
- Inherits:
-
Object
- Object
- Spec::Rails::Matchers::TableMatcher
- Defined in:
- lib/matchers/table_matcher.rb
Instance Method Summary collapse
- #extract_html_content(html) ⇒ Object
- #failure_message ⇒ Object
-
#initialize(table_selector_or_expected, expected) ⇒ TableMatcher
constructor
A new instance of TableMatcher.
- #matches?(response) ⇒ Boolean
- #negative_failure_message ⇒ Object
Constructor Details
#initialize(table_selector_or_expected, expected) ⇒ TableMatcher
Returns a new instance of TableMatcher.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/matchers/table_matcher.rb', line 6 def initialize table_selector_or_expected, expected case table_selector_or_expected when String @table_selector = table_selector_or_expected @expected = expected when Array @expected = table_selector_or_expected end raise 'Invalid "expected" argument' if @expected.nil? end |
Instance Method Details
#extract_html_content(html) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/matchers/table_matcher.rb', line 30 def extract_html_content html doc = Hpricot.XML(html) rows = doc.search("table#{"#{@table_selector}" if @table_selector} tr") header_elements = rows.reject{|e| e.search('th').empty? } header_content = header_elements.map do |header_element| header_element.search('/th').map do |n| stripped = n.inner_html.gsub(/<script.*<\/script>/m, '') temp_node = Hpricot.XML("<temp-node>#{stripped}</temp-node>") temp_node.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n") end end body_elements = rows.reject{|e| e.search('td').empty? } body_content = body_elements.map do |body_element| body_element.search('/td').map do |n| stripped = n.inner_html.gsub(/<script.*<\/script>/m, '') temp_node = Hpricot.XML("<temp-node>#{stripped}</temp-node>") temp_node.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n") end end header_content + body_content end |
#failure_message ⇒ Object
22 23 24 |
# File 'lib/matchers/table_matcher.rb', line 22 def "\nWrong table contents.\nexpected: #{@expected.inspect.gsub('], [', "],\n[")}\n found: #{@actual.inspect.gsub('], [', "],\n[")}\n\n" end |
#matches?(response) ⇒ Boolean
17 18 19 20 |
# File 'lib/matchers/table_matcher.rb', line 17 def matches? response @actual = extract_html_content response.body @actual == @expected end |
#negative_failure_message ⇒ Object
26 27 28 |
# File 'lib/matchers/table_matcher.rb', line 26 def "\nTable should not have matched: #{@expected.inspect}\n" end |