aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKouhei Sutou <kou@clear-code.com>2019-05-25 17:47:41 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2019-08-04 11:55:37 +0900
commit5f78b138b10a6732676689f0f8690c1db16c1355 (patch)
tree0df730f6064bb54da62021d0c66dbe3b98afd771
parent6ef82943978ea5816a91c32e9ff822c73d1935f9 (diff)
downloadruby-5f78b138b10a6732676689f0f8690c1db16c1355.tar.gz
[ruby/rexml] xpath boolean: implement
https://github.com/ruby/rexml/commit/feb8ddb1ec
-rw-r--r--lib/rexml/functions.rb27
-rw-r--r--test/rexml/test_functions_boolean.rb75
2 files changed, 91 insertions, 11 deletions
diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb
index 9c226d2cdf..4b46b8e678 100644
--- a/lib/rexml/functions.rb
+++ b/lib/rexml/functions.rb
@@ -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
- else
- return object.size > 0
- end
- elsif object.kind_of? Array
- object = object.find{|x| x and true}
+ 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
+ object ? true : false
end
- return object ? true : false
end
# UNTESTED
diff --git a/test/rexml/test_functions_boolean.rb b/test/rexml/test_functions_boolean.rb
new file mode 100644
index 0000000000..b3e2117c10
--- /dev/null
+++ b/test/rexml/test_functions_boolean.rb
@@ -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