From d707bd3dab4019f16fc366ca3c6d5b55e5a9bcf4 Mon Sep 17 00:00:00 2001 From: "Peter H. Boling" Date: Tue, 8 Jul 2025 21:32:11 +0000 Subject: [PATCH 01/11] =?UTF-8?q?=F0=9F=90=9B=20Handle=20nil=20when=20pars?= =?UTF-8?q?ing=20fragment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/rexml/xpath_parser.rb | 7 ++++- test/parser/test_xpath.rb | 2 +- test/test_xpath_parser.rb | 66 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 test/test_xpath_parser.rb diff --git a/lib/rexml/xpath_parser.rb b/lib/rexml/xpath_parser.rb index 70ae8919..dbe5fa9d 100644 --- a/lib/rexml/xpath_parser.rb +++ b/lib/rexml/xpath_parser.rb @@ -84,7 +84,12 @@ def parse path, node node = node.first end - node.document.__send__(:enable_cache) do + document = node.document + if document + document.__send__(:enable_cache) do + match( path_stack, node ) + end + else match( path_stack, node ) end end diff --git a/test/parser/test_xpath.rb b/test/parser/test_xpath.rb index 9143d25c..5d62afee 100644 --- a/test/parser/test_xpath.rb +++ b/test/parser/test_xpath.rb @@ -4,7 +4,7 @@ require "rexml/parsers/xpathparser" module REXMLTests - class TestXPathParser < Test::Unit::TestCase + class TestParserXPathParser < Test::Unit::TestCase sub_test_case("#abbreviate") do def abbreviate(xpath) parser = REXML::Parsers::XPathParser.new diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb new file mode 100644 index 00000000..04c8fefd --- /dev/null +++ b/test/test_xpath_parser.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: false +# +# Created by Henrik MÃ¥rtensson on 2007-02-18. +# Copyright (c) 2007. All rights reserved. + +module REXMLTests + class TestXPathParser < Test::Unit::TestCase + def setup + @root_element = _make_service_element(["urn:type1", "urn:type2"], ["http://uri"]) + @root = @root_element.root + @element = @root_element.children[0] + end + + def _make_service_element(types, uris) + root_element = REXML::Element.new + element = root_element.add_element("Service") + types.each do |type_text| + element.add_element("Type").text = type_text + end + uris.each do |uri_text| + element.add_element("URI").text = uri_text + end + root_element + end + + def test_is_first_child + assert_kind_of(REXML::Element, @element) + end + + def test_has_element_as_parent + assert_kind_of(REXML::Element, @element.parent) + end + + def test_has_element_as_root + assert_kind_of(REXML::Element, @root) + end + + def test_parent_is_root + assert_equal(@root, @element.parent) + end + + def test_has_nil_siblings + assert_nil(@root.previous_sibling) + assert_nil(@element.next_sibling) + end + + def test_has_not_nil_siblings + assert_kind_of(REXML::Element, @element.children[0].next_sibling) + assert_kind_of(REXML::Element, @element.children[1].previous_sibling) + end + + def test_parsing_when_found + @parser = REXML::XPathParser.new + res = @parser.parse("/Service", @root_element) + assert_equal("[ ... ]", + res.to_s) + end + + def test_parsing_when_not_found + @parser = REXML::XPathParser.new + res = @parser.parse("/Stardust", @root_element) + assert_equal("[]", + res.to_s) + end + end +end From b3ad31dc45933f714fc0bbf7f65640045025a30d Mon Sep 17 00:00:00 2001 From: "|7eter l-|. l3oling" Date: Wed, 9 Jul 2025 07:47:16 +0700 Subject: [PATCH 02/11] Update test/test_xpath_parser.rb Co-authored-by: Sutou Kouhei --- test/test_xpath_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index 04c8fefd..1cb890c2 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -11,7 +11,7 @@ def setup @element = @root_element.children[0] end - def _make_service_element(types, uris) + def make_service_element(types, uris) root_element = REXML::Element.new element = root_element.add_element("Service") types.each do |type_text| From 48624f48434be1b55f77ea9071ceb37489f6553b Mon Sep 17 00:00:00 2001 From: "|7eter l-|. l3oling" Date: Wed, 9 Jul 2025 07:48:38 +0700 Subject: [PATCH 03/11] Update test/test_xpath_parser.rb Co-authored-by: Sutou Kouhei --- test/test_xpath_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index 1cb890c2..a1a571ed 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -58,7 +58,7 @@ def test_parsing_when_found def test_parsing_when_not_found @parser = REXML::XPathParser.new - res = @parser.parse("/Stardust", @root_element) + res = @parser.parse("/nonexistent", @root_element) assert_equal("[]", res.to_s) end From 7159d3c145f6b6e15c1481d74492fc9b41308f38 Mon Sep 17 00:00:00 2001 From: "|7eter l-|. l3oling" Date: Wed, 9 Jul 2025 07:49:09 +0700 Subject: [PATCH 04/11] Update test/test_xpath_parser.rb Co-authored-by: Sutou Kouhei --- test/test_xpath_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index a1a571ed..a466d867 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -49,7 +49,7 @@ def test_has_not_nil_siblings assert_kind_of(REXML::Element, @element.children[1].previous_sibling) end - def test_parsing_when_found + def test_found @parser = REXML::XPathParser.new res = @parser.parse("/Service", @root_element) assert_equal("[ ... ]", From d664bab0953a3b8221c2dae8a5956f29c4dd8e30 Mon Sep 17 00:00:00 2001 From: "|7eter l-|. l3oling" Date: Wed, 9 Jul 2025 07:50:04 +0700 Subject: [PATCH 05/11] Update test/test_xpath_parser.rb --- test/test_xpath_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index a466d867..3d40d725 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -56,7 +56,7 @@ def test_found res.to_s) end - def test_parsing_when_not_found + def test_not_found @parser = REXML::XPathParser.new res = @parser.parse("/nonexistent", @root_element) assert_equal("[]", From 7b9950d59abe57155522c956545e6d2ae1d85f94 Mon Sep 17 00:00:00 2001 From: "Peter H. Boling" Date: Wed, 9 Jul 2025 00:52:11 +0000 Subject: [PATCH 06/11] =?UTF-8?q?=F0=9F=93=84=20Remove=20accidental=20copy?= =?UTF-8?q?right?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_xpath_parser.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index 3d40d725..4eb6110f 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -1,7 +1,4 @@ # frozen_string_literal: false -# -# Created by Henrik MÃ¥rtensson on 2007-02-18. -# Copyright (c) 2007. All rights reserved. module REXMLTests class TestXPathParser < Test::Unit::TestCase From 4b7e969c4a7bcbf62f8d1d2066e2315e025ecca1 Mon Sep 17 00:00:00 2001 From: "Peter H. Boling" Date: Wed, 9 Jul 2025 00:53:23 +0000 Subject: [PATCH 07/11] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20method=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_xpath_parser.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index 4eb6110f..291c6c1d 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -3,7 +3,7 @@ module REXMLTests class TestXPathParser < Test::Unit::TestCase def setup - @root_element = _make_service_element(["urn:type1", "urn:type2"], ["http://uri"]) + @root_element = make_service_element(["urn:type1", "urn:type2"], ["http://uri"]) @root = @root_element.root @element = @root_element.children[0] end @@ -56,8 +56,8 @@ def test_found def test_not_found @parser = REXML::XPathParser.new res = @parser.parse("/nonexistent", @root_element) - assert_equal("[]", - res.to_s) + assert_equal([], + res) end end end From db6834629e90f013dd1bf86e3e4a6202ef576049 Mon Sep 17 00:00:00 2001 From: "Peter H. Boling" Date: Wed, 9 Jul 2025 00:55:15 +0000 Subject: [PATCH 08/11] =?UTF-8?q?=E2=9C=85=20Improve=20tests=20with=20obje?= =?UTF-8?q?ct=20comparisons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_xpath_parser.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index 291c6c1d..94ca6c88 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -49,8 +49,8 @@ def test_has_not_nil_siblings def test_found @parser = REXML::XPathParser.new res = @parser.parse("/Service", @root_element) - assert_equal("[ ... ]", - res.to_s) + assert_equal([@element], + res) end def test_not_found From 0be1869bb16fc55642b723428b9b026c4f00b3f9 Mon Sep 17 00:00:00 2001 From: "Peter H. Boling" Date: Wed, 9 Jul 2025 00:57:46 +0000 Subject: [PATCH 09/11] =?UTF-8?q?=F0=9F=94=A5=20Remove=20redundant=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_xpath_parser.rb | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index 94ca6c88..f7aac912 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -4,8 +4,8 @@ module REXMLTests class TestXPathParser < Test::Unit::TestCase def setup @root_element = make_service_element(["urn:type1", "urn:type2"], ["http://uri"]) - @root = @root_element.root @element = @root_element.children[0] + @parser = REXML::XPathParser.new end def make_service_element(types, uris) @@ -20,34 +20,7 @@ def make_service_element(types, uris) root_element end - def test_is_first_child - assert_kind_of(REXML::Element, @element) - end - - def test_has_element_as_parent - assert_kind_of(REXML::Element, @element.parent) - end - - def test_has_element_as_root - assert_kind_of(REXML::Element, @root) - end - - def test_parent_is_root - assert_equal(@root, @element.parent) - end - - def test_has_nil_siblings - assert_nil(@root.previous_sibling) - assert_nil(@element.next_sibling) - end - - def test_has_not_nil_siblings - assert_kind_of(REXML::Element, @element.children[0].next_sibling) - assert_kind_of(REXML::Element, @element.children[1].previous_sibling) - end - def test_found - @parser = REXML::XPathParser.new res = @parser.parse("/Service", @root_element) assert_equal([@element], res) From b4211dba5bbf6b4974fe51c379cf9d0bc999ff05 Mon Sep 17 00:00:00 2001 From: "Peter H. Boling" Date: Wed, 9 Jul 2025 00:59:13 +0000 Subject: [PATCH 10/11] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Frozen=20String=20Li?= =?UTF-8?q?teral?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_xpath_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index f7aac912..61b41023 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true module REXMLTests class TestXPathParser < Test::Unit::TestCase From 8060e7aa4eb7b0c482c25796a73cc830f900ce27 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Wed, 9 Jul 2025 10:12:51 +0900 Subject: [PATCH 11/11] Remove a needless code --- test/test_xpath_parser.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_xpath_parser.rb b/test/test_xpath_parser.rb index 61b41023..bcb14c34 100644 --- a/test/test_xpath_parser.rb +++ b/test/test_xpath_parser.rb @@ -27,7 +27,6 @@ def test_found end def test_not_found - @parser = REXML::XPathParser.new res = @parser.parse("/nonexistent", @root_element) assert_equal([], res)