Sha256: 48e29e1a323bd6f8f10529731e190c22e249488023d242a6bf03937b98925f45

Contents?: true

Size: 1.13 KB

Versions: 23

Compression:

Stored size: 1.13 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 do
    "expected to #{description}"
  end

  failure_message_when_negated 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

23 entries across 23 versions & 2 rubygems

Version Path
qbrick-2.7.1 spec/support/write_expectation.rb
qbrick-2.7 spec/support/write_expectation.rb
qbrick-2.6.10 spec/support/write_expectation.rb
qbrick-2.6.9 spec/support/write_expectation.rb
qbrick-2.6.8 spec/support/write_expectation.rb
qbrick-2.6.7 spec/support/write_expectation.rb
qbrick-2.6.6 spec/support/write_expectation.rb
qbrick-2.6.5 spec/support/write_expectation.rb
qbrick-2.6.4 spec/support/write_expectation.rb
qbrick-2.6.3 spec/support/write_expectation.rb
qbrick-2.6.2 spec/support/write_expectation.rb
qbrick-2.6.1 spec/support/write_expectation.rb
kuhsaft-2.6.3 spec/support/write_expectation.rb
qbrick-2.6.0 spec/support/write_expectation.rb
qbrick-2.5.2 spec/support/write_expectation.rb
qbrick-2.5.1 spec/support/write_expectation.rb
qbrick-2.5.0 spec/support/write_expectation.rb
kuhsaft-2.6.2 spec/support/write_expectation.rb
kuhsaft-2.6.1 spec/support/write_expectation.rb
kuhsaft-2.5.2 spec/support/write_expectation.rb