Skip to content

Commit b112e18

Browse files
committed
prefactor: restructure css-to-xpath tests
1 parent 3b28b49 commit b112e18

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

Diff for: test/css/test_xpath_visitor.rb

+39-31
Original file line numberDiff line numberDiff line change
@@ -143,53 +143,61 @@ def visit_pseudo_class_aaron(node)
143143
)
144144
end
145145

146-
it "# id" do
147-
assert_xpath("//*[@id='foo']", "#foo")
148-
assert_xpath("//*[@id='escape:needed,']", "#escape\\:needed\\,")
149-
assert_xpath("//*[@id='escape:needed,']", '#escape\3Aneeded\,')
150-
assert_xpath("//*[@id='escape:needed,']", '#escape\3A needed\2C')
151-
assert_xpath("//*[@id='escape:needed']", '#escape\00003Aneeded')
152-
end
153-
154-
describe "attribute" do
155-
it "basic mechanics" do
156-
assert_xpath("//h1[@a='Tender Lovemaking']", "h1[a='Tender Lovemaking']")
157-
assert_xpath("//h1[@a]", "h1[a]")
158-
assert_xpath(%q{//h1[@a='gnewline\n']}, "h1[a='\\gnew\\\nline\\\\n']")
159-
assert_xpath("//h1[@a='test']", %q{h1[a=\te\st]})
160-
end
161-
162-
it "parses leading @ (extended-syntax)" do
163-
assert_xpath("//a[@id='Boing']", "a[@id='Boing']")
164-
assert_xpath("//a[@id='Boing']", "a[@id = 'Boing']")
165-
assert_xpath("//a[@id='Boing']//div", "a[@id='Boing'] div")
146+
describe "namespaces" do
147+
let(:ns) do
148+
{
149+
"xmlns" => "http://default.example.com/",
150+
"hoge" => "http://hoge.example.com/",
151+
}
166152
end
167153

168-
it "namespacing" do
154+
it "basic mechanics" do
169155
assert_xpath("//a[@flavorjones:href]", "a[flavorjones|href]")
170156
assert_xpath("//a[@href]", "a[|href]")
171157
assert_xpath("//*[@flavorjones:href]", "*[flavorjones|href]")
158+
end
172159

173-
ns = {
174-
"xmlns" => "http://default.example.com/",
175-
"hoge" => "http://hoge.example.com/",
176-
}
177-
178-
# An intentionally-empty namespace means "don't use the default xmlns"
179-
assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false))
180-
181-
# The default namespace is not applied to attributes (just elements)
160+
it "default namespace is applied to elements but not attributes" do
182161
assert_equal(
183162
["//xmlns:a[@class='bar']"],
184163
Nokogiri::CSS.xpath_for("a[class='bar']", ns: ns, cache: false),
185164
)
165+
end
166+
167+
it "intentionally-empty namespace omits the default xmlns" do
168+
# An intentionally-empty namespace
169+
assert_equal(["//a"], Nokogiri::CSS.xpath_for("|a", ns: ns, cache: false))
170+
end
186171

187-
# We can explicitly apply a namespace to an attribue
172+
it "explicit namespaces are applied to attributes" do
188173
assert_equal(
189174
["//xmlns:a[@hoge:class='bar']"],
190175
Nokogiri::CSS.xpath_for("a[hoge|class='bar']", ns: ns, cache: false),
191176
)
192177
end
178+
end
179+
180+
describe "attribute" do
181+
it "basic mechanics" do
182+
assert_xpath("//h1[@a='Tender Lovemaking']", "h1[a='Tender Lovemaking']")
183+
assert_xpath("//h1[@a]", "h1[a]")
184+
assert_xpath(%q{//h1[@a='gnewline\n']}, "h1[a='\\gnew\\\nline\\\\n']")
185+
assert_xpath("//h1[@a='test']", %q{h1[a=\te\st]})
186+
end
187+
188+
it "#id escaping" do
189+
assert_xpath("//*[@id='foo']", "#foo")
190+
assert_xpath("//*[@id='escape:needed,']", "#escape\\:needed\\,")
191+
assert_xpath("//*[@id='escape:needed,']", '#escape\3Aneeded\,')
192+
assert_xpath("//*[@id='escape:needed,']", '#escape\3A needed\2C')
193+
assert_xpath("//*[@id='escape:needed']", '#escape\00003Aneeded')
194+
end
195+
196+
it "parses leading @ (extended-syntax)" do
197+
assert_xpath("//a[@id='Boing']", "a[@id='Boing']")
198+
assert_xpath("//a[@id='Boing']", "a[@id = 'Boing']")
199+
assert_xpath("//a[@id='Boing']//div", "a[@id='Boing'] div")
200+
end
193201

194202
it "rhs with quotes" do
195203
assert_xpath(%q{//h1[@a="'"]}, %q{h1[a="'"]})

0 commit comments

Comments
 (0)