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

noreply at rubyforge.org noreply at rubyforge.org
Sun Nov 18 20:28:30 EST 2007


Feature Requests item #5544, was opened at 2006-08-28 00:24
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=5544&group_id=797

Category: None
Group: None
>Status: Closed
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 ''
                else
                  File.open '/tmp/rspec_output_#{$$}', 'w+'
                end                   # if readline
      }                               # eval
       
      @target.call 
      eval(to).rewind
      output = eval(to).read 

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


----------------------------------------------------------------------

Comment By: Chad Humphries (spicycode)
Date: 2007-11-19 01:23

Message:
Moved to http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/19-5544-should_output-includes-complete-method-definition#ticket-19-2

----------------------------------------------------------------------

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

Message:
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.

Thanks,
David

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-08-29 12:04

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


----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=5544&group_id=797


More information about the rspec-devel mailing list