Class: Spec::Rails::Matchers::TableHeaderMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/matchers/table_header_matcher.rb

Instance Method Summary collapse

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_messageObject



21
22
23
# File 'lib/matchers/table_header_matcher.rb', line 21

def failure_message
  "\nWrong table header contents.\nexpected: #{@expected.inspect}\n   found: #{@actual.inspect}\n\n"
end

#matches?(response) ⇒ Boolean

Returns:

  • (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_messageObject



25
26
27
# File 'lib/matchers/table_header_matcher.rb', line 25

def negative_failure_message
  "\nTable header should not have matched: #{@expected.inspect}\n"
end