Support for the XPath "or" operator.

This commit is contained in:
Yorick Peterse 2014-08-28 21:01:12 +02:00
parent 4f189d9218
commit 4fa40b58cf
2 changed files with 57 additions and 0 deletions

View File

@ -609,6 +609,23 @@ module Oga
return on_call_boolean(context, left) && on_call_boolean(context, right)
end
##
# Processes the `or` operator.
#
# This operator returns `true` if one of the expressions evaluates to
# true, otherwise false is returned. If the first expression evaluates to
# `true` the second expression is ignored.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [TrueClass|FalseClass]
#
def on_or(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.
#

View File

@ -0,0 +1,40 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'or operator' do
before do
@document = parse('<root><a>1</a><b>1</b></root>')
@evaluator = described_class.new(@document)
end
example 'return true if both boolean literals are true' do
@evaluator.evaluate('true() or true()').should == true
end
example 'return true if one of the boolean literals is false' do
@evaluator.evaluate('true() or false()').should == true
end
example 'return true if the left node set is not empty' do
@evaluator.evaluate('root/a or root/x').should == true
end
example 'return true if the right node set is not empty' do
@evaluator.evaluate('root/x or root/b').should == true
end
example 'return true if both node sets are not empty' do
@evaluator.evaluate('root/a or root/b').should == true
end
example 'return false if both node sets are empty' do
@evaluator.evaluate('root/x or root/y').should == false
end
example 'skip the right expression if the left one evaluates to false' do
@evaluator.should_not receive(:on_call_false)
@evaluator.evaluate('true() or false()').should == true
end
end
end