From b112e18a4851eca1d40dcc7e755653dc32ea885d Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 19 Jan 2025 11:49:23 -0500 Subject: [PATCH] prefactor: restructure css-to-xpath tests --- test/css/test_xpath_visitor.rb | 70 +++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/test/css/test_xpath_visitor.rb b/test/css/test_xpath_visitor.rb index 7d87f523bc6..a51e14c2a2e 100644 --- a/test/css/test_xpath_visitor.rb +++ b/test/css/test_xpath_visitor.rb @@ -143,53 +143,61 @@ def visit_pseudo_class_aaron(node) ) end - it "# id" do - assert_xpath("//*[@id='foo']", "#foo") - assert_xpath("//*[@id='escape:needed,']", "#escape\\:needed\\,") - assert_xpath("//*[@id='escape:needed,']", '#escape\3Aneeded\,') - assert_xpath("//*[@id='escape:needed,']", '#escape\3A needed\2C') - assert_xpath("//*[@id='escape:needed']", '#escape\00003Aneeded') - end - - describe "attribute" do - it "basic mechanics" do - assert_xpath("//h1[@a='Tender Lovemaking']", "h1[a='Tender Lovemaking']") - assert_xpath("//h1[@a]", "h1[a]") - assert_xpath(%q{//h1[@a='gnewline\n']}, "h1[a='\\gnew\\\nline\\\\n']") - assert_xpath("//h1[@a='test']", %q{h1[a=\te\st]}) - end - - it "parses leading @ (extended-syntax)" do - assert_xpath("//a[@id='Boing']", "a[@id='Boing']") - assert_xpath("//a[@id='Boing']", "a[@id = 'Boing']") - assert_xpath("//a[@id='Boing']//div", "a[@id='Boing'] div") + describe "namespaces" do + let(:ns) do + { + "xmlns" => "http://default.example.com/", + "hoge" => "http://hoge.example.com/", + } end - it "namespacing" do + it "basic mechanics" do assert_xpath("//a[@flavorjones:href]", "a[flavorjones|href]") assert_xpath("//a[@href]", "a[|href]") assert_xpath("//*[@flavorjones:href]", "*[flavorjones|href]") + end - ns = { - "xmlns" => "http://default.example.com/", - "hoge" => "http://hoge.example.com/", - } - - # An intentionally-empty namespace means "don't use the default xmlns" - assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false)) - - # The default namespace is not applied to attributes (just elements) + it "default namespace is applied to elements but not attributes" do assert_equal( ["//xmlns:a[@class='bar']"], Nokogiri::CSS.xpath_for("a[class='bar']", ns: ns, cache: false), ) + end + + it "intentionally-empty namespace omits the default xmlns" do + # An intentionally-empty namespace + assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false)) + end - # We can explicitly apply a namespace to an attribue + it "explicit namespaces are applied to attributes" do assert_equal( ["//xmlns:a[@hoge:class='bar']"], Nokogiri::CSS.xpath_for("a[hoge|class='bar']", ns: ns, cache: false), ) end + end + + describe "attribute" do + it "basic mechanics" do + assert_xpath("//h1[@a='Tender Lovemaking']", "h1[a='Tender Lovemaking']") + assert_xpath("//h1[@a]", "h1[a]") + assert_xpath(%q{//h1[@a='gnewline\n']}, "h1[a='\\gnew\\\nline\\\\n']") + assert_xpath("//h1[@a='test']", %q{h1[a=\te\st]}) + end + + it "#id escaping" do + assert_xpath("//*[@id='foo']", "#foo") + assert_xpath("//*[@id='escape:needed,']", "#escape\\:needed\\,") + assert_xpath("//*[@id='escape:needed,']", '#escape\3Aneeded\,') + assert_xpath("//*[@id='escape:needed,']", '#escape\3A needed\2C') + assert_xpath("//*[@id='escape:needed']", '#escape\00003Aneeded') + end + + it "parses leading @ (extended-syntax)" do + assert_xpath("//a[@id='Boing']", "a[@id='Boing']") + assert_xpath("//a[@id='Boing']", "a[@id = 'Boing']") + assert_xpath("//a[@id='Boing']//div", "a[@id='Boing'] div") + end it "rhs with quotes" do assert_xpath(%q{//h1[@a="'"]}, %q{h1[a="'"]})