mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
Create and use new rspec matcher 'match_html'
This introduces match_html, which converts actual and expected HTML strings into "canonical" HTML using Nokogiri with NOBLANKS and COMPACT, and then does a simple equality comparison. This eliminates whitespace differences introduced by library changes (e.g. the libxml2 2.9.0 change). pretty_text_spec.rb has been changed to use match_html where appropriate. and all tests pass under libxml2 2.7.6, 2.8.0 or 2.9.0
This commit is contained in:
21
spec/support/match_html_matcher.rb
Normal file
21
spec/support/match_html_matcher.rb
Normal file
@ -0,0 +1,21 @@
|
||||
require 'nokogiri/xml/parse_options'
|
||||
RSpec::Matchers.define :match_html do |expected|
|
||||
match do |actual|
|
||||
a = make_canonical_html expected
|
||||
b = make_canonical_html actual
|
||||
a.to_html == b.to_html
|
||||
end
|
||||
|
||||
failure_message_for_should do |actual|
|
||||
"after sanitizing for extra white space and compactness, expected #{actual} to match #{expected}"
|
||||
end
|
||||
|
||||
failure_message_for_should_not do |actual|
|
||||
"after sanitizing for extra white space and compactness, expected #{actual} not to match #{expected}"
|
||||
end
|
||||
|
||||
def make_canonical_html(html)
|
||||
Nokogiri::HTML(html) { |config| config.options = Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::COMPACT }
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user