Sha256: 728ad2285a2bb023d482c6f5457a871b8c660df40644690e1614745e54a8a4f9

Contents?: true

Size: 1.14 KB

Versions: 12

Compression:

Stored size: 1.14 KB

Contents

RSpec::Matchers.define :write do |message|
  chain(:to) do |io|
    @io = io
  end

  match do |block|
    output =
      case io
      when :output then fake_stdout(&block)
      when :error  then fake_stderr(&block)
      else raise("Allowed values for `to` are :output and :error, got `#{io.inspect}`")
      end
    output.include? message
  end

  description do
    "write \"#{message}\" #{io_name}"
  end

  failure_message_for_should do
    "expected to #{description}"
  end

  failure_message_for_should_not do
    "expected to not #{description}"
  end

  # Fake STDERR and return a string written to it.
  def fake_stderr
    original_stderr = $stderr
    $stderr = StringIO.new
    yield
    $stderr.string
  ensure
    $stderr = original_stderr
  end

  # Fake STDOUT and return a string written to it.
  def fake_stdout
    original_stdout = $stdout
    $stdout = StringIO.new
    yield
    $stdout.string
  ensure
    $stdout = original_stdout
  end

  # default IO is standard output
  def io
    @io ||= :output
  end

  # IO name is used for description message
  def io_name
    { output: 'standard output', error: 'standard error' }[io]
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
qbrick-2.5.0.pre spec/support/write_expectation.rb
kuhsaft-2.4.3 spec/support/write_expectation.rb
kuhsaft-2.4.2 spec/support/write_expectation.rb
kuhsaft-2.4.1 spec/support/write_expectation.rb
kuhsaft-2.4.0 spec/support/write_expectation.rb
kuhsaft-2.3.6 spec/support/write_expectation.rb
kuhsaft-2.3.5 spec/support/write_expectation.rb
kuhsaft-2.3.4 spec/support/write_expectation.rb
kuhsaft-2.3.3 spec/support/write_expectation.rb
kuhsaft-2.3.2 spec/support/write_expectation.rb
kuhsaft-2.3.1 spec/support/write_expectation.rb
kuhsaft-2.3.0 spec/support/write_expectation.rb