Sha256: 5d20b9a58da153c2d28bb2057e09b89e49173a75e0a36a835395592678f1aceb

Contents?: true

Size: 805 Bytes

Versions: 15

Compression:

Stored size: 805 Bytes

Contents

class MatchYAMLMatcher

  def initialize(expected)
    if valid_yaml?(expected)
      @expected = expected
    else
      @expected = expected.to_yaml
    end
  end

  def matches?(actual)
    @actual = actual
    clean_yaml(@actual) == clean_yaml(@expected)
  end

  def failure_message
    ["Expected #{@actual.inspect}", " to match #{@expected.inspect}"]
  end

  def negative_failure_message
    ["Expected #{@actual.inspect}", " to match #{@expected.inspect}"]
  end

  protected

  def clean_yaml(yaml)
    yaml.gsub(/([^-]|^---)\s+\n/, "\\1\n").sub(/\n\.\.\.\n$/, "\n")
  end

  def valid_yaml?(obj)
    require 'yaml'
    begin
      YAML.load(obj)
    rescue
      false
    else
      true
    end
  end
end

class Object
  def match_yaml(expected)
    MatchYAMLMatcher.new(expected)
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
rhodes-7.6.0 lib/extensions/mspec/mspec/matchers/match_yaml.rb
rhodes-7.5.1 lib/extensions/mspec/mspec/matchers/match_yaml.rb
rhodes-7.4.1 lib/extensions/mspec/mspec/matchers/match_yaml.rb
rhodes-7.1.17 lib/extensions/mspec/mspec/matchers/match_yaml.rb
rhodes-6.2.0 lib/extensions/mspec/mspec/matchers/match_yaml.rb
rhodes-6.0.11 lib/extensions/mspec/mspec/matchers/match_yaml.rb
mspec-1.9.1 lib/mspec/matchers/match_yaml.rb
mspec-1.9.0 lib/mspec/matchers/match_yaml.rb
mspec-1.8.0 lib/mspec/matchers/match_yaml.rb
mspec-1.7.0 lib/mspec/matchers/match_yaml.rb
mspec-1.6.0 lib/mspec/matchers/match_yaml.rb
mspec-1.5.21 lib/mspec/matchers/match_yaml.rb
mspec-1.5.20 lib/mspec/matchers/match_yaml.rb
mspec-1.5.19 lib/mspec/matchers/match_yaml.rb
mspec-1.5.18 lib/mspec/matchers/match_yaml.rb