Commit | Line | Data |
36062241 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
cfdb93c6 |
4 | use Test::More; |
5 | use Test::Mouse; |
36062241 |
6 | do { |
7 | package Class; |
8 | use Mouse; |
9 | |
10 | has class => ( |
11 | is => 'rw', |
12 | isa => 'Bool', |
13 | ); |
14 | |
15 | package Child; |
16 | use Mouse; |
17 | extends 'Class'; |
18 | |
19 | has child => ( |
20 | is => 'rw', |
21 | isa => 'Bool', |
22 | ); |
36062241 |
23 | |
cfdb93c6 |
24 | package CA; |
25 | use Mouse; |
26 | extends qw(Class); |
27 | has ca => (is => 'rw'); |
28 | package CB; |
29 | use Mouse; |
30 | extends qw(Class); |
31 | has cb => (is => 'rw'); |
32 | package CC; |
33 | use Mouse; |
34 | extends qw(CB CA); |
35 | has cc => (is => 'rw'); |
36 | }; |
26f0aa17 |
37 | with_immutable { |
cfdb93c6 |
38 | my $obj = Child->new(class => 1, child => 1); |
39 | ok($obj->child, "local attribute set in constructor"); |
40 | ok($obj->class, "inherited attribute set in constructor"); |
41 | |
42 | is_deeply([sort(Child->meta->get_all_attributes)], [sort( |
43 | Child->meta->get_attribute('child'), |
44 | Class->meta->get_attribute('class'), |
45 | )], "correct get_all_attributes"); |
46 | |
47 | is_deeply([sort(CC->meta->get_all_attributes)], [sort( |
48 | CC->meta->get_attribute('cc'), |
49 | CB->meta->get_attribute('cb'), |
50 | CA->meta->get_attribute('ca'), |
51 | Class->meta->get_attribute('class'), |
52 | )], "correct get_all_attributes"); |
26f0aa17 |
53 | } 'Class', 'CA', 'CB', 'CC'; |
28d26949 |
54 | |
069668c4 |
55 | do { |
56 | package Foo; |
57 | use Mouse; |
58 | |
59 | has attr => ( |
60 | is => 'ro', |
61 | default => 'Foo', |
62 | ); |
63 | |
64 | package Bar; |
65 | use Mouse; |
66 | extends 'Foo'; |
67 | |
68 | has attr => ( |
69 | is => 'rw', |
70 | ); |
71 | }; |
72 | |
cfdb93c6 |
73 | with_immutable { |
74 | my $foo = Foo->new; |
75 | is($foo->attr, 'Foo', 'subclass does not affect parent attr'); |
76 | |
77 | my $bar = Bar->new; |
78 | is($bar->attr, undef, 'new attribute does not have the new default'); |
069668c4 |
79 | |
cfdb93c6 |
80 | is(Foo->meta->get_attribute('attr')->default, 'Foo'); |
81 | is(Foo->meta->get_attribute('attr')->_is_metadata, 'ro'); |
069668c4 |
82 | |
cfdb93c6 |
83 | is(Bar->meta->get_attribute('attr')->default, undef); |
84 | is(Bar->meta->get_attribute('attr')->_is_metadata, 'rw'); |
069668c4 |
85 | |
cfdb93c6 |
86 | is_deeply([Foo->meta->get_all_attributes], [ |
87 | Foo->meta->get_attribute('attr'), |
88 | ], "correct get_all_attributes"); |
069668c4 |
89 | |
cfdb93c6 |
90 | is_deeply([Bar->meta->get_all_attributes], [ |
91 | Bar->meta->get_attribute('attr'), |
92 | ], "correct get_all_attributes"); |
26f0aa17 |
93 | } 'Foo', 'Bar'; |
069668c4 |
94 | |
cfdb93c6 |
95 | done_testing; |
069668c4 |
96 | |