Skip to content

Commit

Permalink
Raise error when invalid keys given
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Jul 13, 2012
1 parent 6d68242 commit 21fad63
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/capybara/query.rb
Expand Up @@ -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
Expand All @@ -18,6 +21,8 @@ def initialize(*args)
@selector ||= Selector.all[Capybara.default_selector]

@xpath = @selector.call(@locator).to_s

assert_valid_keys!
end

def name; selector.name; end
Expand Down Expand Up @@ -57,5 +62,17 @@ def matches_count?(count)
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
4 changes: 4 additions & 0 deletions lib/capybara/spec/session/all_spec.rb
Expand Up @@ -21,6 +21,10 @@
@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'
Expand Down

0 comments on commit 21fad63

Please sign in to comment.