Skip to content

Commit

Permalink
test: add ad-hoc tests for CSS :has pseudoselector
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Apr 22, 2019
1 parent 68e50c5 commit c88c9b7
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions test/css/test_css_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,43 @@ def setup
</html>
EOF
@parser = Nokogiri.HTML doc

@nested = Nokogiri.HTML(<<~EOF)
<html><body>
<div class='unnested direct'>
<b>bold</b>
<p>para</p>
</div>
<div class='unnested indirect'>
<b>bold</b>
<i>...</i>
<i>...</i>
<p>para</p>
</div>
<div class='nested-parent'>
<div class='nested-child direct'>
<b>bold</b>
<p>para</p>
</div>
<div class='nested-child indirect'>
<b>bold</b>
<i>...</i>
<i>...</i>
<p>para</p>
</div>
</div>
<div class="has-bold">
<b>bold</b>
</div>
<div class="has-para">
<p>para</p>
</div>
EOF
end

def test_even
Expand Down Expand Up @@ -216,6 +253,83 @@ def test_siblings
assert_equal 0, parser.search("#5 + p").size
end

def test_has_a
result = @nested.css("div:has(b)")
expected = [
@nested.at_css(".unnested.direct"),
@nested.at_css(".unnested.indirect"),
@nested.at_css(".nested-parent"),
@nested.at_css(".nested-child.direct"),
@nested.at_css(".nested-child.indirect"),
@nested.at_css(".has-bold"),
]
assert_equal expected, result.to_a
end

def test_has_a_gt_b
result = @nested.css("body *:has(div > b)")
expected = [
@nested.at_css(".nested-parent"),
]
assert_equal expected, result.to_a
end

def test_has_gt_b
result = @nested.css("body *:has(> b)")
expected = [
@nested.at_css(".unnested.direct"),
@nested.at_css(".unnested.indirect"),
@nested.at_css(".nested-child.direct"),
@nested.at_css(".nested-child.indirect"),
@nested.at_css(".has-bold"),
]
assert_equal expected, result.to_a
end

def test_has_a_plus_b
result = @nested.css("div:has(b + p)")
expected = [
@nested.at_css(".unnested.direct"),
@nested.at_css(".nested-parent"),
@nested.at_css(".nested-child.direct"),
]
assert_equal expected, result.to_a
end

def test_has_plus_b
result = @nested.css("b:has(+ p)")
expected = [
@nested.at_css(".unnested.direct b"),
@nested.at_css(".nested-child.direct b"),
]
assert_equal expected, result.to_a
end

def test_has_a_tilde_b
result = @nested.css("div:has(b ~ p)")
expected = [
@nested.at_css(".unnested.direct"),
@nested.at_css(".unnested.indirect"),
@nested.at_css(".nested-parent"),
@nested.at_css(".nested-child.direct"),
@nested.at_css(".nested-child.indirect"),
]
assert_equal expected, result.to_a
end

def test_has_tilde_b
result = @nested.css("b:has(~ p)")
expected = [
@nested.at_css(".unnested.direct b"),
@nested.at_css(".unnested.indirect b"),
@nested.at_css(".nested-child.direct b"),
@nested.at_css(".nested-child.indirect b"),
].flatten
assert_equal expected, result.to_a
end

private

def assert_result_rows(intarray, result, word = "row")
assert_equal intarray.size, result.size,
"unexpected number of rows returned: '#{result.inner_text}'"
Expand Down

0 comments on commit c88c9b7

Please sign in to comment.