Re: Test::Simple fails "output" subtest 2 on Cygwin/2000

2002-07-07 Thread Danny Faught

Michael G Schwern wrote:
> Never seen it in 5.6.1, but it sounds like an odd chomp() mistake.  Where
> did you get this perl?

I installed Perl along with Cygwin.  Here's what I'm seeing with chomp. 
I'm not familiar enough with Perl on Windows to know what's expected
from chomp, or whether the string in question should have a CR in the
first place.

  DB<1> $foo = "a\r\n"

  DB<2> chomp $foo

  DB<3> print length $foo
2
  DB<4> print ord(substr $foo, 0, 1)
97
  DB<5> print ord(substr $foo, 1, 1)
13

$/ is set to a newline.

> > Also, I see a few tests are skipped because they say they need
> > Test::Harness version 1.20 or 1.23.  But I just installed 1.26 - CPAN
> > listed it as a prerequisite.
> 
> Did you install it or "install" it?  ie. Did you run 'make install' (or
> nmake install) or just copy the .pm files somewhere?  The normal
> installation process should overwrite the old Test::Harness.

I used the CPAN module.  "perl -MCPAN -e shell" and "install
Test::Harness".

> PS  Test::Harness is way beyond 1.26.

Sorry, I meant 2.26 (the references to 1.20 and 1.23 above are correct). 

Turns out I have 1.1604 installed.  2.26 doesn't seem to be installing
correctly:


All tests successful, 48 subtests skipped.
Files=9, Tests=447, 22 wallclock secs ( 8.78 cusr + 11.87 csys = 20.65
CPU)
  /usr/bin/make test -- OK
Running make install
Installing /usr/man/man3/Test.Harness.3
Installing /usr/man/man3/Test.Harness.Assert.3
Installing /usr/man/man3/Test.Harness.Iterator.3
Installing /usr/man/man3/Test.Harness.Straps.3
Writing /usr/lib/perl5/5.6.1/cygwin-multi/auto/Test/Harness/.packlist
Appending installation info to
/usr/lib/perl5/5.6.1/cygwin-multi/perllocal.pod
  /usr/bin/make install  -- OK

I still have 1.1604 installed at this point.

$ perl -e 'use Test::Harness; print $Test::Harness::VERSION'
1.1604
--
Danny R. Faught
Tejas Software Consulting
http://www.tejasconsulting.com/



testing with stubs

2002-12-12 Thread Danny Faught
I'm writing an article about test-first programming for Linux Magazine. 
 Unfortunately, my forte is more with black-box testing.  Could I get 
some suggestions about implementation?

I have a legacy Perl script, not object-oriented but at least ported to 
Perl 5.  I want to use a real-world example, rather than new code like 
what Kent Beck uses in the book Test-Driven Development.  So I thought 
I'd implement a unit test for one of the functions in this old script. 
I'm using Test::Unit::Procedural.

I'm pulling my hair out trying to stub out the functions that the 
function under test is calling.  Here's the function (complete with an 
error that will be corrected as part of the exercise):

sub abort_handler {
my ($event) = @_;
print STDERR "stress_driver: aborted by SIG$event->data()\n";
&log ("stress_driver: aborted by SIG$event->data()");
exit (&cleanup);
}

I want to stub out (replace) the &log and &cleanup functions and the 
builtin exit function.  I'll probably also want to redirect STDERR to 
capture that output as well, or more likely I should refactor that line 
into a different log subroutine.  Of course, I need to set up the test 
harness with minimal or no modifications to the code under test.

I can use "use subs" and the exporter to override the exit call.  And I 
can use an ugly typeglob hack to override the user-defined subroutines. 
 But I can't figure out how to turn unit test mode on and off.  The 
only way I can get the override of exit() to work is to "use sd_stubs" 
to pull it in with the exporter.  Here's the code that I added to the 
top of the main program that tries to do that:

if ($ARGV[0] && $ARGV[0] eq "-t") {
use sd_stubs;
require sd_unit_test;
}

If I try to run the program without "-t", so it functions as it did 
before, my exit stub is still in place.  This code fixes that problem 
(after moving exit from @EXPORT to @EXPORT_OK):

BEGIN {
if ($ARGV[0] && $ARGV[0] eq "-t") {
my @overrides = qw(exit);
use sd_stubs @overrides;
require sd_unit_test;
}
}

But now the sd_unit_test package can't see the &main::abort_handler 
function.  I'm lost in a mess of namespaces at this point.

Can someone point out in what way I'm being boneheaded here, and/or what 
the most reasonable way to stub out these functions would be?  Here's 
sd_stubs.pm and sd_unit_test.pm.  I split them into two files to solve a 
different problem I ran across.

-
package sd_stubs;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(exit);
use warnings;
use strict;
use subs qw(exit);

sub log_stub {
print "LOG_STUB\n";
return 0;
}

sub exit {
print "EXIT_STUB\n";
return 0;
}

1;

package sd_unit_test;
use warnings;
use strict;
use Test::Unit::Procedural;

my $log_main = \&log;
{
no warnings;
*main::log = \&sd_stubs::log_stub;
}

sub test_abort1 {
assert (main::abort_handler() == 0);
}
create_suite();
CORE::exit(run_suite());

1;
--
Danny Faught
Tejas Software Consulting
http://www.tejasconsulting.com