Sha256: c12076733237ce262711a318049d7e70bcb8859ca2b93c2bf7d3e0f067a9c02f

Contents?: true

Size: 1.67 KB

Versions: 82

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require 'stringio'

# Custom matcher to test text written to standard output and standard error
#
# @example
#   expect { $stderr.puts "Some random error" }.to write(/Some.* error/).to(:stderr)
#
# @example
#   expect { $stderr.puts "Some specific error" }.to write('Some specific error').to(:stderr)
#
RSpec::Matchers.define :write do |message|
  chain(:to) do |io|
    @io = io
  end

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

    case message
    when String then output.include? message
    when Regexp then output.match message
    else fail("Allowed types for write `message` are String or Regexp, got `#{message.class}`")
    end
  end

  def supports_block_expectations?
    true # or some logic
  end

  description do
    %Q[write #{message.inspect} to #{@io}]
  end

  def f_message(to = 'to')
    %Q[expected #{to} #{description} but got #{@buffer.inspect}]
  end

  failure_message do
    f_message 'to'
  end

  failure_message_when_negated do
    f_message 'not to'
  end

  # Fake STDERR and return a string written to it.
  def fake_stderr
    original_stderr = $stderr
    $stderr = StringIO.new
    yield
    @buffer = $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
    @buffer = $stdout.string
  ensure
    $stdout = original_stdout
  end

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

Version data entries

82 entries across 82 versions & 1 rubygems

Version Path
better_record-0.25.2 lib/better_record/rspec/expectations/write.rb
better_record-0.25.1 lib/better_record/rspec/expectations/write.rb
better_record-0.25.0 lib/better_record/rspec/expectations/write.rb
better_record-0.24.4 lib/better_record/rspec/expectations/write.rb
better_record-0.24.3 lib/better_record/rspec/expectations/write.rb
better_record-0.23.7 lib/better_record/rspec/expectations/write.rb
better_record-0.23.6 lib/better_record/rspec/expectations/write.rb
better_record-0.23.5 lib/better_record/rspec/expectations/write.rb
better_record-0.23.4 lib/better_record/rspec/expectations/write.rb
better_record-0.23.3 lib/better_record/rspec/expectations/write.rb
better_record-0.22.9 lib/better_record/rspec/expectations/write.rb
better_record-0.22.8 lib/better_record/rspec/expectations/write.rb
better_record-0.22.7 lib/better_record/rspec/expectations/write.rb
better_record-0.22.6 lib/better_record/rspec/expectations/write.rb
better_record-0.22.5 lib/better_record/rspec/expectations/write.rb
better_record-0.22.4 lib/better_record/rspec/expectations/write.rb
better_record-0.22.3 lib/better_record/rspec/expectations/write.rb
better_record-0.21.3 lib/better_record/rspec/expectations/write.rb
better_record-0.22.2 lib/better_record/rspec/expectations/write.rb
better_record-0.22.0 lib/better_record/rspec/expectations/write.rb