From 4ef79bad9016ca4987b98f98e630f39669bf2d09 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 27 Aug 2014 09:35:02 +0200 Subject: [PATCH] Support for the XPath not() function. --- lib/oga/xpath/evaluator.rb | 19 ++++++++++++++++ spec/oga/xpath/evaluator/calls/not_spec.rb | 26 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 spec/oga/xpath/evaluator/calls/not_spec.rb diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index b694c91..8fa6a84 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -1045,6 +1045,10 @@ module Oga # # The boolean `false` is returned for all other cases. # + # @param [Oga::XML::NodeSet] context + # @param [Oga::XPath::Node] expression + # @return [TrueClass|FalseClass] + # def on_call_boolean(context, expression) retval = process(expression, context) @@ -1055,6 +1059,21 @@ module Oga end end + ## + # Processes the `not()` function call. + # + # This function converts the 1st argument to a boolean and returns the + # opposite boolean value. For example, if the first argument results in + # `true` then this function returns `false` instead. + # + # @param [Oga::XML::NodeSet] context + # @param [Oga::XPath::Node] expression + # @return [TrueClass|FalseClass] + # + def on_call_not(context, expression) + return !on_call_boolean(context, expression) + end + ## # Processes an `(int)` node. # diff --git a/spec/oga/xpath/evaluator/calls/not_spec.rb b/spec/oga/xpath/evaluator/calls/not_spec.rb new file mode 100644 index 0000000..2edda52 --- /dev/null +++ b/spec/oga/xpath/evaluator/calls/not_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'not() function' do + before do + @document = parse('foo') + @evaluator = described_class.new(@document) + end + + example 'return false when the argument is a non-zero integer' do + @evaluator.evaluate('not(10)').should == false + end + + example 'return true when the argument is a zero integer' do + @evaluator.evaluate('not(0)').should == true + end + + example 'return false when the argument is a non-empty node set' do + @evaluator.evaluate('not(root)').should == false + end + + example 'return itrue when the argument is an empty node set' do + @evaluator.evaluate('not(foo)').should == true + end + end +end