Created
October 1, 2008 16:24
-
-
Save jim/14111 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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