Skip to content

assert_dom strict parameter #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ GEM

PLATFORMS
aarch64-linux
x86_64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(values, previous_selection = nil, refute: false, &root_fallback)
@values = values
@root = extract_root(previous_selection, root_fallback)
extract_selectors
@strict = false
@tests = extract_equality_tests(refute)
@message = @values.shift

Expand Down Expand Up @@ -59,6 +60,7 @@ def filter(matches)

content.strip! unless NO_STRIP.include?(match.name)
content.delete_prefix!("\n") if text_matches && match.name == "textarea"
collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || @strict

next if regex_matching ? (content =~ match_with) : (content == match_with)
content_mismatch ||= diff(match_with, content)
Expand Down Expand Up @@ -136,8 +138,14 @@ def extract_equality_tests(refute)
raise ArgumentError, "Range begin or :minimum cannot be greater than Range end or :maximum"
end

@strict = comparisons[:strict]

comparisons
end

def collapse_html_whitespace!(text)
text.gsub!(/\s+/, " ")
end
end
end
end
Expand Down
60 changes: 60 additions & 0 deletions test/selector_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,66 @@ def test_assert_select_with_invalid_minimum_and_maximum
assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message
end

def test_assert_select_not_strict_collapses_whitespace
render_html "<p>Some\n line-broken\n text</p>"

assert_nothing_raised do
assert_select "p", {
text: "Some line-broken text",
strict: false
}, "Whitespace was not collapsed from text when not strict"

assert_select "p", {
html: "Some line-broken text",
strict: false
}, "Whitespace was not collapsed from html when not strict"
end

render_html "<p>Some<br><br>line-broken<br><br>text</p>"

assert_nothing_raised do
assert_select "p", {
text: "Someline-brokentext",
strict: false
}, "<br> was not removed from text when not strict"

assert_select "p", {
html: "Some<br><br>line-broken<br><br>text",
strict: false
}, "<br> was removed from html when not strict"
end
end

def test_assert_select_strict_respects_whitespace
render_html "<p>Some\n line-broken\n text</p>"

assert_nothing_raised do
assert_select "p", {
text: "Some\n line-broken\n text",
strict: true
}, "Whitespace was collapsed from text when strict"

assert_select "p", {
html: "Some\n line-broken\n text",
strict: true
}, "Whitespace was collapsed from html when strict"
end

render_html "<p>Some<br><br>line-broken<br><br>text</p>"

assert_nothing_raised do
assert_select "p", {
text: "Someline-brokentext",
strict: true
}, "<br> was not removed from text when strict"

assert_select "p", {
html: "Some<br><br>line-broken<br><br>text",
strict: true
}, "<br> was removed from html when strict"
end
end

#
# Test assert_not_select.
#
Expand Down