From 444c9ce7449d3c5a75ae50087555ec73ae1963a8 Mon Sep 17 00:00:00 2001 From: flatisland Date: Thu, 8 Feb 2024 14:59:30 +0900 Subject: [PATCH] xpath: Fix normalize_space(array) case (#111) GitHub: fix GH-110 Fixed a bug in `REXML::Functions.normalize_space(array)` and introduced test cases for it: - Corrected a typo in the variable name within the collect block (`string` -> `x`). - Added `test_normalize_space_strings` to `test/functions/test_base.rb`. --------- Co-authored-by: Sutou Kouhei --- lib/rexml/functions.rb | 3 +-- test/functions/test_base.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb index 77926bf2..4c114616 100644 --- a/lib/rexml/functions.rb +++ b/lib/rexml/functions.rb @@ -262,11 +262,10 @@ 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 - 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 diff --git a/test/functions/test_base.rb b/test/functions/test_base.rb index 74dc1a31..9ba3ed24 100644 --- a/test/functions/test_base.rb +++ b/test/functions/test_base.rb @@ -229,6 +229,28 @@ def test_normalize_space assert_equal( [REXML::Comment.new("COMMENT A")], m ) end + def test_normalize_space_strings + source = <<-XML +breakfast boosts\t\t + +concentration +Coffee beans + aroma + + + + Dessert + \t\t after dinner + XML + 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 doc = REXML::Document.new(<<-XML)