[Rspec-devel] should_output proof-of-concept

David Chelimsky dchelimsky at gmail.com
Mon Aug 28 21:59:49 EDT 2006


On 8/27/06, E S <eero.saynatkari at kolumbus.fi> wrote:
> > Lähettäjä: "David Chelimsky" <dchelimsky at gmail.com>
> > Päiväys: 2006/08/27 su PM 07:06:10 EEST
> > Vastaanottaja: rspec-devel at rubyforge.org
> > Aihe: Re: [Rspec-devel] should_output proof-of-concept
> >
> > Even without a patch, would you please submit this as a feature
> > request at rubyforge?
>
> Done. Added a workaround for Readline.
>
> > 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

Thanks for putting it on the site.

By any chance, do you have any tests? We'd like to keep the code coverage high.

Thanks,
David


More information about the Rspec-devel mailing list