teamcapybara--capybara/lib/capybara/result.rb

58 lines
1.4 KiB
Ruby
Raw Normal View History

module Capybara
class Result
include Enumerable
def initialize(elements, query)
@unfiltered_elements = elements
@filtered_elements = elements.select { |node| query.matches_filters?(node) }
@query = query
end
def each(&block)
@filtered_elements.each(&block)
end
def first
@filtered_elements.first
end
def matches_count?
@query.matches_count?(@filtered_elements.size)
end
def find!
2012-07-09 11:21:44 +00:00
raise find_error if @filtered_elements.count != 1
@filtered_elements.first
end
2012-07-09 11:33:45 +00:00
def size; @filtered_elements.size; end
alias_method :length, :size
alias_method :count, :size
2012-07-09 11:21:44 +00:00
def find_error
if @filtered_elements.count == 0
Capybara::ElementNotFound.new("Unable to find #{@query.description}")
elsif @filtered_elements.count > 1
2012-07-09 11:33:45 +00:00
Capybara::Ambiguous.new("Ambiguous match, found #{size} elements matching #{@query.description}")
2012-07-09 11:21:44 +00:00
end
end
def failure_message
if @query.options[:count]
2012-07-09 11:33:45 +00:00
"expected #{@query.description} to be returned #{@query.options[:count]} times, was found #{size} times"
else
2012-07-09 11:33:45 +00:00
"expected to find #{@query.description} but there were no matches"
end
end
def negative_failure_message
2012-07-09 11:33:45 +00:00
"expected not to find #{@query.description}, but there were #{size} matches"
end
def empty?
@filtered_elements.empty?
end
2012-06-11 15:24:50 +00:00
def [](key); @filtered_elements[key]; end
end
end