Update of /cvsroot/fink/fink/t/Finally
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22722/t/Finally

Added Files:
        ChangeLog finally.t 
Log Message:
new finalizer mechanism

--- NEW FILE: finally.t ---
#!/usr/bin/perl
use warnings;
use strict;

use Test::More 'no_plan';
use File::Temp qw(tempfile);

BEGIN { use_ok 'Fink::Finally'; }

# bad args
{
        eval { Fink::Finally->new };
        ok($@, 'bad args - no args');
        eval { Fink::Finally->new('not code') };
        ok($@, 'bad args - not a code ref');
}

# explicit cleanup
{
        my $x = 0;
        my $finally = Fink::Finally->new(sub { $x++ });
        $finally->run;
        is($x, 1, 'explicit cleanup');
}

# automatic cleanup
{
        my $x = 0;
        {
                my $finally = Fink::Finally->new(sub { $x++ });
        }
        is($x, 1, 'automatic cleanup');
}

# exceptional cleanup
{
        my $x = 0;
        eval {
                my $finally = Fink::Finally->new(sub { $x++ });
                die "exception!";
        };
        ok($@, 'exceptional cleanup - exception thrown');
        is($x, 1, 'exceptional cleanup - executed');
}

# run once
{
        my $x = 0;
        {
                my $finally = Fink::Finally->new(sub { $x++ });
                $finally->run;
                $finally->run;
                # out of scope => automatic run
        }
        is($x, 1, 'run once');
}

# cleanup on exit
{
        my $script = <<'SCRIPT';
use Fink::Finally;
my $finally = Fink::Finally->new(sub { print "cleanup\n" });
exit 0;
SCRIPT
        my ($fh, $fname) = tempfile(".capture.XXXX");
        print $fh $script;
        close $fh;
        
        local $ENV{PERL5LIB} = join(':', @INC);
        open my $subproc, '-|', "perl $fname" or die "Can't open subproc: $!";
        my $out = join('', <$subproc>);
        close $subproc;
        
        is($out, "cleanup\n", "cleanup on exit");
        unlink $fname;
}

# cancellation
{
        my $x = 0;
        {
                my $fin = Fink::Finally->new(sub { $x++ });
                $fin->cancel;
                $fin->run;
        } # out of scope
        is($x, 0, 'cancellation');
}

--- NEW FILE: ChangeLog ---
2006-03-21  Dave Vasilevsky  <[EMAIL PROTECTED]>

        * NEW DIRECTORY, finally.t: Test Fink::Finally.



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Fink-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/fink-commits

Reply via email to