X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=t%2F001_mouse%2F019-handles.t;h=3dbf3149382fea1d1afab4b225d0ddaf02ec81c8;hp=32f865bf87010fc11cc7247d22c585fc6843ffbe;hb=cb80a70a3d6101e0bc8f6576765a2d6c588d2ecd;hpb=3aa1e09a9e63a374c0057a91eed32d41287d2ee0 diff --git a/t/001_mouse/019-handles.t b/t/001_mouse/019-handles.t index 32f865b..3dbf314 100644 --- a/t/001_mouse/019-handles.t +++ b/t/001_mouse/019-handles.t @@ -1,22 +1,21 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 24; +use Test::More; use Test::Exception; do { package Person; + use Mouse; - sub new { - my $class = shift; - my %args = @_; + has name => (is => 'rw'); + has age => (is => 'rw'); - bless \%args, $class; + sub make_string { + my($self, $template) = @_; + return sprintf $template, $self->name; } - sub name { $_[0]->{name} = $_[1] if @_ > 1; $_[0]->{name} } - sub age { $_[0]->{age} = $_[1] if @_ > 1; $_[0]->{age} } - package Class; use Mouse; @@ -28,15 +27,18 @@ do { handles => { person_name => 'name', person_age => 'age', + person_hello => [make_string => 'Hello, %s'], }, ); has me => ( - is => 'rw', + is => 'rw', + isa => 'Person', default => sub { Person->new(age => 21, name => "Shawn") }, predicate => 'quid', handles => [qw/name age/], ); + }; can_ok(Class => qw(person has_person person_name person_age name age quid)); @@ -51,6 +53,7 @@ is($object->person_name, "Todd", "handles method"); is($object->person->name, "Todd", "traditional lookup"); is($object->person_age, 37, "handles method"); is($object->person->age, 37, "traditional lookup"); +is($object->person_hello, 'Hello, Todd', 'curring'); my $object2 = Class->new(person => Person->new(name => "Philbert")); ok($object2->has_person, "we have a person from the constructor"); @@ -58,7 +61,7 @@ is($object2->person_name, "Philbert", "handles method"); is($object2->person->name, "Philbert", "traditional lookup"); is($object2->person_age, undef, "no age because we didn't use the default"); is($object2->person->age, undef, "no age because we didn't use the default"); - +is($object2->person_hello, 'Hello, Philbert', 'currying'); ok($object->quid, "we have a Shawn"); is($object->name, "Shawn", "name handle"); @@ -74,33 +77,30 @@ is_deeply( is_deeply( $object->meta->get_attribute('person')->handles, - { person_name => 'name', person_age => 'age' }, + { person_name => 'name', person_age => 'age', person_hello => [make_string => 'Hello, %s']}, "correct handles layout for 'person'", ); +throws_ok{ + $object->person(undef); + $object->person_name(); +} qr/Cannot delegate person_name to name because the value of person is not defined/; -{ - throws_ok{ - $object->person(undef); - $object->person_name(); - } qr/Cannot delegate person_name to name because the value of person is not defined/; - - throws_ok{ - $object->person([]); - $object->person_age(); - } qr/Cannot delegate person_age to age because the value of person is not an object/; -} +throws_ok{ + $object->person([]); + $object->person_age(); +} qr/Cannot delegate person_age to age because the value of person is not an object/; -eval{ +throws_ok{ $object->person(undef); $object->person_name(); -}; -like $@, qr/Cannot delegate person_name to name because the value of person is not defined/; +} qr/Cannot delegate person_name to name because the value of person is not defined/; -eval{ +throws_ok{ $object->person([]); $object->person_age(); -}; -like $@, qr/Cannot delegate person_age to age because the value of person is not an object/; +} qr/Cannot delegate person_age to age because the value of person is not an object/; + +done_testing;