File: 021_attribute_errors_and_edge_cases.t

package info (click to toggle)
libclass-mop-perl 1.04-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,244 kB
  • ctags: 1,272
  • sloc: perl: 5,192; ansic: 241; makefile: 2
file content (205 lines) | stat: -rw-r--r-- 4,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use strict;
use warnings;

use Test::More;
use Test::Exception;

use Class::MOP;
use Class::MOP::Attribute;

# most values are static

{
    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            default => qr/hello (.*)/
        ));
    } '... no refs for defaults';

    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            default => []
        ));
    } '... no refs for defaults';

    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            default => {}
        ));
    } '... no refs for defaults';


    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            default => \(my $var)
        ));
    } '... no refs for defaults';

    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            default => bless {} => 'Foo'
        ));
    } '... no refs for defaults';

}

{
    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            builder => qr/hello (.*)/
        ));
    } '... no refs for builders';

    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            builder => []
        ));
    } '... no refs for builders';

    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            builder => {}
        ));
    } '... no refs for builders';


    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            builder => \(my $var)
        ));
    } '... no refs for builders';

    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            builder => bless {} => 'Foo'
        ));
    } '... no refs for builders';

    dies_ok {
        Class::MOP::Attribute->new('$test' => (
            builder => 'Foo', default => 'Foo'
        ));
    } '... no default AND builder';

}


{ # bad construtor args
    dies_ok {
        Class::MOP::Attribute->new();
    } '... no name argument';

    # These are no longer errors
    lives_ok {
        Class::MOP::Attribute->new('');
    } '... bad name argument';

    lives_ok {
        Class::MOP::Attribute->new(0);
    } '... bad name argument';
}

{
    my $attr = Class::MOP::Attribute->new('$test');
    dies_ok {
        $attr->attach_to_class();
    } '... attach_to_class died as expected';

    dies_ok {
        $attr->attach_to_class('Fail');
    } '... attach_to_class died as expected';

    dies_ok {
        $attr->attach_to_class(bless {} => 'Fail');
    } '... attach_to_class died as expected';
}

{
    my $attr = Class::MOP::Attribute->new('$test' => (
        reader => [ 'whoops, this wont work' ]
    ));

    $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));

    dies_ok {
        $attr->install_accessors;
    } '... bad reader format';
}

{
    my $attr = Class::MOP::Attribute->new('$test');

    dies_ok {
        $attr->_process_accessors('fail', 'my_failing_sub');
    } '... cannot find "fail" type generator';
}


{
    {
        package My::Attribute;
        our @ISA = ('Class::MOP::Attribute');
        sub generate_reader_method { eval { die } }
    }

    my $attr = My::Attribute->new('$test' => (
        reader => 'test'
    ));

    dies_ok {
        $attr->install_accessors;
    } '... failed to generate accessors correctly';
}

{
    my $attr = Class::MOP::Attribute->new('$test' => (
        predicate => 'has_test'
    ));

    my $Bar = Class::MOP::Class->create('Bar');
    isa_ok($Bar, 'Class::MOP::Class');

    $Bar->add_attribute($attr);

    can_ok('Bar', 'has_test');

    is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');

    ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
}


{
    # NOTE:
    # the next three tests once tested that
    # the code would fail, but we lifted the
    # restriction so you can have an accessor
    # along with a reader/writer pair (I mean
    # why not really). So now they test that
    # it works, which is kinda silly, but it
    # tests the API change, so I keep it.

    lives_ok {
        Class::MOP::Attribute->new('$foo', (
            accessor => 'foo',
            reader   => 'get_foo',
        ));
    } '... can create accessors with reader/writers';

    lives_ok {
        Class::MOP::Attribute->new('$foo', (
            accessor => 'foo',
            writer   => 'set_foo',
        ));
    } '... can create accessors with reader/writers';

    lives_ok {
        Class::MOP::Attribute->new('$foo', (
            accessor => 'foo',
            reader   => 'get_foo',
            writer   => 'set_foo',
        ));
    } '... can create accessors with reader/writers';
}

done_testing;