Blame t/autodie.t

Packit 6427f8
#!/usr/bin/perl -w
Packit 6427f8
use strict;
Packit 6427f8
Packit 6427f8
use constant NO_SUCH_FILE => 'this_file_had_so_better_not_be_here';
Packit 6427f8
Packit 6427f8
use Test::More tests => 19;
Packit 6427f8
Packit 6427f8
{
Packit 6427f8
Packit 6427f8
    use autodie qw(open);
Packit 6427f8
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
    like($@,qr{Can't open},"autodie qw(open) in lexical scope");
Packit 6427f8
Packit 6427f8
    no autodie qw(open);
Packit 6427f8
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
    is($@,"","no autodie qw(open) in lexical scope");
Packit 6427f8
Packit 6427f8
    use autodie qw(open);
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
    like($@,qr{Can't open},"autodie qw(open) in lexical scope 2");
Packit 6427f8
Packit 6427f8
    no autodie; # Should turn off all autodying subs
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
    is($@,"","no autodie in lexical scope 2");
Packit 6427f8
Packit 6427f8
    # Turn our pragma on one last time, so we can verify that
Packit 6427f8
    # falling out of this block reverts it back to previous
Packit 6427f8
    # behaviour.
Packit 6427f8
    use autodie qw(open);
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
    like($@,qr{Can't open},"autodie qw(open) in lexical scope 3");
Packit 6427f8
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
is($@,"","autodie open outside of lexical scope");
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie;	# Should turn on everything
Packit 6427f8
    open(my $fh, '<', NO_SUCH_FILE);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
like($@, qr{Can't open}, "vanilla use autodie turns on everything.");
Packit 6427f8
Packit 6427f8
eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
is($@,"","vanilla autodie cleans up");
Packit 6427f8
Packit 6427f8
{
Packit 6427f8
    use autodie qw(:io);
Packit 6427f8
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
    like($@,qr{Can't open},"autodie q(:io) makes autodying open");
Packit 6427f8
Packit 6427f8
    no autodie qw(:io);
Packit 6427f8
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
    is($@,"", "no autodie qw(:io) disabled autodying open");
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
{
Packit 6427f8
    package Testing_autodie;
Packit 6427f8
Packit 6427f8
    use Test::More;
Packit 6427f8
Packit 6427f8
    use constant NO_SUCH_FILE => ::NO_SUCH_FILE();
Packit 6427f8
Packit 6427f8
    use Fatal qw(open);
Packit 6427f8
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
Packit 6427f8
    like($@, qr{Can't open}, "Package fatal working");
Packit 6427f8
    is(ref $@,"","Old Fatal throws strings");
Packit 6427f8
Packit 6427f8
    {
Packit 6427f8
        use autodie qw(open);
Packit 6427f8
Packit 6427f8
        ok(1,"use autodie allowed with Fatal");
Packit 6427f8
Packit 6427f8
        eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
        like($@, qr{Can't open}, "autodie and Fatal works");
Packit 6427f8
        isa_ok($@, "autodie::exception"); # autodie throws real exceptions
Packit 6427f8
Packit 6427f8
    }
Packit 6427f8
Packit 6427f8
    eval { open(my $fh, '<', NO_SUCH_FILE); };
Packit 6427f8
Packit 6427f8
    like($@, qr{Can't open}, "Package fatal working after autodie");
Packit 6427f8
    is(ref $@,"","Old Fatal throws strings after autodie");
Packit 6427f8
Packit 6427f8
    eval " no autodie qw(open); ";
Packit 6427f8
Packit 6427f8
    ok($@,"no autodie on Fataled sub an error.");
Packit 6427f8
Packit 6427f8
    eval "
Packit 6427f8
        no autodie qw(close);
Packit 6427f8
        use Fatal 'close';
Packit 6427f8
    ";
Packit 6427f8
Packit 6427f8
    like($@, qr{not allowed}, "Using fatal after autodie is an error.");
Packit 6427f8
}
Packit 6427f8