diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index bb690b2..48f265a 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -770,6 +770,22 @@ module Oga return on_call_number(context, left) < on_call_number(context, right) end + ## + # Processes the `>` operator. + # + # This operator converts the left and right expression to a number and + # returns `true` if the first number is greater than the second number. + # + # @param [Oga::XML::Node] ast_node + # @param [Oga::XML::NodeSet] context + # @return [TrueClass|FalseClass] + # + def on_gt(ast_node, context) + left, right = *ast_node + + return on_call_number(context, left) > on_call_number(context, right) + end + ## # Delegates function calls to specific handlers. # diff --git a/spec/oga/xpath/evaluator/operators/gt_spec.rb b/spec/oga/xpath/evaluator/operators/gt_spec.rb new file mode 100644 index 0000000..3605eba --- /dev/null +++ b/spec/oga/xpath/evaluator/operators/gt_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'greater-than operator' do + before do + @document = parse('1020') + @evaluator = described_class.new(@document) + end + + example 'return true if a number is greater than another number' do + @evaluator.evaluate('20 > 10').should == true + end + + example 'return false if a number is not greater than another number' do + @evaluator.evaluate('10 > 20').should == false + end + + example 'return true if a number is greater than a string' do + @evaluator.evaluate('20 > "10"').should == true + end + + example 'return true if a string is greater than a number' do + @evaluator.evaluate('"20" > 10').should == true + end + + example 'return true if a string is greater than another string' do + @evaluator.evaluate('"20" > "10"').should == true + end + + example 'return true if a number is greater than a node set' do + @evaluator.evaluate('20 > root/a').should == true + end + + example 'return true if a string is greater than a node set' do + @evaluator.evaluate('"20" > root/a').should == true + end + + example 'return true if a node set is greater than another node set' do + @evaluator.evaluate('root/b > root/a').should == true + end + end +end