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-5.2.3 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.2.1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.2.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.1.2 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.1.1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-5.1.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.30.1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.30.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.29.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.28.2 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.28.1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.28.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.27.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.26.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.25.2 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.25.1 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.25.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.24.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.23.0 lib/conjur/command/rspec/output_matchers.rb
conjur-cli-4.22.0 lib/conjur/command/rspec/output_matchers.rb