class MatchYAMLMatcher

Public Class Methods

new(expected) click to toggle source
# File lib/mspec/matchers/match_yaml.rb, line 3
def initialize(expected)
  if valid_yaml?(expected)
    @expected = expected
  else
    @expected = expected.to_yaml
  end
end

Public Instance Methods

failure_message() click to toggle source
# File lib/mspec/matchers/match_yaml.rb, line 16
def failure_message
  ["Expected #{@actual.inspect}", " to match #{@expected.inspect}"]
end
matches?(actual) click to toggle source
# File lib/mspec/matchers/match_yaml.rb, line 11
def matches?(actual)
  @actual = actual
  clean_yaml(@actual) == clean_yaml(@expected)
end
negative_failure_message() click to toggle source
# File lib/mspec/matchers/match_yaml.rb, line 20
def negative_failure_message
  ["Expected #{@actual.inspect}", " to match #{@expected.inspect}"]
end

Protected Instance Methods

clean_yaml(yaml) click to toggle source
# File lib/mspec/matchers/match_yaml.rb, line 26
def clean_yaml(yaml)
  yaml.gsub(/([^-]|^---)\s+\n/, "\\1\n").sub(/\n\.\.\.\n$/, "\n")
end
valid_yaml?(obj) click to toggle source
# File lib/mspec/matchers/match_yaml.rb, line 30
def valid_yaml?(obj)
  require 'yaml'
  begin
    YAML.load(obj)
  rescue
    false
  else
    true
  end
end