-
-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert most of "reuse XPathContext objects" (969bea9)
Revert the pieces of 969bea9 related to re-use of xpath context objects. Leave in these pieces of that commit: - ability to de-register variables and namespaces - XPathContext#node= and backfill explicit test coverage for these features.
- Loading branch information
1 parent
6a899a3
commit 47e89a3
Showing
5 changed files
with
84 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
|
||
module Nokogiri | ||
module XML | ||
describe XPathContext do | ||
it "can register and deregister namespaces" do | ||
doc = Document.parse(<<~XML) | ||
<root xmlns="http://nokogiri.org/default" xmlns:ns1="http://nokogiri.org/ns1"> | ||
<child>default</child> | ||
<ns1:child>ns1</ns1:child> | ||
</root> | ||
XML | ||
|
||
xc = XPathContext.new(doc) | ||
|
||
assert_raises(XPath::SyntaxError) do | ||
xc.evaluate("//xmlns:child") | ||
end | ||
|
||
xc.register_namespaces({ "xmlns" => "http://nokogiri.org/default" }) | ||
assert_pattern do | ||
xc.evaluate("//xmlns:child") => [ | ||
{ name: "child", namespace: { href: "http://nokogiri.org/default" } } | ||
] | ||
end | ||
|
||
xc.register_namespaces({ "xmlns" => nil }) | ||
assert_raises(XPath::SyntaxError) do | ||
xc.evaluate("//xmlns:child") | ||
end | ||
end | ||
|
||
it "can register and deregister variables" do | ||
doc = Nokogiri::XML.parse(File.read(TestBase::XML_FILE), TestBase::XML_FILE) | ||
|
||
xc = XPathContext.new(doc) | ||
|
||
assert_raises(XPath::SyntaxError) do | ||
xc.evaluate("//address[@domestic=$value]") | ||
end | ||
|
||
xc.register_variables({ "value" => "Yes" }) | ||
nodes = xc.evaluate("//address[@domestic=$value]") | ||
assert_equal(4, nodes.length) | ||
|
||
xc.register_variables({ "value" => "Qwerty" }) | ||
nodes = xc.evaluate("//address[@domestic=$value]") | ||
assert_empty(nodes) | ||
|
||
xc.register_variables({ "value" => nil }) | ||
assert_raises(XPath::SyntaxError) do | ||
xc.evaluate("//address[@domestic=$value]") | ||
end | ||
end | ||
|
||
it "#node=" do | ||
doc = Nokogiri::XML::Document.parse(<<~XML) | ||
<root> | ||
<child><foo>one</foo></child> | ||
<child><foo>two</foo></child> | ||
<child><foo>three</foo></child> | ||
</root> | ||
XML | ||
|
||
xc = XPathContext.new(doc) | ||
results = xc.evaluate(".//foo") | ||
assert_equal(3, results.length) | ||
|
||
xc.node = doc.root.elements[0] | ||
assert_pattern { xc.evaluate(".//foo") => [{ name: "foo", inner_html: "one" }] } | ||
|
||
xc.node = doc.root.elements[1] | ||
assert_pattern { xc.evaluate(".//foo") => [{ name: "foo", inner_html: "two" }] } | ||
end | ||
end | ||
end | ||
end |