mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[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!!!
b48f3afa3b
This commit is contained in:
parent
643344dc94
commit
39f275edf7
2 changed files with 35 additions and 35 deletions
|
@ -66,7 +66,6 @@ module REXML
|
||||||
def Functions::id( object )
|
def Functions::id( object )
|
||||||
end
|
end
|
||||||
|
|
||||||
# UNTESTED
|
|
||||||
def Functions::local_name(node_set=nil)
|
def Functions::local_name(node_set=nil)
|
||||||
get_namespace(node_set) do |node|
|
get_namespace(node_set) do |node|
|
||||||
return node.local_name
|
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
|
# 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
|
# number in a way that is dependent on that type
|
||||||
def Functions::number( object=nil )
|
def Functions::number(object=@@context[:node])
|
||||||
object = @@context[:node] unless object
|
|
||||||
case object
|
case object
|
||||||
when true
|
when true
|
||||||
Float(1)
|
Float(1)
|
||||||
when false
|
when false
|
||||||
Float(0)
|
Float(0)
|
||||||
when Array
|
when Array
|
||||||
number(string( object ))
|
number(string(object))
|
||||||
when Numeric
|
when Numeric
|
||||||
object.to_f
|
object.to_f
|
||||||
else
|
else
|
||||||
str = string( object )
|
str = string(object)
|
||||||
# If XPath ever gets scientific notation...
|
case str.strip
|
||||||
#if str =~ /^\s*-?(\d*\.?\d+|\d+\.)([Ee]\d*)?\s*$/
|
when /\A\s*(-?(?:\d+(?:\.\d*)?|\.\d+))\s*\z/
|
||||||
if str =~ /^\s*-?(\d*\.?\d+|\d+\.)\s*$/
|
$1.to_f
|
||||||
str.to_f
|
|
||||||
else
|
else
|
||||||
(0.0 / 0.0)
|
Float::NAN
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,35 +1,38 @@
|
||||||
# frozen_string_literal: false
|
# frozen_string_literal: false
|
||||||
require 'rexml/document'
|
|
||||||
require 'test/unit'
|
require "test/unit"
|
||||||
require 'rexml/functions'
|
require "rexml/document"
|
||||||
|
require "rexml/functions"
|
||||||
|
|
||||||
module REXMLTests
|
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
|
def test_true
|
||||||
telem = REXML::Element.new("elem")
|
assert_equal(1, REXML::Functions.number(true))
|
||||||
telem.text="9"
|
|
||||||
assert_equal(9, REXML::Functions::number(telem))
|
|
||||||
end
|
end
|
||||||
def test_functions_number_float
|
|
||||||
telem = REXML::Element.new("elem")
|
def test_false
|
||||||
telem.text="10.4"
|
assert_equal(0, REXML::Functions.number(false))
|
||||||
assert_equal(10.4, REXML::Functions::number(telem))
|
|
||||||
end
|
end
|
||||||
def test_functions_number_negative_int
|
|
||||||
telem = REXML::Element.new("elem")
|
def test_numeric
|
||||||
telem.text="-9"
|
assert_equal(29, REXML::Functions.number(29))
|
||||||
assert_equal(-9, REXML::Functions::number(telem))
|
|
||||||
end
|
end
|
||||||
def test_functions_number_negative_float
|
|
||||||
telem = REXML::Element.new("elem")
|
def test_string_integer
|
||||||
telem.text="-9.13"
|
assert_equal(100, REXML::Functions.number("100"))
|
||||||
assert_equal(-9.13, REXML::Functions::number(telem))
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue