Class: MerbExt::SpecCustomMatchers::HaveTable

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-ext/spec_custom_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ HaveTable

Returns a new instance of HaveTable.



20
21
22
23
24
25
# File 'lib/merb-ext/spec_custom_matchers.rb', line 20

def initialize(table)
  @raw = table
  rows = table.split("\n").map {|row| row.split("|").map {|cell| cell.strip} }
  @headers = rows.first
  @rows = rows[1..-1]
end

Instance Method Details

#failure_messageObject



46
47
48
# File 'lib/merb-ext/spec_custom_matchers.rb', line 46

def failure_message
  "expected #{@target.body} to look like:\n#{@raw} \n #{@fields.inspect}"
end

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/merb-ext/spec_custom_matchers.rb', line 27

def matches?(target)
  @target = target
  @fields = []

  @headers.each_with_index do |header, index|
    matcher = ::Webrat::Matchers::HaveXpath.new("//table/tr[1]/th[#{index+1}][contains(. ,'#{header}')]")
    @fields << header unless matcher.matches?(target)
  end

  @rows.each_with_index do |row, row_index|
    row.each_with_index do |cell, cell_index|
      matcher = ::Webrat::Matchers::HaveXpath.new("//table/tr[#{row_index+2}]/td[#{cell_index+1}][contains(. ,'#{cell}')]")
      @fields << cell unless matcher.matches?(target)
    end
  end

  @fields.empty?
end