Skip to content

assert_dom :text collapses whitespace #123

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 @@ -52,15 +51,16 @@ def filter(matches)

content_mismatch = nil
text_matches = tests.has_key?(:text)
html_matches = tests.has_key?(:html)
regex_matching = match_with.is_a?(Regexp)

remaining = matches.reject do |match|
# Preserve markup with to_s for html elements
content = text_matches ? match.text : match.children.to_s
content = text_matches ? match.text : match.inner_html

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
collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || html_matches

next if regex_matching ? (content =~ match_with) : (content == match_with)
content_mismatch ||= diff(match_with, content)
Expand Down
43 changes: 6 additions & 37 deletions test/selector_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,70 +237,39 @@ 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
def test_assert_select_text_equality_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"
}, "Whitespace was not collapsed from text"
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"
}, "<br> was not removed from text"
end
end

def test_assert_select_strict_respects_whitespace
def test_assert_select_html_equality_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

assert_raises(Assertion) do
assert_select "p", {
html: "Some line-broken text",
strict: true
}
}, "Whitespace was collapsed from html"
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"
}, "<br> was removed from html"
end
end

Expand Down