Class::MOP - getting there
[gitmo/Class-MOP.git] / t / 020_attribute.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More no_plan => 1;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP::Attribute');
11 }
12
13 {
14     my $attr = Class::MOP::Attribute->new('$foo');
15     isa_ok($attr, 'Class::MOP::Attribute');
16
17     is($attr->name, '$foo', '... $attr->name == $foo');
18     
19     ok(!$attr->has_accessor, '... $attr does not have an accessor');
20     ok(!$attr->has_reader, '... $attr does not have an reader');
21     ok(!$attr->has_writer, '... $attr does not have an writer');
22     ok(!$attr->has_init_arg, '... $attr does not have an init_arg');
23     ok(!$attr->has_default, '... $attr does not have an default');                
24 }
25
26 {
27     my $attr = Class::MOP::Attribute->new('$foo', (
28         init_arg => '-foo',
29         default  => 'BAR'
30     ));
31     isa_ok($attr, 'Class::MOP::Attribute');
32
33     is($attr->name, '$foo', '... $attr->name == $foo');
34     
35     ok($attr->has_init_arg, '... $attr does have an init_arg');
36     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
37     ok($attr->has_default, '... $attr does have an default');    
38     is($attr->default, 'BAR', '... $attr->default == BAR');
39     
40     ok(!$attr->has_accessor, '... $attr does not have an accessor');
41     ok(!$attr->has_reader, '... $attr does not have an reader');
42     ok(!$attr->has_writer, '... $attr does not have an writer');               
43 }
44
45 {
46     my $attr = Class::MOP::Attribute->new('$foo', (
47         accessor => 'foo',
48         init_arg => '-foo',
49         default  => 'BAR'
50     ));
51     isa_ok($attr, 'Class::MOP::Attribute');
52
53     is($attr->name, '$foo', '... $attr->name == $foo');
54     
55     ok($attr->has_init_arg, '... $attr does have an init_arg');
56     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
57     ok($attr->has_default, '... $attr does have an default');    
58     is($attr->default, 'BAR', '... $attr->default == BAR');
59
60     ok($attr->has_accessor, '... $attr does have an accessor');    
61     is($attr->accessor, 'foo', '... $attr->accessor == foo');
62     
63     ok(!$attr->has_reader, '... $attr does not have an reader');
64     ok(!$attr->has_writer, '... $attr does not have an writer');               
65 }
66
67 {
68     my $attr = Class::MOP::Attribute->new('$foo', (
69         reader   => 'get_foo',
70         writer   => 'set_foo',        
71         init_arg => '-foo',
72         default  => 'BAR'
73     ));
74     isa_ok($attr, 'Class::MOP::Attribute');
75
76     is($attr->name, '$foo', '... $attr->name == $foo');
77     
78     ok($attr->has_init_arg, '... $attr does have an init_arg');
79     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
80     ok($attr->has_default, '... $attr does have an default');    
81     is($attr->default, 'BAR', '... $attr->default == BAR');
82
83     ok($attr->has_reader, '... $attr does have an reader');
84     is($attr->reader, 'get_foo', '... $attr->reader == get_foo');    
85     ok($attr->has_writer, '... $attr does have an writer');
86     is($attr->writer, 'set_foo', '... $attr->writer == set_foo');    
87
88     ok(!$attr->has_accessor, '... $attr does not have an accessor');    
89 }
90
91 dies_ok {
92     my $attr = Class::MOP::Attribute->new('$foo', (
93         accessor => 'foo',
94         reader   => 'get_foo',
95     ));
96 } '... cannot create accessors with reader/writers';
97
98 dies_ok {
99     my $attr = Class::MOP::Attribute->new('$foo', (
100         accessor => 'foo',
101         writer   => 'set_foo',
102     ));
103 } '... cannot create accessors with reader/writers';
104
105 dies_ok {
106     my $attr = Class::MOP::Attribute->new('$foo', (
107         accessor => 'foo',
108         reader   => 'get_foo',        
109         writer   => 'set_foo',
110     ));
111 } '... cannot create accessors with reader/writers';
112
113
114 {
115     my $meta = Class::MOP::Attribute->meta();
116     isa_ok($meta, 'Class::MOP::Class');
117     
118     foreach my $method_name (qw(
119         meta 
120         new
121         has_accessor accessor
122         has_writer   writer
123         has_reader   reader
124         has_init_arg init_arg
125         has_default  default
126         install_accessors
127         remove_accessors
128         )) {
129         ok($meta->has_method($method_name), '... Class::MOP::Attribute->has_method(' . $method_name . ')');
130     }
131     
132     
133 }