[Rspec-devel] should_output proof-of-concept

E S eero.saynatkari at kolumbus.fi
Sat Aug 26 19:25:02 EDT 2006


Hi! Sorry if this is the wrong place--and apologies that
this is not in patch format (I do not have SVN access and
did not want to try to patch from 0.6.1). I currently just
have this hacked in a helper lib I #require for the specs.
I was briefly pondering about something similar for input
but that is more of a context than specification thing.

Thoughts?

# 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 value
      eval "#{to}, old_to = StringIO.new(''), #{to}"
       
      @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"
    end                               # output()
  end                                 # ShouldHelper
end                                   # Spec



More information about the Rspec-devel mailing list