Class: Spec::Rails::Matchers::TableHeaderMatcher
- Inherits:
-
Object
- Object
- Spec::Rails::Matchers::TableHeaderMatcher
- Defined in:
- lib/matchers/table_header_matcher.rb
Instance Method Summary collapse
- #extract_html_content(html) ⇒ Object
- #failure_message ⇒ Object
-
#initialize(table_id_or_expected, expected) ⇒ TableHeaderMatcher
constructor
A new instance of TableHeaderMatcher.
- #matches?(response) ⇒ Boolean
- #negative_failure_message ⇒ Object
Constructor Details
#initialize(table_id_or_expected, expected) ⇒ TableHeaderMatcher
Returns a new instance of TableHeaderMatcher.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/matchers/table_header_matcher.rb', line 5 def initialize table_id_or_expected, expected case table_id_or_expected when String @table_id = table_id_or_expected @expected = expected when Array @expected = table_id_or_expected end raise 'Invalid "expected" argument' if @expected.nil? end |
Instance Method Details
#extract_html_content(html) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/matchers/table_header_matcher.rb', line 29 def extract_html_content html html = html.gsub(/[ \t]*<br *\/>[ \t]*/, "\n") doc = Hpricot.XML(html) elements = doc.search("table#{"##{@table_id}" if @table_id} tr") elements = elements.reject{|e| e.search('th').empty? } elements.map do |node| node.search('/th').map do |n| n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n") end end end |
#failure_message ⇒ Object
21 22 23 |
# File 'lib/matchers/table_header_matcher.rb', line 21 def "\nWrong table header contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n" end |
#matches?(response) ⇒ Boolean
16 17 18 19 |
# File 'lib/matchers/table_header_matcher.rb', line 16 def matches? response @actual = extract_html_content response.body @actual == @expected end |
#negative_failure_message ⇒ Object
25 26 27 |
# File 'lib/matchers/table_header_matcher.rb', line 25 def "\nTable header should not have matched: #{@expected.inspect}\n" end |