Sha256: 139821ff48687795af3b6003b247413a6ea2cc84cc744ddc6e9ffa783ba2ebc6

Contents?: true

Size: 1.18 KB

Versions: 49

Compression:

Stored size: 1.18 KB

Contents

require 'io/grab'

# 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)
#
# @note http://greyblake.com/blog/2012/12/14/custom-expectations-with-rspec/
RSpec::Matchers.define :write do |message|
  supports_block_expectations

  chain(:to) do |io|
    @io = io
  end

  match do |block|
    stream = case io
    when :stdout
      $stdout
    when :stderr
      $stderr
    else
      io
    end

    @actual = output = stream.grab &block

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

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

  diffable

  failure_message do
    %Q[expected to #{description} but got #{@actual.inspect}]
  end

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

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
conjur-cli-6.2.6 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.2.5 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.2.4 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.2.3 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.2.2 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.2.1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.2.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.6.6 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.1.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.0.1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.6.5 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.6.4 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.6.3 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.0.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-6.0.0.rc1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.5.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.4.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.3.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.2.5 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.2.4 lib/conjur/command/rspec/output_matchers.rb