Permalink
Browse files

Metaprogrammed matchers

  • Loading branch information...
jnicklas committed Jul 29, 2010
1 parent fcc4531 commit cf7a7fcedf0db54a7eb8e979d9de4ae0755a1024
Showing with 38 additions and 45 deletions.
  1. +38 −45 lib/capybara/node/matchers.rb
@@ -37,60 +37,53 @@ def has_no_css?(path, options={})
has_no_xpath?(XPath.from_css(path), options)
end
- def has_content?(content)
- has_xpath?(XPath.content(content))
- end
-
- def has_no_content?(content)
- has_no_xpath?(XPath.content(content))
- end
-
- def has_link?(locator)
- has_xpath?(XPath.link(locator))
- end
-
- def has_no_link?(locator)
- has_no_xpath?(XPath.link(locator))
- end
-
- def has_button?(locator)
- has_xpath?(XPath.button(locator))
- end
-
- def has_no_button?(locator)
- has_no_xpath?(XPath.button(locator))
- end
-
- def has_field?(locator, options={})
- has_xpath?(XPath.field(locator, options))
- end
+ class Matcher
+ def expression(&block)
+ @expression = block if block
+ @expression
+ end
- def has_no_field?(locator, options={})
- has_no_xpath?(XPath.field(locator, options))
- end
+ def failure_message(&block)
+ @failure_message = block if block
+ @failure_message
+ end
- def has_checked_field?(locator)
- has_xpath?(XPath.field(locator, :checked => true))
+ def negative_failure_message(&block)
+ @negative_failure_message = block if block
+ @negative_failure_message
+ end
end
- def has_unchecked_field?(locator)
- has_xpath?(XPath.field(locator, :unchecked => true))
+ def self.matcher(name, &block)
+ get_matcher = lambda do |*args|
+ matcher = Matcher.new
+ matcher.instance_exec(*args, &block) if block
+ matcher.expression { XPath.send(name, *args) } unless matcher.expression
+ matcher
+ end
+ define_method(:"has_#{name}?") do |*args|
+ has_xpath?(get_matcher[*args].expression.call)
+ end
+ define_method(:"has_no_#{name}?") do |*args|
+ has_no_xpath?(get_matcher[*args].expression.call)
+ end
end
- def has_select?(locator, options={})
- has_xpath?(XPath.select(locator, options))
+ matcher :content
+ matcher :link
+ matcher :button
+ matcher :field
+ matcher :table do |locator, options|
+ expression { XPath.table(locator, options || {}) }
end
-
- def has_no_select?(locator, options={})
- has_no_xpath?(XPath.select(locator, options))
+ matcher :select do |locator, options|
+ expression { XPath.select(locator, options || {}) }
end
-
- def has_table?(locator, options={})
- has_xpath?(XPath.table(locator, options))
+ matcher :unchecked_field do |locator|
+ expression { XPath.field(locator, :unchecked => true) }
end
-
- def has_no_table?(locator, options={})
- has_no_xpath?(XPath.table(locator, options))
+ matcher :checked_field do |locator|
+ expression { XPath.field(locator, :checked => true) }
end
end
end

0 comments on commit cf7a7fc

Please sign in to comment.