[Rspec-devel] should_output proof-of-concept

David Chelimsky dchelimsky at gmail.com
Sun Aug 27 12:06:10 EDT 2006


Even without a patch, would you please submit this as a feature
request at rubyforge?

I like this idea. Anyone else?

Thanks,
David

On 8/26/06, E S <eero.saynatkari at kolumbus.fi> wrote:
> 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
>
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>


More information about the Rspec-devel mailing list