Skip to content

Commit

Permalink
afficher le diff lorsqu’une spec échoue (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
adipasquale authored Feb 6, 2025
1 parent 53ef576 commit ca65a13
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ gem "actionview"
gem "activemodel"
gem "nokogiri"
gem "uri" # necessary on github actions
gem "diffy"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ GEM
connection_pool (2.5.0)
crass (1.0.6)
diff-lcs (1.5.1)
diffy (3.4.3)
drb (2.2.1)
erubi (1.13.1)
i18n (1.14.7)
Expand Down Expand Up @@ -143,6 +144,7 @@ PLATFORMS
DEPENDENCIES
actionview
activemodel
diffy
dsfr-form_builder!
nokogiri
rspec
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
require 'active_support'
require 'active_model'
require 'nokogiri'
require 'diffy'

Dir[File.expand_path('{../lib/**/*.rb,support/**/*.rb}', __dir__)].sort.each { |f| require f }
21 changes: 12 additions & 9 deletions spec/support/matchers/html_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,31 @@ def normalize_html(html)
attr.value = attr.value.strip
end
end
if node.text?
node.content = node.content.strip
end
end
# remove all whitespaces between tags
doc.to_s.gsub(/>\s+</, '><')
end

def beautify_html(html)
# cf https://stackoverflow.com/a/7839017
Nokogiri::XML(html, &:noblanks).to_s.sub('<?xml version="1.0"?>', '')
Nokogiri::HTML.fragment(html).to_xhtml(indent: 2)
end

match do |actual|
@actual = actual
normalize_html(expected) == normalize_html(actual)
@actual_normalized = normalize_html(actual)
@expected_normalized = normalize_html(expected)
@actual_normalized == @expected_normalized
end

failure_message do
expected_beautified = beautify_html(@expected_normalized)
actual_beautified = beautify_html(@actual_normalized)
differences = Diffy::Diff.new(actual_beautified, expected_beautified).to_s(:text)
<<~MSG
--- Expected ---
#{beautify_html(expected)}
--- Actual ---
#{beautify_html(@actual)}
--- Differences ---
#{differences}
MSG
end
end

0 comments on commit ca65a13

Please sign in to comment.