summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKouhei Sutou <kou@clear-code.com>2019-05-25 18:25:37 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2019-08-04 11:55:49 +0900
commit39f275edf7284ef0c0f9b9391038ae9f2c019731 (patch)
treee50ea0a7d6ef28453b79ddfd728570be7aa4b07d
parent643344dc9460626617c9ce88f07b3ae0fed49150 (diff)
[ruby/rexml] xpath number: fix a bug that false is converted to NaN
GitHub: fix #18 It must be 0. Reported by Mirko Budszuhn. Thanks!!! https://github.com/ruby/rexml/commit/b48f3afa3b
-rw-r--r--lib/rexml/functions.rb17
-rw-r--r--test/rexml/functions/test_number.rb53
2 files changed, 35 insertions, 35 deletions
diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb
index a0f4823adaf..77926bf2af4 100644
--- a/lib/rexml/functions.rb
+++ b/lib/rexml/functions.rb
@@ -66,7 +66,6 @@ module REXML
def Functions::id( object )
end
- # UNTESTED
def Functions::local_name(node_set=nil)
get_namespace(node_set) do |node|
return node.local_name
@@ -386,25 +385,23 @@ module REXML
#
# an object of a type other than the four basic types is converted to a
# number in a way that is dependent on that type
- def Functions::number( object=nil )
- object = @@context[:node] unless object
+ def Functions::number(object=@@context[:node])
case object
when true
Float(1)
when false
Float(0)
when Array
- number(string( object ))
+ number(string(object))
when Numeric
object.to_f
else
- str = string( object )
- # If XPath ever gets scientific notation...
- #if str =~ /^\s*-?(\d*\.?\d+|\d+\.)([Ee]\d*)?\s*$/
- if str =~ /^\s*-?(\d*\.?\d+|\d+\.)\s*$/
- str.to_f
+ str = string(object)
+ case str.strip
+ when /\A\s*(-?(?:\d+(?:\.\d*)?|\.\d+))\s*\z/
+ $1.to_f
else
- (0.0 / 0.0)
+ Float::NAN
end
end
end
diff --git a/test/rexml/functions/test_number.rb b/test/rexml/functions/test_number.rb
index 84ec5c7ba79..16e635701c7 100644
--- a/test/rexml/functions/test_number.rb
+++ b/test/rexml/functions/test_number.rb
@@ -1,35 +1,38 @@
# frozen_string_literal: false
-require 'rexml/document'
-require 'test/unit'
-require 'rexml/functions'
+
+require "test/unit"
+require "rexml/document"
+require "rexml/functions"
module REXMLTests
- class TC_Rexml_Functions_Number < Test::Unit::TestCase
+ class TestFunctionsNumber < Test::Unit::TestCase
+ def setup
+ REXML::Functions.context = nil
+ end
- def test_functions_number_int
- telem = REXML::Element.new("elem")
- telem.text="9"
- assert_equal(9, REXML::Functions::number(telem))
+ def test_true
+ assert_equal(1, REXML::Functions.number(true))
end
- def test_functions_number_float
- telem = REXML::Element.new("elem")
- telem.text="10.4"
- assert_equal(10.4, REXML::Functions::number(telem))
+
+ def test_false
+ assert_equal(0, REXML::Functions.number(false))
end
- def test_functions_number_negative_int
- telem = REXML::Element.new("elem")
- telem.text="-9"
- assert_equal(-9, REXML::Functions::number(telem))
+
+ def test_numeric
+ assert_equal(29, REXML::Functions.number(29))
end
- def test_functions_number_negative_float
- telem = REXML::Element.new("elem")
- telem.text="-9.13"
- assert_equal(-9.13, REXML::Functions::number(telem))
+
+ def test_string_integer
+ assert_equal(100, REXML::Functions.number("100"))
+ end
+
+ def test_string_float
+ assert_equal(-9.13, REXML::Functions.number("-9.13"))
+ end
+
+ def test_node_set
+ root = REXML::Document.new("<root>100</root>").root
+ assert_equal(100, REXML::Functions.number([root]))
end
- #def test_functions_number_scientific_notation
- # telem = REXML::Element.new("elem")
- # telem.text="9.13E12"
- # assert_equal(9.13E12, REXML::Functions::number(telem))
- #end
end
end