Sha256: d2d0df6e43558d2bd854df118d59ba1ac2072356922553a118c63344157678da

Contents?: true

Size: 815 Bytes

Versions: 2

Compression:

Stored size: 815 Bytes

Contents

require_relative 'assertion'

# Assert that there is output, either from stdout or stderr.
#
#   OutputAssay.pass?(/foo/){ puts 'foo!' }  #=> true
#
class OutputAssay < Assertion

  register :output

  #
  # Compare +match+ against $stdout and $stderr via `#===` method.
  # 
  # Note that $stdout and $stderr are temporarily reouted to StringIO
  # objects and the results have any trailing newline chomped off.
  #
  def self.pass?(match, &block)
    require 'stringio'

    begin
      stdout, stderr = $stdout, $stderr
      newout, newerr = StringIO.new, StringIO.new
      $stdout, $stderr = newout, newerr
      yield  
    ensure
      $stdout, $stderr = stdout, stderr
    end

    newout, newerr = newout.string.chomp("\n"), newerr.string.chomp("\n")

    match === newout || match === newerr
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assay-0.4.1 lib/assay/output_assay.rb
assay-0.4.0 lib/assay/output_assay.rb