mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/rexml] xpath boolean: implement
https://github.com/ruby/rexml/commit/feb8ddb1ec
This commit is contained in:
parent
6ef8294397
commit
5f78b138b1
2 changed files with 91 additions and 11 deletions
|
@ -315,18 +315,23 @@ module REXML
|
|||
end
|
||||
end
|
||||
|
||||
# UNTESTED
|
||||
def Functions::boolean( object=nil )
|
||||
if object.kind_of? String
|
||||
if object =~ /\d+/u
|
||||
return object.to_f != 0
|
||||
def Functions::boolean(object=@@context[:node])
|
||||
case object
|
||||
when true, false
|
||||
object
|
||||
when Float
|
||||
return false if object.zero?
|
||||
return false if object.nan?
|
||||
true
|
||||
when Numeric
|
||||
not object.zero?
|
||||
when String
|
||||
not object.empty?
|
||||
when Array
|
||||
not object.empty?
|
||||
else
|
||||
return object.size > 0
|
||||
object ? true : false
|
||||
end
|
||||
elsif object.kind_of? Array
|
||||
object = object.find{|x| x and true}
|
||||
end
|
||||
return object ? true : false
|
||||
end
|
||||
|
||||
# UNTESTED
|
||||
|
|
75
test/rexml/test_functions_boolean.rb
Normal file
75
test/rexml/test_functions_boolean.rb
Normal file
|
@ -0,0 +1,75 @@
|
|||
# frozen_string_literal: false
|
||||
|
||||
require "test/unit"
|
||||
require "rexml/document"
|
||||
require "rexml/functions"
|
||||
|
||||
module REXMLTests
|
||||
class TestFunctionsBoolean < Test::Unit::TestCase
|
||||
def setup
|
||||
REXML::Functions.context = nil
|
||||
end
|
||||
|
||||
def test_true
|
||||
assert_equal(true, REXML::Functions.boolean(true))
|
||||
end
|
||||
|
||||
def test_false
|
||||
assert_equal(false, REXML::Functions.boolean(false))
|
||||
end
|
||||
|
||||
def test_integer_true
|
||||
assert_equal(true, REXML::Functions.boolean(1))
|
||||
end
|
||||
|
||||
def test_integer_positive_zero
|
||||
assert_equal(false, REXML::Functions.boolean(0))
|
||||
end
|
||||
|
||||
def test_integer_negative_zero
|
||||
assert_equal(false, REXML::Functions.boolean(-0))
|
||||
end
|
||||
|
||||
def test_float_true
|
||||
assert_equal(true, REXML::Functions.boolean(1.1))
|
||||
end
|
||||
|
||||
def test_float_positive_zero
|
||||
assert_equal(false, REXML::Functions.boolean(-0.0))
|
||||
end
|
||||
|
||||
def test_float_negative_zero
|
||||
assert_equal(false, REXML::Functions.boolean(-0.0))
|
||||
end
|
||||
|
||||
def test_float_nan
|
||||
assert_equal(false, REXML::Functions.boolean(Float::NAN))
|
||||
end
|
||||
|
||||
def test_string_true
|
||||
assert_equal(true, REXML::Functions.boolean("content"))
|
||||
end
|
||||
|
||||
def test_string_empty
|
||||
assert_equal(false, REXML::Functions.boolean(""))
|
||||
end
|
||||
|
||||
def test_node_set_true
|
||||
root = REXML::Document.new("<root/>").root
|
||||
assert_equal(true, REXML::Functions.boolean([root]))
|
||||
end
|
||||
|
||||
def test_node_set_empty
|
||||
assert_equal(false, REXML::Functions.boolean([]))
|
||||
end
|
||||
|
||||
def test_nil
|
||||
assert_equal(false, REXML::Functions.boolean(nil))
|
||||
end
|
||||
|
||||
def test_context
|
||||
REXML::Functions.context = {node: true}
|
||||
assert_equal(true, REXML::Functions.boolean())
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue