[rspec-devel] [ rspec-Feature Requests-5544 ] should_output (includes complete method definition)

noreply at rubyforge.org noreply at rubyforge.org
Thu Oct 26 11:58:59 EDT 2006

Feature Requests item #5544, was opened at 2006-08-28 00:24
You can respond by visiting: 

Category: None
Group: None
Status: Open
Priority: 3
Submitted By: Eero Saynatkari (esaynatkari)
Assigned to: Nobody (None)
Summary: should_output (includes complete method definition)

Initial Comment:
This expectation provides a way to specify program output.
It takes into account Readline which refuses to allow use
of StringIO or Tempfile. It is a bit ugly having to work
around symbolic names but seems to work fine in my code.

# Usage examples:

 lambda {UI.mainloop {|ui| ui.output 'foo'}}.should_output "foo\n"
 lambda {UI.mainloop {|ui| ui.output 'foo'}}.should_output /foo\n/

 lambda {UI.mainloop {|ui| ui.error 'foo'}}.should_output "foo\n", :to => '$stderr'

# Code:

# Modify RSpec a little
module Spec
  class ShouldHelper

    # Verify output from block is expected
    def output(expected, io = {:to => '$stdout'})
      to = io[:to]

      # Store the old stream
      eval "old_to = #{to}"

      # Replace, work around Readline
      eval %{
        #{to} = if $LOADED_FEATURES.grep(/readline/i).empty?
                  StringIO.new ''
                  File.open '/tmp/rspec_output_#{$$}', 'w+'
                end                   # if readline
      }                               # eval
      output = eval(to).read 

      # Match up
      case expected
        when Regexp
          output.should_match expected
          output.should_equal expected
      end                             # case expected
    # Clean up
      eval %{
        #{to}, old_to = old_to, #{to}
        if old_to and old_to.kind_of? File
          require 'fileutils'
          FileUtils.rm_rf old_to.path
        end                           # if old_to
      }                               # eval
    end                               # output()
  end                                 # ShouldHelper
end                                   # Spec


>Comment By: David Chelimsky (dchelimsky)
Date: 2006-10-26 15:58

Eero - I like this idea a lot. Would you consider submitting this as a patch against the trunk (pls indicate the revision number against which you are patching) including specs written in rspec.



Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-08-29 12:04

Looks good, but where is the test case for this?


You can respond by visiting: 

More information about the rspec-devel mailing list