moving some tests around, increasing the coverage and generally improving the test...
[gitmo/Class-MOP.git] / t / 020_attribute.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 52;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP');
11     use_ok('Class::MOP::Attribute');
12 }
13
14 {
15     my $attr = Class::MOP::Attribute->new('$foo');
16     isa_ok($attr, 'Class::MOP::Attribute');
17
18     is($attr->name, '$foo', '... $attr->name == $foo');
19     ok($attr->has_init_arg, '... $attr does have an init_arg');
20     is($attr->init_arg, '$foo', '... $attr init_arg is the name');        
21     
22     ok(!$attr->has_accessor, '... $attr does not have an accessor');
23     ok(!$attr->has_reader, '... $attr does not have an reader');
24     ok(!$attr->has_writer, '... $attr does not have an writer');
25     ok(!$attr->has_default, '... $attr does not have an default');  
26     
27     my $attr_clone = $attr->clone();
28     isa_ok($attr_clone, 'Class::MOP::Attribute');
29     isnt($attr, $attr_clone, '... but they are different instances');
30     
31     is_deeply($attr, $attr_clone, '... but they are the same inside');
32 }
33
34 {
35     my $attr = Class::MOP::Attribute->new('$foo', (
36         init_arg => '-foo',
37         default  => 'BAR'
38     ));
39     isa_ok($attr, 'Class::MOP::Attribute');
40
41     is($attr->name, '$foo', '... $attr->name == $foo');
42     
43     ok($attr->has_init_arg, '... $attr does have an init_arg');
44     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
45     ok($attr->has_default, '... $attr does have an default');    
46     is($attr->default, 'BAR', '... $attr->default == BAR');
47     
48     ok(!$attr->has_accessor, '... $attr does not have an accessor');
49     ok(!$attr->has_reader, '... $attr does not have an reader');
50     ok(!$attr->has_writer, '... $attr does not have an writer');   
51     
52     my $attr_clone = $attr->clone();
53     isa_ok($attr_clone, 'Class::MOP::Attribute');
54     isnt($attr, $attr_clone, '... but they are different instances');
55     
56     is_deeply($attr, $attr_clone, '... but they are the same inside');                
57 }
58
59 {
60     my $attr = Class::MOP::Attribute->new('$foo', (
61         accessor => 'foo',
62         init_arg => '-foo',
63         default  => 'BAR'
64     ));
65     isa_ok($attr, 'Class::MOP::Attribute');
66
67     is($attr->name, '$foo', '... $attr->name == $foo');
68     
69     ok($attr->has_init_arg, '... $attr does have an init_arg');
70     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
71     ok($attr->has_default, '... $attr does have an default');    
72     is($attr->default, 'BAR', '... $attr->default == BAR');
73
74     ok($attr->has_accessor, '... $attr does have an accessor');    
75     is($attr->accessor, 'foo', '... $attr->accessor == foo');
76     
77     ok(!$attr->has_reader, '... $attr does not have an reader');
78     ok(!$attr->has_writer, '... $attr does not have an writer');   
79     
80     my $attr_clone = $attr->clone();
81     isa_ok($attr_clone, 'Class::MOP::Attribute');
82     isnt($attr, $attr_clone, '... but they are different instances');
83     
84     is_deeply($attr, $attr_clone, '... but they are the same inside');                
85 }
86
87 {
88     my $attr = Class::MOP::Attribute->new('$foo', (
89         reader   => 'get_foo',
90         writer   => 'set_foo',        
91         init_arg => '-foo',
92         default  => 'BAR'
93     ));
94     isa_ok($attr, 'Class::MOP::Attribute');
95
96     is($attr->name, '$foo', '... $attr->name == $foo');
97     
98     ok($attr->has_init_arg, '... $attr does have an init_arg');
99     is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
100     ok($attr->has_default, '... $attr does have an default');    
101     is($attr->default, 'BAR', '... $attr->default == BAR');
102
103     ok($attr->has_reader, '... $attr does have an reader');
104     is($attr->reader, 'get_foo', '... $attr->reader == get_foo');    
105     ok($attr->has_writer, '... $attr does have an writer');
106     is($attr->writer, 'set_foo', '... $attr->writer == set_foo');    
107
108     ok(!$attr->has_accessor, '... $attr does not have an accessor'); 
109     
110     my $attr_clone = $attr->clone();
111     isa_ok($attr_clone, 'Class::MOP::Attribute');
112     isnt($attr, $attr_clone, '... but they are different instances');
113     
114     is_deeply($attr, $attr_clone, '... but they are the same inside');       
115 }