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 8f01e8f
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions test/css/test_css_integration.rb
Expand Up @@ -67,6 +67,34 @@ def setup
</html>
EOF
@parser = Nokogiri.HTML doc

@nested = Nokogiri.HTML(<<~EOF)
<html><body>
<div class='unnested'>
<b>bold1 </b>
<i>italic1 </i>
<b class="a">bold2 </b>
<em class="a">emphasis1 </em>
<i>italic2 </i>
<p>para1 </p>
</div>
<div class='nested nested-parent'>
<div class='nested nested-child'>
<b>bold1 </b>
<i>italic1 </i>
<b class="a">bold2 </b>
<em class="a">emphasis1 </em>
<i>italic2 </i>
</div>
</div>
<div class="nerp">
<i>nerrrrp</i>
<a>nerp</a>
<b>also nerp</b>
</div>
EOF
end

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

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

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

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

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

def test_has_plus_b
result = @nested.css("i:has(+ b)")
expected = [
@nested.css(".unnested i")[0],
@nested.css(".nested-parent i")[0],
]
assert_equal expected, result.to_a

result = @nested.css("a:has(+ b)")
expected = [
@nested.at_css(".nerp a"),
]
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"),
]
assert_equal expected, result.to_a

result = @nested.css("div:has(b ~ i)")
expected = [
@nested.at_css(".unnested"),
@nested.at_css(".nested-parent"),
@nested.at_css(".nested-child"),
]
assert_equal expected, result.to_a

result = @nested.css("div:has(i ~ b)")
expected = [
@nested.at_css(".unnested"),
@nested.at_css(".nested-parent"),
@nested.at_css(".nested-child"),
@nested.at_css(".nerp"),
]
assert_equal expected, result.to_a
end

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

result = @nested.css("i:has(~ i)")
expected = [
@nested.css(".unnested i")[0],
@nested.css(".nested-child i")[0],
]
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 8f01e8f

Please sign in to comment.