From 809ed9bfa60d35b2573a0500feabd516a35a3e14 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 28 Aug 2014 09:36:21 +0200 Subject: [PATCH] Handle boolean values in the boolean() function. --- lib/oga/xpath/evaluator.rb | 9 ++++++--- spec/oga/xpath/evaluator/calls/boolean_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 3c251a9..986a7a9 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -1075,12 +1075,15 @@ module Oga # def on_call_boolean(context, expression) retval = process(expression, context) + bool = false if retval.is_a?(Numeric) - return !retval.nan? && !retval.zero? - else - return retval && !retval.empty? + bool = !retval.nan? && !retval.zero? + elsif retval + bool = !retval.respond_to?(:empty?) || !retval.empty? end + + return bool end ## diff --git a/spec/oga/xpath/evaluator/calls/boolean_spec.rb b/spec/oga/xpath/evaluator/calls/boolean_spec.rb index e0b3328..a3d99b2 100644 --- a/spec/oga/xpath/evaluator/calls/boolean_spec.rb +++ b/spec/oga/xpath/evaluator/calls/boolean_spec.rb @@ -19,6 +19,14 @@ describe Oga::XPath::Evaluator do @evaluator.evaluate('boolean(10)').should == true end + example 'return true for a boolean true' do + @evaluator.evaluate('boolean(true())').should == true + end + + example 'return false for a boolean false' do + @evaluator.evaluate('boolean(false())').should == false + end + example 'return true for a positive float' do @evaluator.evaluate('boolean(10.5)').should == true end