Acme-Shotgun

 view release on metacpan or  search on metacpan

Readme.md  view on Meta::CPAN


    my $gun = Acme::Shotgun->new(
        type    => 'double',  # 'double' (default) or 'pump'
        load    => 'bird',    # 'bird' (default), 'buck', or 'slug'
        shots   => undef,     # optional: cap the number of rounds loaded
        quiet   => 0,         # suppress all output
        debug   => 0,         # dry-run mode, no file modifications
        verbose => 1,         # verbose output (disabled automatically if quiet)
    );

Dies with an error if an invalid `type` or `load` value is given.

## reload()

Loads the magazine for the current shotgun type and ammunition. Default
capacity is 2 rounds for `double` and 5 rounds for `pump`. If `shots`
was set in the constructor and is less than the default capacity, it is
used instead.

Prints a loading message and the resulting mag state when `verbose` is on.
Returns the object for chaining.

lib/Acme/Shotgun.pm  view on Meta::CPAN


    my $gun = Acme::Shotgun->new(
        type    => 'double',  # 'double' (default) or 'pump'
        load    => 'bird',    # 'bird' (default), 'buck', or 'slug'
        shots   => undef,     # optional: cap the number of rounds loaded
        quiet   => 0,         # suppress all output
        debug   => 0,         # dry-run mode, no file modifications
        verbose => 1,         # verbose output (disabled automatically if quiet)
    );

Dies with an error if an invalid C<type> or C<load> value is given.

=head2 reload()

Loads the magazine for the current shotgun type and ammunition. Default
capacity is 2 rounds for C<double> and 5 rounds for C<pump>. If C<shots>
was set in the constructor and is less than the default capacity, it is
used instead.

Prints a loading message and the resulting mag state when C<verbose> is on.
Returns the object for chaining.

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

    $gun = quiet_gun(type => 'pump', shots => 99);
    is($gun->{num_rounds}, 5, 'shots cannot exceed pump capacity of 5');
};

## Validation

subtest 'invalid type dies' => sub {
    dies_ok { Acme::Shotgun->new(type => 'bazooka', quiet => 1) }
        'invalid type dies';

    like($@, qr/Invalid shotgun type/, 'error mentions invalid shotgun type');
};

subtest 'invalid load dies' => sub {
    dies_ok { Acme::Shotgun->new(load => 'rubber', quiet => 1) }
        'invalid load dies';

    like($@, qr/Invalid ammo type/, 'error mentions invalid ammo type');
};

## Reload

subtest 'reload restores round count' => sub {
    my $gun = quiet_gun();
    $gun->{num_rounds} = 0;
    $gun->reload();
    is($gun->{num_rounds}, 2, 'reload restores double to 2 rounds');
};

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

    my $gun = quiet_gun();
    my $ret = $gun->check();
    is($ret, $gun, 'check returns $self');
};

## 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 {

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


    $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 1.925 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )