no ref in the defaults
[gitmo/Class-MOP.git] / t / 021_attribute_errors_and_edge_cases.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 23;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP');
11     use_ok('Class::MOP::Attribute');
12 }
13
14 # most values are static
15
16 {
17     dies_ok {
18         Class::MOP::Attribute->new('$test' => (
19             default => qr/hello (.*)/
20         ));
21     } '... no refs for defaults';
22     
23     dies_ok {
24         Class::MOP::Attribute->new('$test' => (
25             default => []
26         ));
27     } '... no refs for defaults';    
28     
29     dies_ok {
30         Class::MOP::Attribute->new('$test' => (
31             default => {}
32         ));
33     } '... no refs for defaults';    
34     
35     
36     dies_ok {
37         Class::MOP::Attribute->new('$test' => (
38             default => \(my $var)
39         ));
40     } '... no refs for defaults';    
41
42     dies_ok {
43         Class::MOP::Attribute->new('$test' => (
44             default => bless {} => 'Foo'
45         ));
46     } '... no refs for defaults';
47
48 }
49
50 { # bad construtor args
51     dies_ok {
52         Class::MOP::Attribute->new();
53     } '... no name argument';
54
55     dies_ok {
56         Class::MOP::Attribute->new('');
57     } '... bad name argument';
58
59     dies_ok {
60         Class::MOP::Attribute->new(0);
61     } '... bad name argument';
62 }
63
64 {
65     my $attr = Class::MOP::Attribute->new('$test');    
66     dies_ok {
67         $attr->attach_to_class();
68     } '... attach_to_class died as expected';
69     
70     dies_ok {
71         $attr->attach_to_class('Fail');
72     } '... attach_to_class died as expected';    
73     
74     dies_ok {
75         $attr->attach_to_class(bless {} => 'Fail');
76     } '... attach_to_class died as expected';    
77 }
78
79 {
80     my $attr = Class::MOP::Attribute->new('$test' => (
81         reader => [ 'whoops, this wont work' ]
82     ));
83     
84     $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
85
86     dies_ok {
87         $attr->install_accessors;
88     } '... bad reader format';  
89 }
90
91 {
92     my $attr = Class::MOP::Attribute->new('$test');
93
94     dies_ok {
95         $attr->process_accessors('fail', 'my_failing_sub');
96     } '... cannot find "fail" type generator';
97 }
98
99
100 {
101     {
102         package My::Attribute;
103         our @ISA = ('Class::MOP::Attribute');
104         sub generate_reader_method { eval { die } }
105     }
106
107     my $attr = My::Attribute->new('$test' => (
108         reader => 'test'
109     ));
110     
111     dies_ok {
112         $attr->install_accessors;
113     } '... failed to generate accessors correctly';    
114 }
115
116 {
117     my $attr = Class::MOP::Attribute->new('$test' => (
118         predicate => 'has_test'
119     ));
120     
121     my $Bar = Class::MOP::Class->create('Bar' => '0.01');
122     isa_ok($Bar, 'Class::MOP::Class');
123     
124     $Bar->add_attribute($attr);
125     
126     can_ok('Bar', 'has_test');
127     
128     is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');    
129     
130     ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');    
131 }
132
133
134 {
135     # NOTE:
136     # the next three tests once tested that 
137     # the code would fail, but we lifted the 
138     # restriction so you can have an accessor 
139     # along with a reader/writer pair (I mean 
140     # why not really). So now they test that 
141     # it works, which is kinda silly, but it 
142     # tests the API change, so I keep it.
143
144     lives_ok {
145         Class::MOP::Attribute->new('$foo', (
146             accessor => 'foo',
147             reader   => 'get_foo',
148         ));
149     } '... can create accessors with reader/writers';
150
151     lives_ok {
152         Class::MOP::Attribute->new('$foo', (
153             accessor => 'foo',
154             writer   => 'set_foo',
155         ));
156     } '... can create accessors with reader/writers';
157
158     lives_ok {
159         Class::MOP::Attribute->new('$foo', (
160             accessor => 'foo',
161             reader   => 'get_foo',        
162             writer   => 'set_foo',
163         ));
164     } '... can create accessors with reader/writers';
165 }