Permalink
Browse files

Added inverse method, to create negative expressions

  • Loading branch information...
1 parent 11c6784 commit 0c7342d4e9248c790c2e049d73915920e3993805 @jnicklas jnicklas committed Aug 6, 2010
Showing with 19 additions and 0 deletions.
  1. +10 −0 lib/xpath/expression.rb
  2. +9 −0 spec/xpath_spec.rb
View
@@ -139,6 +139,11 @@ def to_xpath
end
end
+ class Inverse < Unary
+ def to_xpath
+ "not(#{@expression.to_xpath})"
+ end
+ end
class Applied < Expression
def initialize(expression, variables={})
@@ -183,6 +188,11 @@ def and(expression)
end
alias_method :&, :and
+ def inverse
+ Expression::Inverse.new(current)
+ end
+ alias_method :~, :inverse
+
def string_literal
Expression::StringLiteral.new(self.to_xpath)
end
View
@@ -98,6 +98,15 @@ def xpath(&block)
xpath { |x| x.descendant(:div)[:"@id = 'foo'"] }.first[:title].should == "fooDiv"
end
end
+
+ describe '#inverse' do
+ it "should invert the expression" do
+ xpath { |x| x.descendant(:p).where(x.attr(:id).equals('fooDiv').inverse) }.first.text.should == 'Bax'
+ end
+
+ it "should be aliased as the unary tilde" do
+ xpath { |x| x.descendant(:p).where(~x.attr(:id).equals('fooDiv')) }.first.text.should == 'Bax'
+ end
end
describe '#equals' do

0 comments on commit 0c7342d

Please sign in to comment.