Support for the XPath "mod" operator.
This commit is contained in:
		
							parent
							
								
									78c8cd1323
								
							
						
					
					
						commit
						034b360d13
					
				| 
						 | 
				
			
			@ -658,6 +658,22 @@ module Oga
 | 
			
		|||
        return on_call_number(context, left) / on_call_number(context, right)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      ##
 | 
			
		||||
      # Processes the `mod` operator.
 | 
			
		||||
      #
 | 
			
		||||
      # This operator converts the left and right expressions to numbers and
 | 
			
		||||
      # returns the modulo of the two numbers.
 | 
			
		||||
      #
 | 
			
		||||
      # @param [Oga::XPath::Node] ast_node
 | 
			
		||||
      # @param [Oga::XML::NodeSet] context
 | 
			
		||||
      # @return [Float]
 | 
			
		||||
      #
 | 
			
		||||
      def on_mod(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.
 | 
			
		||||
      #
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,34 @@
 | 
			
		|||
require 'spec_helper'
 | 
			
		||||
 | 
			
		||||
describe Oga::XPath::Evaluator do
 | 
			
		||||
  context 'mod operator' do
 | 
			
		||||
    before do
 | 
			
		||||
      @document  = parse('<root><a>2</a><b>3</b></root>')
 | 
			
		||||
      @evaluator = described_class.new(@document)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    example 'return the modulo of two numbers' do
 | 
			
		||||
      @evaluator.evaluate('2 mod 3').should == 2.0
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    example 'return the modulo of a number and a string' do
 | 
			
		||||
      @evaluator.evaluate('2 mod "3"').should == 2.0
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    example 'return the modulo of two strings' do
 | 
			
		||||
      @evaluator.evaluate('"2" mod "3"').should == 2.0
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    example 'return the modulo of a node set and a number' do
 | 
			
		||||
      @evaluator.evaluate('root/a mod 3').should == 2.0
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    example 'return the modulo of two node sets' do
 | 
			
		||||
      @evaluator.evaluate('root/a mod root/b').should == 2.0
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    example 'return NaN when trying to get the modulo of invalid values' do
 | 
			
		||||
      @evaluator.evaluate('"" mod 1').should be_nan
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
		Reference in New Issue