szabgab has asked for the wisdom of the Perl Monks concerning the following question:
use Test::More tests => 3; ok(1, "Before"); BEGIN { package M; use TE; package main; } use TE; use M; exits_ok( sub {f()}, -2, "exits"); ok(1, "After");
package M; use strict; require Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(f); sub f { exit(-1); } 1;
package TE; use strict; require Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(exit exits_ok); use Test::Builder; my $Test = Test::Builder->new(); sub exit { my ($arg) = @_; $arg = '' if not defined $arg; die "EXIT$arg\n"; } sub exits_ok (&$;$$) { my ($coderef, $expecting, $description) = @_; $expecting = '' if not defined $expecting; $description ||= "exit $expecting"; my $result; eval {&$coderef}; my $ex = $@; $Test->ok($ex and $ex eq "EXIT$expecting\n", $description); if ($ex) { if ($ex =~ /^EXIT(.*)/) { if ($1 ne $expecting) { $Test->diag("Received '$1' expected '$expecting'"); } } else { $Test->diag("died before reaching an exit statement"); } } } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: testing exit in Perl modules
by adrianh (Chancellor) on Jul 20, 2005 at 16:19 UTC | |
|
Re: testing exit in Perl modules
by chromatic (Archbishop) on Jul 20, 2005 at 17:50 UTC |