Acme-Shotgun

 view release on metacpan or  search on metacpan

t/01_blastin.t  view on Meta::CPAN


## Fire

subtest 'fire dies with no target' => sub {
    my $gun = quiet_gun();
    dies_ok { $gun->fire() } 'fire with no target dies';
    like($@, qr/No target specified/, 'error mentions no target');
};

subtest 'fire dies on nonexistent file' => sub {
    my $gun = quiet_gun();
    dies_ok { $gun->fire(target => '/no/such/file.txt') }
        'fire on nonexistent file dies';
    like($@, qr/does not exist/, 'error mentions file does not exist');
};

subtest 'fire expends all rounds' => sub {
    my $gun    = quiet_gun();
    my $target = make_target();
    $gun->fire(target => $target);
    is($gun->{num_rounds}, 0, 'all rounds expended after fire');
};

subtest 'fire returns $self for chaining' => sub {
    my $gun    = quiet_gun();
    my $target = make_target();
    my $ret    = $gun->fire(target => $target);
    is($ret, $gun, 'fire returns $self');
};

subtest 'fire with empty mag does not die' => sub {
    my $gun    = quiet_gun();
    my $target = make_target();
    $gun->{num_rounds} = 0;
    lives_ok { $gun->fire(target => $target) } 'fire with empty mag does not die';
};

## Actual file damage

subtest 'fire modifies file contents' => sub {
    my $gun    = quiet_gun(type => 'pump', shots => 5);
    my $target = make_target();

    open my $fh, '<', $target or die "Can't open: $!";
    my $before = do { local $/; <$fh> };
    close $fh;

    # Fire multiple volleys to ensure at least one shot lands on text.
    for (1..5) {
        $gun->reload();
        $gun->fire(target => $target);
    }

    open $fh, '<', $target or die "Can't open: $!";
    my $after = do { local $/; <$fh> };
    close $fh;

    isnt($before, $after, 'file contents changed after firing');
};

subtest 'debug mode does not modify file' => sub {
    my $gun    = quiet_gun(debug => 1);
    my $target = make_target();

    open my $fh, '<', $target or die "Can't open: $!";
    my $before = do { local $/; <$fh> };
    close $fh;

    $gun->fire(target => $target);

    open $fh, '<', $target or die "Can't open: $!";
    my $after = do { local $/; <$fh> };
    close $fh;

    is($before, $after, 'file contents unchanged in debug mode');
};

subtest 'all ammo types fire without error' => sub {
    for my $load (qw(bird buck slug)) {
        my $gun    = quiet_gun(load => $load);
        my $target = make_target();
        lives_ok { $gun->fire(target => $target) } "$load fires without error";
    }
};

subtest 'all shotgun types fire without error' => sub {
    for my $type (qw(double pump)) {
        my $gun    = quiet_gun(type => $type);
        my $target = make_target();
        lives_ok { $gun->fire(target => $target) } "$type fires without error";
    }
};

done_testing();



( run in 0.460 second using v1.01-cache-2.11-cpan-e1769b4cff6 )