Sha256: 1e6ec236a35316aec0fb75dc33b71368f632355484ba995bd118b03e08314c70

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

require 'mspec/guards/feature'
require 'mspec/helpers/io'

class HaveDataMatcher
  def initialize(data, mode="rb:binary")
    @data = data
    @mode = mode
  end

  def matches?(name)
    @name = name

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

    File.open @name, fmode(@mode) 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, mode="rb:binary")
    HaveDataMatcher.new(data, mode)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mspec-1.9.1 lib/mspec/matchers/have_data.rb
mspec-1.9.0 lib/mspec/matchers/have_data.rb
mspec-1.8.0 lib/mspec/matchers/have_data.rb
mspec-1.7.0 lib/mspec/matchers/have_data.rb
mspec-1.6.0 lib/mspec/matchers/have_data.rb
mspec-1.5.21 lib/mspec/matchers/have_data.rb
mspec-1.5.20 lib/mspec/matchers/have_data.rb
mspec-1.5.19 lib/mspec/matchers/have_data.rb
mspec-1.5.18 lib/mspec/matchers/have_data.rb