Using Test::Unit to assert messages appeared on $stdout/$stde rr

From: Massimiliano Mirra [mailto:list@NOSPAMchromatic-harp.com]

I don’t see any obvious assertion in Test::Unit that allows
checking of I/O to $stdout/$stderr. What’s a good way to
test for such a case?

Get stringio.rb from www.allruby.com/rpkg/stringio.rb

Massimiliano –

Thanks for the suggestion! I have installed stringio.rb but am having
problems with the redir method. I get ‘uninitialized stream’ errors in both
my code and your unit tests, on many flavors of Ruby (windows/linux, 1.6.5
through 1.7.2):

% ruby-latest -v /users/jgray/pkgs/ruby/lib/stringio.rb
ruby 1.7.2 (2002-06-04) [i686-linux]
Loaded suite /users/jgray/pkgs/ruby/lib/stringio
Started…

Error occurred in test_redirect(TestStringIO): IOError: uninitialized stream
/users/jgray/pkgs/ruby/lib/stringio.rb:133:in redir' /users/jgray/pkgs/ruby/lib/stringio.rb:151:intest_redirect’
/users/jgray/pkgs/ruby/lib/stringio.rb:265

Finished in 0.04797999999999999 seconds.
16 runs, 25 assertions, 0 failures, 1 errors

The context in stringio.rb:
131 begin
132 $defout, $stderr = redout, rederr
=> 133 $stdout = redout
134 yield
135 ensure

Any ideas what might be happening?

Thanks,

- jeff

Hi,

···

At Wed, 21 Aug 2002 06:17:19 +0900, Gray, Jeff jeff.gray@intel.com wrote:

% ruby-latest -v /users/jgray/pkgs/ruby/lib/stringio.rb
ruby 1.7.2 (2002-06-04) [i686-linux]
Loaded suite /users/jgray/pkgs/ruby/lib/stringio
Started…

Error occurred in test_redirect(TestStringIO): IOError: uninitialized stream
/users/jgray/pkgs/ruby/lib/stringio.rb:133:in redir' /users/jgray/pkgs/ruby/lib/stringio.rb:151:intest_redirect’
/users/jgray/pkgs/ruby/lib/stringio.rb:265

Finished in 0.04797999999999999 seconds.
16 runs, 25 assertions, 0 failures, 1 errors

Because StringIO inherits IO but #initialize doesn’t call super.

Additonally, 1.7 has StringIO extesion.


Nobu Nakada

That’s because, for some reason now known only to the gods, I made
StringIO inherit from IO. It does not support opening modes and all
that, so I can’t see why I did that. Oh well, age… Anyway, just
nuke the `< IO’ in the first line and it should work fine for what it
was written for (testing user interaction, you name it ;-)).

BTW, I agree with Nat that a class should be free from references to
the external world such as the $stdin and $stdout implicit in puts and
gets. I also should add that I walked that path when writing classes
explicitely aimed at user I/O, and writing and reading lots of calls
to @stdin.gets and @stdout.gets quickly became such a pain that the
feel of good engineering could not balance it. Depending on the
purpose of your classes, YMMV, and I’d be the first to cut
away $stdin’s and $stdout’s in a non-I/O intensive class.

Massimiliano

···

On Wed, Aug 21, 2002 at 06:17:19AM +0900, Gray, Jeff wrote:

Thanks for the suggestion! I have installed stringio.rb but am having
problems with the redir method. I get ‘uninitialized stream’ errors in both
my code and your unit tests, on many flavors of Ruby (windows/linux, 1.6.5
through 1.7.2):