Raise error when invalid keys given

This commit is contained in:
Jonas Nicklas 2012-07-13 15:36:43 +02:00
parent 6d68242d1a
commit 21fad63bc2
2 changed files with 21 additions and 0 deletions

View File

@ -2,8 +2,11 @@ module Capybara
class Query
attr_accessor :selector, :locator, :options, :xpath, :find, :negative
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum]
def initialize(*args)
@options = if args.last.is_a?(Hash) then args.pop.dup else {} end
unless options.has_key?(:visible)
@options[:visible] = Capybara.ignore_hidden_elements
end
@ -18,6 +21,8 @@ module Capybara
@selector ||= Selector.all[Capybara.default_selector]
@xpath = @selector.call(@locator).to_s
assert_valid_keys!
end
def name; selector.name; end
@ -57,5 +62,17 @@ module Capybara
count > 0
end
end
private
def assert_valid_keys!
valid_keys = VALID_KEYS + @selector.custom_filters.keys
invalid_keys = @options.keys - valid_keys
unless invalid_keys.empty?
invalid_names = invalid_keys.map(&:inspect).join(", ")
valid_names = valid_keys.map(&:inspect).join(", ")
raise ArgumentError, "invalid keys #{invalid_names}, should be one of #{valid_names}"
end
end
end
end

View File

@ -21,6 +21,10 @@ shared_examples_for "all" do
@result.should include('Smith', 'John', 'John Smith')
end
it "should raise an error when given invalid options" do
expect { @session.all('//p', :schmoo => "foo") }.to raise_error(ArgumentError)
end
context "with css selectors" do
it "should find all elements using the given selector" do
@session.all(:css, 'h1').first.text.should == 'This is a test'