File: 021_attribute_errors_and_edge_cases.t

package info (click to toggle)
libclass-mop-perl 0.36-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 552 kB
  • ctags: 209
  • sloc: perl: 6,157; makefile: 46
file content (165 lines) | stat: -rw-r--r-- 3,789 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 23;
use Test::Exception;

BEGIN {
    use_ok('Class::MOP');
    use_ok('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';

}

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

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

    dies_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';
}