From a7157c21ff78932dea7d4d2a10c0cd67e48ad0ab Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 25 Jun 2023 13:32:42 -0400 Subject: [PATCH] Use Minitest::Assertion#diff for content failure messages So where it previously would output: < "This is not a big problem," he said. > expected but was <"This is not a big problem," he said. >. Expected 0 to be >= 1. it now outputs: --- expected +++ actual @@ -1,3 +1,2 @@ -" -\"This is not a big problem,\" he said. +"\"This is not a big problem,\" he said. " . Expected 0 to be >= 1. Also improve the count failure message. --- lib/rails/dom/testing/assertions/selector_assertions.rb | 2 +- .../testing/assertions/selector_assertions/html_selector.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/rails/dom/testing/assertions/selector_assertions.rb b/lib/rails/dom/testing/assertions/selector_assertions.rb index dab6233..c86b53a 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions.rb @@ -275,7 +275,7 @@ def document_root_element def assert_size_match!(size, equals, css_selector, message = nil) min, max, count = equals[:minimum], equals[:maximum], equals[:count] - message ||= %(Expected #{count_description(min, max, count)} matching "#{css_selector}", found #{size}.) + message ||= %(Expected #{count_description(min, max, count)} matching #{css_selector.inspect}, found #{size}) if count assert_equal count, size, message else diff --git a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb index 7e927ce..1f9facf 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb @@ -4,6 +4,8 @@ class HTMLSelector #:nodoc: attr_reader :css_selector, :tests, :message + include Minitest::Assertions + def initialize(values, previous_selection = nil, &root_fallback) @values = values @root = extract_root(previous_selection, root_fallback) @@ -52,7 +54,7 @@ def filter(matches) content.sub!(/\A\n/, '') if text_matches && match.name == "textarea" next if regex_matching ? (content =~ match_with) : (content == match_with) - content_mismatch ||= sprintf("<%s> expected but was\n<%s>", match_with, content) + content_mismatch ||= diff(match_with, content) true end