Move t/*/t into t/001_mouse
[gitmo/Mouse.git] / t / 001_mouse / 019-handles.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
912fa381 4use Test::More tests => 28;
7b133c92 5use Test::Exception;
c3398f5b 6
7do {
8 package Person;
9
10 sub new {
11 my $class = shift;
12 my %args = @_;
13
14 bless \%args, $class;
15 }
16
17 sub name { $_[0]->{name} = $_[1] if @_ > 1; $_[0]->{name} }
18 sub age { $_[0]->{age} = $_[1] if @_ > 1; $_[0]->{age} }
19
20 package Class;
21 use Mouse;
22
23 has person => (
24 is => 'rw',
25 lazy => 1,
26 default => sub { Person->new(age => 37, name => "Chuck") },
27 predicate => 'has_person',
28 handles => {
29 person_name => 'name',
30 person_age => 'age',
31 },
32 );
33
34 has me => (
35 is => 'rw',
36 default => sub { Person->new(age => 21, name => "Shawn") },
37 predicate => 'quid',
38 handles => [qw/name age/],
39 );
40
6fbbcaf4 41 TODO: {
1ea948d1 42 local our $TODO = "Mouse lacks this";
6fbbcaf4 43 eval {
44 has error => (
45 handles => "string",
46 );
47 };
1ea948d1 48 ::ok(!$@, "handles => role");
6fbbcaf4 49 }
c3398f5b 50
6fbbcaf4 51 TODO: {
1ea948d1 52 local our $TODO = "Mouse lacks this";
6fbbcaf4 53 eval {
54 has error2 => (
55 handles => \"ref_to_string",
56 );
57 };
1ea948d1 58 ::ok(!$@, "handles => \\str");
6fbbcaf4 59 }
c3398f5b 60
6fbbcaf4 61 TODO: {
1ea948d1 62 local our $TODO = "Mouse lacks this";
6fbbcaf4 63 eval {
64 has error3 => (
65 handles => qr/regex/,
66 );
67 };
1ea948d1 68 ::ok(!$@, "handles => qr/re/");
6fbbcaf4 69 }
c3398f5b 70
6fbbcaf4 71 TODO: {
1ea948d1 72 local our $TODO = "Mouse lacks this";
6fbbcaf4 73 eval {
74 has error4 => (
75 handles => sub { "code" },
76 );
77 };
1ea948d1 78 ::ok(!$@, "handles => sub { code }");
6fbbcaf4 79 }
c3398f5b 80};
81
82can_ok(Class => qw(person has_person person_name person_age name age quid));
83
84my $object = Class->new;
85ok(!$object->has_person, "don't have a person yet");
86$object->person_name("Todd");
87ok($object->has_person, "calling person_name instantiated person");
88ok($object->person, "we really do have a person");
89
90is($object->person_name, "Todd", "handles method");
91is($object->person->name, "Todd", "traditional lookup");
92is($object->person_age, 37, "handles method");
93is($object->person->age, 37, "traditional lookup");
94
95my $object2 = Class->new(person => Person->new(name => "Philbert"));
96ok($object2->has_person, "we have a person from the constructor");
97is($object2->person_name, "Philbert", "handles method");
98is($object2->person->name, "Philbert", "traditional lookup");
99is($object2->person_age, undef, "no age because we didn't use the default");
100is($object2->person->age, undef, "no age because we didn't use the default");
101
102
103ok($object->quid, "we have a Shawn");
104is($object->name, "Shawn", "name handle");
105is($object->age, 21, "age handle");
106is($object->me->name, "Shawn", "me->name");
107is($object->me->age, 21, "me->age");
108
109is_deeply(
110 $object->meta->get_attribute('me')->handles,
c3cc3642 111 [ 'name', 'age' ],
c3398f5b 112 "correct handles layout for 'me'",
113);
114
115is_deeply(
116 $object->meta->get_attribute('person')->handles,
117 { person_name => 'name', person_age => 'age' },
118 "correct handles layout for 'person'",
119);
120
912fa381 121
122{
284ca75b 123 local $TODO = "failed on some environment, but I don't know why it happens (gfx)";
912fa381 124 throws_ok{
125 $object->person(undef);
126 $object->person_name();
127 } qr/Cannot delegate person_name to name because the value of person is not defined/;
128
129 throws_ok{
130 $object->person([]);
131 $object->person_age();
132 } qr/Cannot delegate person_age to age because the value of person is not an object/;
133}
134
135eval{
7b133c92 136 $object->person(undef);
137 $object->person_name();
912fa381 138};
139like $@, qr/Cannot delegate person_name to name because the value of person is not defined/;
7b133c92 140
912fa381 141eval{
7b133c92 142 $object->person([]);
143 $object->person_age();
912fa381 144};
145like $@, qr/Cannot delegate person_age to age because the value of person is not an object/;
146
7b133c92 147