Created
July 24, 2014 09:24
-
-
Save cybersiddhu/66f08564932973e55bc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ArrayPackage; | |
=pod | |
=head1 NAME | |
ArrayPackage -- A package class that uses perl array to store items | |
=cut | |
use Moose; | |
use Function::Parameters qw/:strict/; | |
=pod | |
=head1 Attributes | |
=cut | |
has name => ( | |
is => 'rw', | |
isa => 'Str', | |
reader => 'get_name' | |
); | |
has height => ( | |
is => 'rw', | |
isa => 'Int', | |
reader => 'get_height' | |
); | |
has width => ( | |
is => 'rw', | |
isa => 'Int', | |
reader => 'get_width' | |
); | |
has weight => ( | |
is => 'rw', | |
isa => 'Int', | |
reader => 'get_weight' | |
); | |
has '_items' => ( | |
is => 'rw', | |
isa => 'ArrayRef', | |
auto_deref => 1, | |
lazy => 1, | |
default => sub { return [] } | |
); | |
=pod | |
=head METHODS | |
B<dimension()> | |
Gets the dimension of the package from its height and width | |
=cut | |
method dimension() { | |
return $self->get_height * $self->get_width; | |
} | |
=pod | |
B<get_items()> | |
Gets the items for the package. Returns a list of items as B<Item> objects. | |
=cut | |
method get_items() { | |
return $self->_items; | |
} | |
method add_item($item) { | |
my $all_items = $self->_items; | |
push @$all_items,$item; | |
$self->_items($all_items); | |
} | |
method total_items() { | |
return scalar @{$self->_items}; | |
} | |
1; | |
__END__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package NativePackage; | |
=pod | |
=head1 NAME | |
NativePackage -- A package class that uses Moose native attributes to store items | |
=cut | |
use Moose; | |
use Function::Parameters qw/:strict/; | |
=pod | |
=head1 Attributes | |
=cut | |
has name => ( | |
is => 'rw', | |
isa => 'Str', | |
reader => 'get_name' | |
); | |
has height => ( | |
is => 'rw', | |
isa => 'Int', | |
reader => 'get_height' | |
); | |
has width => ( | |
is => 'rw', | |
isa => 'Int', | |
reader => 'get_width' | |
); | |
has weight => ( | |
is => 'rw', | |
isa => 'Int', | |
reader => 'get_weight' | |
); | |
has '_items' => ( | |
is => 'rw', | |
isa => 'ArrayRef', | |
traits => [qw/Array/], | |
lazy => 1, | |
default => sub { return [] }, | |
handles => { | |
'get_items' => 'elements', | |
'add_item' => 'push', | |
'total_items' => 'count' | |
} | |
); | |
=pod | |
=head1 METHODS | |
B<dimension()> | |
Gets the dimension of the package from its height and width | |
=cut | |
method dimension() { | |
return $self->get_height * $self->get_width; | |
} | |
1; | |
__END__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use Test::More qw/no_plan/; | |
use_ok 'ArrayPackage'; | |
use_ok 'NativePackage'; | |
use_ok 'Item'; | |
my $array_package = ArrayPackage->new( | |
name => 'array', | |
height => 25, | |
width => 52, | |
weight => 100 | |
); | |
isa_ok( $array_package, 'ArrayPackage' ); | |
is( $array_package->dimension, 25 * 52 ); | |
my $item1 | |
= Item->new( name => 'first', shape => 'rectangale', type => 'document' ); | |
my $item2 = Item->new( name => 'second', shape => 'square', type => 'book' ); | |
$array_package->add_item($item1); | |
$array_package->add_item($item2); | |
is($array_package->total_items, 2); | |
isa_ok($_,'Item') for $array_package->get_items; | |
my $native_package = NativePackage->new( | |
name => 'array', | |
height => 25, | |
width => 52, | |
weight => 100 | |
); | |
isa_ok( $native_package, 'NativePackage' ); | |
is( $native_package->dimension, 25 * 52 ); | |
$native_package->add_item($item1); | |
$native_package->add_item($item2); | |
is($native_package->total_items, 2); | |
isa_ok($_,'Item') for $native_package->get_items; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run test with