Box-Limited
view release on metacpan or search on metacpan
t/box-limited.t view on Meta::CPAN
#!/usr/bin/env perl
use Test::Roo;
use experimental qw(signatures);
use List::Util qw(sum0);
use Test::Differences qw(eq_or_diff);
use Test::Fatal qw(exception);
use Box::Limited;
has class_name => (
is => 'ro',
default => sub {'Box::Limited'},
);
has box => (
is => 'rw',
lazy => 1,
builder => '_build_box',
);
sub _build_box ($self) {
return $self->class_name->new(
size => 5,
max_weight => 10,
weight_function => sub (@items) {
return sum0(@items);
},
);
}
before each_test => sub ($self, @) {
# Rebuild testing box before every test
$self->box($self->_build_box);
};
test construction => sub ($self) {
my $box = $self->box;
ok($box, 'Box created');
is($box->size, 5, 'Box size is as expected');
is($box->max_weight, 10, 'Box max_weight is as expected');
is(ref($box->weight_function), 'CODE',
'Box weight_function is a coderef');
ok($box->is_empty, 'Box is initially empty');
is($box->items_count, 0, '...and contains 0 items');
};
test can_add => sub ($self) {
my $box = $self->box;
ok($box->can_add(9),
'Element with weight smaller than max_weight can be added');
ok($box->can_add(10),
'Element with weight equal to max_weight can be added');
ok(!$box->can_add(11),
'Element with weight higher than max_weight cannot be added');
};
test add => sub ($self) {
my $box = $self->box;
ok(exception { $box->add(11) },
'Attempt to add too big item raises an exception');
ok($box->add(9), 'Smaller item was added though');
is($box->items_count, 1, 'Items count was updated');
ok(!$box->is_empty, 'Box is not empty anymore');
( run in 1.489 second using v1.01-cache-2.11-cpan-99c4e6809bf )