Sha256: 5e0930350404a2b9f816784a8995c4280b4a1551d595c333e47d7a3011d28d7b

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

require 'mspec/guards/feature'
require 'mspec/helpers/fmode'

class HaveDataMatcher
  def initialize(data)
    @data = data
  end

  def matches?(name)
    @name = name

    if FeatureGuard.enabled? :encoding
      size = @data.bytesize
    else
      size = @data.size
    end

    File.open @name, fmode("rb:binary") do |f|
      return f.read(size) == @data
    end
  end

  def failure_message
    ["Expected #{@name}",
     "to have data #{@data.pretty_inspect}"]
  end

  def negative_failure_message
    ["Expected #{@name}",
     "not to have data #{@data.pretty_inspect}"]
  end
end

class Object
  # Opens a file specified by the string the matcher is called on
  # and compares the +data+ passed to the matcher with the contents
  # of the file. Expects to match the first N bytes of the file
  # with +data+. For example, suppose @name is the name of a file:
  #
  #   @name.should have_data("123")
  #
  # passes if the file @name has "123" as the first 3 bytes. The
  # file can contain more bytes than +data+. The extra bytes do not
  # affect the result.
  def have_data(data)
    HaveDataMatcher.new(data)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mspec-1.5.17 lib/mspec/matchers/have_data.rb
mspec-1.5.16 lib/mspec/matchers/have_data.rb