X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F200_examples%2F003_example.t;h=a61ba351624ef900449f15e1796c233e388dc8ea;hb=d4e538d9bf46d1c14d2ecfd36ac35ed541ae7ee6;hp=3c3e46398eaf74afc801abc587f81567ab4580b2;hpb=e59a5c292a333cac504b65ebd4bba20b5e98d796;p=gitmo%2FMoose.git diff --git a/t/200_examples/003_example.t b/t/200_examples/003_example.t index 3c3e463..a61ba35 100644 --- a/t/200_examples/003_example.t +++ b/t/200_examples/003_example.t @@ -3,14 +3,9 @@ use strict; use warnings; -use Test::More tests => 32; +use Test::More; use Test::Exception; -BEGIN { - use_ok('Moose'); - use_ok('Moose::Role'); -} - sub U { my $f = shift; sub { $f->($f, @_) }; @@ -24,36 +19,36 @@ sub Y { { package List; use Moose::Role; - + has '_list' => ( is => 'ro', - isa => 'ArrayRef', + isa => 'ArrayRef', init_arg => '::', default => sub { [] } ); - + sub head { (shift)->_list->[0] } sub tail { my $self = shift; - $self->new( - '::' => [ - @{$self->_list}[1 .. $#{$self->_list}] + (ref $self)->new( + '::' => [ + @{$self->_list}[1 .. $#{$self->_list}] ] ); - } - + } + sub print { join ", " => @{$_[0]->_list}; - } - + } + package List::Immutable; use Moose::Role; - + requires 'head'; - requires 'tail'; - + requires 'tail'; + sub is_empty { not defined ($_[0]->head) } - + sub length { my $self = shift; (::Y(sub { @@ -65,38 +60,38 @@ sub Y { } }))->($self, 0); } - + sub apply { my ($self, $function) = @_; (::Y(sub { my $redo = shift; sub { my ($list, $func, $acc) = @_; - return $list->new('::' => $acc) + return (ref $list)->new('::' => $acc) if $list->is_empty; $redo->( - $list->tail, + $list->tail, $func, [ @{$acc}, $func->($list->head) ] ); } - }))->($self, $function, []); + }))->($self, $function, []); } - + package My::List1; use Moose; - + ::lives_ok { with 'List', 'List::Immutable'; } '... successfully composed roles together'; - + package My::List2; - use Moose; - + use Moose; + ::lives_ok { with 'List::Immutable', 'List'; - } '... successfully composed roles together'; - + } '... successfully composed roles together'; + } { @@ -107,7 +102,7 @@ sub Y { ok($coll->does('List::Immutable'), '... $coll does List::Immutable'); ok($coll->is_empty, '... we have an empty collection'); - is($coll->length, 0, '... we have a length of 1 for the collection'); + is($coll->length, 0, '... we have a length of 1 for the collection'); } { @@ -118,7 +113,7 @@ sub Y { ok($coll->does('List::Immutable'), '... $coll does List::Immutable'); ok($coll->is_empty, '... we have an empty collection'); - is($coll->length, 0, '... we have a length of 1 for the collection'); + is($coll->length, 0, '... we have a length of 1 for the collection'); } { @@ -129,15 +124,15 @@ sub Y { ok($coll->does('List::Immutable'), '... $coll does List::Immutable'); ok(!$coll->is_empty, '... we do not have an empty collection'); - is($coll->length, 10, '... we have a length of 10 for the collection'); - + is($coll->length, 10, '... we have a length of 10 for the collection'); + is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value'); my $coll2 = $coll->apply(sub { $_[0] * $_[0] }); isa_ok($coll2, 'My::List1'); - - is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same'); - is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed'); + + is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same'); + is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed'); } { @@ -148,21 +143,15 @@ sub Y { ok($coll->does('List::Immutable'), '... $coll does List::Immutable'); ok(!$coll->is_empty, '... we do not have an empty collection'); - is($coll->length, 10, '... we have a length of 10 for the collection'); - + is($coll->length, 10, '... we have a length of 10 for the collection'); + is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value'); my $coll2 = $coll->apply(sub { $_[0] * $_[0] }); isa_ok($coll2, 'My::List2'); - - is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same'); - is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed'); -} - - - - - - + is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same'); + is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed'); +} +done_testing;