diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 986a7a9..c5a9fe8 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -592,6 +592,23 @@ module Oga return process(left, context) + process(right, context) end + ## + # Processes the `and` operator. + # + # This operator returns true if both the left and right expression + # evaluate to `true`. If the first expression evaluates to `false` the + # right expression is ignored. + # + # @param [Oga::XPath::Node] ast_node + # @param [Oga::XML::NodeSet] context + # @return [TrueClass|FalseClass] + # + def on_and(ast_node, context) + left, right = *ast_node + + return on_call_boolean(context, left) && on_call_boolean(context, right) + end + ## # Delegates function calls to specific handlers. # diff --git a/spec/oga/xpath/evaluator/operators/and_spec.rb b/spec/oga/xpath/evaluator/operators/and_spec.rb new file mode 100644 index 0000000..dede481 --- /dev/null +++ b/spec/oga/xpath/evaluator/operators/and_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'and operator' do + before do + @document = parse('11') + @evaluator = described_class.new(@document) + end + + example 'return true if both boolean literals are true' do + @evaluator.evaluate('true() and true()').should == true + end + + example 'return false if one of the boolean literals is false' do + @evaluator.evaluate('true() and false()').should == false + end + + example 'return true if both node sets are non empty' do + @evaluator.evaluate('root/a and root/b').should == true + end + + example 'false false if one of the node sets is empty' do + @evaluator.evaluate('root/a and root/c').should == false + end + + example 'skip the right expression if the left one evaluates to false' do + @evaluator.should_not receive(:on_call_true) + + @evaluator.evaluate('false() and true()').should == false + end + end +end