Skip to content

Instantly share code, notes, and snippets.

@jim
Created October 1, 2008 16:24
Show Gist options
  • Save jim/14111 to your computer and use it in GitHub Desktop.
Save jim/14111 to your computer and use it in GitHub Desktop.
module Spec
module Matchers
module Markup
class MatchMarkup
def initialize(expected)
@expected = expected
end
def matches?(target)
@target = target
standardize(@target).eql?(standardize(@expected))
end
def failure_message
"expected\n#{@target}\nto match\n#{@expected}"
end
def negative_failure_message
"expected\n#{@target}\nto not match\n#{@expected}"
end
# alphabetize the order of attributes
def standardize(markup)
clean_whitespace(markup).gsub(/( [a-zA-Z_]*="[^"])+"/) do |match|
' ' + match.strip!.split(' ').sort.join(' ')
end
end
def clean_whitespace(markup)
markup.
gsub(/\s+/, ' '). # cleanup whitespace
gsub(/>\s/, '>'). # kill space after tags
gsub(/\s</, '<'). # space before tags
gsub(/\s\/>/, '/>'). # space inside self-closing tags
strip
end
end
def match_markup(markup)
MatchMarkup.new(markup)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment