From 14af5b83c21324e19f62f67b8c7518dc49fccb24 Mon Sep 17 00:00:00 2001 From: h-arai Date: Wed, 7 Feb 2024 13:26:09 +0900 Subject: [PATCH 1/4] fix REXML::Functions::normalize_space() bug #110 Replaced the variable 'string' with 'x' inside the collect block. --- lib/rexml/functions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb index 77926bf2..8fa1e750 100644 --- a/lib/rexml/functions.rb +++ b/lib/rexml/functions.rb @@ -266,7 +266,7 @@ def Functions::string_length( string ) def Functions::normalize_space( string=nil ) string = string(@@context[:node]) if string.nil? if string.kind_of? Array - string.collect{|x| string.to_s.strip.gsub(/\s+/um, ' ') if string} + string.collect{|x| x.to_s.strip.gsub(/\s+/um, ' ') if x} else string.to_s.strip.gsub(/\s+/um, ' ') end From 85f2d4c39f7da9a77c2b18f792699c23a0c3dfab Mon Sep 17 00:00:00 2001 From: h-arai Date: Wed, 7 Feb 2024 15:04:52 +0900 Subject: [PATCH 2/4] introduced test_normalize_space2() in test/functions/test_base.rb #110 to validate `REXML::Functions::normalize_space()` functionality --- test/functions/test_base.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/functions/test_base.rb b/test/functions/test_base.rb index 74dc1a31..ea0edc4a 100644 --- a/test/functions/test_base.rb +++ b/test/functions/test_base.rb @@ -229,6 +229,25 @@ def test_normalize_space assert_equal( [REXML::Comment.new("COMMENT A")], m ) end + def test_normalize_space2 + source1 = "breakfast boosts concentrationCoffee beans aromaDessert after dinner" + source2 = <<-XML +breakfast boosts\t\t + +concentration +Coffee beans + aroma + + + + Dessert + \t\t after dinner + XML + ret1 = REXML::XPath.each(REXML::Document.new(source1), "//text()").to_a + ret2 = REXML::XPath.each(REXML::Document.new(source2), "normalize-space(//text())").to_a + assert_equal( ret1, ret2 ) + end + def test_string_nil_without_context doc = REXML::Document.new(<<-XML) From a5508e694d79d9e76409db40cd9c2ead452df0ae Mon Sep 17 00:00:00 2001 From: h-arai Date: Wed, 7 Feb 2024 15:11:23 +0900 Subject: [PATCH 3/4] delete "# UNTESTED" comment #110 --- lib/rexml/functions.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb index 8fa1e750..4c114616 100644 --- a/lib/rexml/functions.rb +++ b/lib/rexml/functions.rb @@ -262,7 +262,6 @@ def Functions::string_length( string ) string(string).length end - # UNTESTED def Functions::normalize_space( string=nil ) string = string(@@context[:node]) if string.nil? if string.kind_of? Array From c87b0e392d476ae2d5a05980051424841cef0cf0 Mon Sep 17 00:00:00 2001 From: flatisland Date: Thu, 8 Feb 2024 13:41:30 +0900 Subject: [PATCH 4/4] Apply suggestions from code review Renamed test_normalize_space2 for better clarity and updated tests to use Array of String for expected data. #110 Co-authored-by: Sutou Kouhei --- test/functions/test_base.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/functions/test_base.rb b/test/functions/test_base.rb index ea0edc4a..9ba3ed24 100644 --- a/test/functions/test_base.rb +++ b/test/functions/test_base.rb @@ -229,9 +229,8 @@ def test_normalize_space assert_equal( [REXML::Comment.new("COMMENT A")], m ) end - def test_normalize_space2 - source1 = "breakfast boosts concentrationCoffee beans aromaDessert after dinner" - source2 = <<-XML + def test_normalize_space_strings + source = <<-XML breakfast boosts\t\t concentration @@ -243,9 +242,13 @@ def test_normalize_space2 Dessert \t\t after dinner XML - ret1 = REXML::XPath.each(REXML::Document.new(source1), "//text()").to_a - ret2 = REXML::XPath.each(REXML::Document.new(source2), "normalize-space(//text())").to_a - assert_equal( ret1, ret2 ) + normalized_texts = REXML::XPath.each(REXML::Document.new(source), "normalize-space(//text())").to_a + assert_equal([ + "breakfast boosts concentration", + "Coffee beans aroma", + "Dessert after dinner", + ], + normalized_texts) end def test_string_nil_without_context