From e1d9e62b72f301ea2083a25bf32309fdfee34894 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 1 Sep 2014 20:48:11 +0200 Subject: [PATCH] Support for the XPath "!=" operator. --- lib/oga/xpath/evaluator.rb | 12 +++++ .../oga/xpath/evaluator/operators/neq_spec.rb | 50 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 spec/oga/xpath/evaluator/operators/neq_spec.rb diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 7b43cf7..3416564 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -742,6 +742,18 @@ module Oga return left == right end + ## + # Processes the `!=` operator. + # + # This operator does the exact opposite of the `=` operator. See {#on_eq} + # for more information. + # + # @see [#on_eq] + # + def on_neq(ast_node, context) + return !on_eq(ast_node, context) + end + ## # Delegates function calls to specific handlers. # diff --git a/spec/oga/xpath/evaluator/operators/neq_spec.rb b/spec/oga/xpath/evaluator/operators/neq_spec.rb new file mode 100644 index 0000000..9e2d55b --- /dev/null +++ b/spec/oga/xpath/evaluator/operators/neq_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'not-equal operator' do + before do + @document = parse('1010') + @evaluator = described_class.new(@document) + end + + example 'return true if two numbers are not equal' do + @evaluator.evaluate('10 != 20').should == true + end + + example 'return false if two numbers are equal' do + @evaluator.evaluate('10 != 10').should == false + end + + example 'return true if a number and a string are not equal' do + @evaluator.evaluate('10 != "20"').should == true + end + + example 'return true if two strings are not equal' do + @evaluator.evaluate('"10" != "20"').should == true + end + + example 'return true if a string and a number are not equal' do + @evaluator.evaluate('"10" != 20').should == true + end + + example 'return false if two strings are equal' do + @evaluator.evaluate('"a" != "a"').should == false + end + + example 'return true if two node sets are not equal' do + @evaluator.evaluate('root != root/b').should == true + end + + example 'return false if two node sets are equal' do + @evaluator.evaluate('root/a != root/b').should == false + end + + example 'return true if a node set and a number are not equal' do + @evaluator.evaluate('root/a != 20').should == true + end + + example 'return true if a node set and a string are not equal' do + @evaluator.evaluate('root/a != "20"').should == true + end + end +end