0.46
[gitmo/Class-MOP.git] / t / 020_attribute.t
CommitLineData
2eb717d5 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
1d68af04 6use Test::More tests => 73;
2eb717d5 7use Test::Exception;
8
9BEGIN {
727919c5 10 use_ok('Class::MOP');
2eb717d5 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');
7b31baf4 19 ok($attr->has_init_arg, '... $attr does have an init_arg');
1d68af04 20 is($attr->init_arg, '$foo', '... $attr init_arg is the name');
21
2eb717d5 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');
1d68af04 25 ok(!$attr->has_default, '... $attr does not have an default');
26 ok(!$attr->has_builder, '... $attr does not have a builder');
27
a27ae83f 28 my $class = Class::MOP::Class->initialize('Foo');
29 isa_ok($class, 'Class::MOP::Class');
1d68af04 30
a27ae83f 31 lives_ok {
32 $attr->attach_to_class($class);
33 } '... attached a class successfully';
1d68af04 34
a27ae83f 35 is($attr->associated_class, $class, '... the class was associated correctly');
1d68af04 36
5659d76e 37 my $attr_clone = $attr->clone();
38 isa_ok($attr_clone, 'Class::MOP::Attribute');
39 isnt($attr, $attr_clone, '... but they are different instances');
1d68af04 40
a27ae83f 41 is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
1d68af04 42 is($attr->associated_class, $class, '... the associated classes are the same though');
43 is($attr_clone->associated_class, $class, '... the associated classes are the same though');
44
5659d76e 45 is_deeply($attr, $attr_clone, '... but they are the same inside');
2eb717d5 46}
47
48{
49 my $attr = Class::MOP::Attribute->new('$foo', (
50 init_arg => '-foo',
51 default => 'BAR'
52 ));
53 isa_ok($attr, 'Class::MOP::Attribute');
54
55 is($attr->name, '$foo', '... $attr->name == $foo');
1d68af04 56
2eb717d5 57 ok($attr->has_init_arg, '... $attr does have an init_arg');
58 is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
1d68af04 59 ok($attr->has_default, '... $attr does have an default');
2eb717d5 60 is($attr->default, 'BAR', '... $attr->default == BAR');
1d68af04 61 ok(!$attr->has_builder, '... $attr does not have a builder');
62
2eb717d5 63 ok(!$attr->has_accessor, '... $attr does not have an accessor');
64 ok(!$attr->has_reader, '... $attr does not have an reader');
1d68af04 65 ok(!$attr->has_writer, '... $attr does not have an writer');
66
5659d76e 67 my $attr_clone = $attr->clone();
68 isa_ok($attr_clone, 'Class::MOP::Attribute');
69 isnt($attr, $attr_clone, '... but they are different instances');
1d68af04 70
a27ae83f 71 is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
1d68af04 72 is($attr->associated_class, undef, '... the associated class is actually undef');
73 is($attr_clone->associated_class, undef, '... the associated class is actually undef');
74
75 is_deeply($attr, $attr_clone, '... but they are the same inside');
2eb717d5 76}
77
78{
79 my $attr = Class::MOP::Attribute->new('$foo', (
80 accessor => 'foo',
81 init_arg => '-foo',
82 default => 'BAR'
83 ));
84 isa_ok($attr, 'Class::MOP::Attribute');
85
86 is($attr->name, '$foo', '... $attr->name == $foo');
1d68af04 87
2eb717d5 88 ok($attr->has_init_arg, '... $attr does have an init_arg');
89 is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
1d68af04 90 ok($attr->has_default, '... $attr does have an default');
2eb717d5 91 is($attr->default, 'BAR', '... $attr->default == BAR');
92
1d68af04 93 ok($attr->has_accessor, '... $attr does have an accessor');
2eb717d5 94 is($attr->accessor, 'foo', '... $attr->accessor == foo');
1d68af04 95
2eb717d5 96 ok(!$attr->has_reader, '... $attr does not have an reader');
1d68af04 97 ok(!$attr->has_writer, '... $attr does not have an writer');
98
5659d76e 99 my $attr_clone = $attr->clone();
100 isa_ok($attr_clone, 'Class::MOP::Attribute');
a740253a 101 isnt($attr, $attr_clone, '... but they are different instances');
1d68af04 102
103 is_deeply($attr, $attr_clone, '... but they are the same inside');
2eb717d5 104}
105
106{
107 my $attr = Class::MOP::Attribute->new('$foo', (
108 reader => 'get_foo',
1d68af04 109 writer => 'set_foo',
2eb717d5 110 init_arg => '-foo',
111 default => 'BAR'
112 ));
113 isa_ok($attr, 'Class::MOP::Attribute');
114
115 is($attr->name, '$foo', '... $attr->name == $foo');
1d68af04 116
2eb717d5 117 ok($attr->has_init_arg, '... $attr does have an init_arg');
118 is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
1d68af04 119 ok($attr->has_default, '... $attr does have an default');
2eb717d5 120 is($attr->default, 'BAR', '... $attr->default == BAR');
121
122 ok($attr->has_reader, '... $attr does have an reader');
1d68af04 123 is($attr->reader, 'get_foo', '... $attr->reader == get_foo');
2eb717d5 124 ok($attr->has_writer, '... $attr does have an writer');
1d68af04 125 is($attr->writer, 'set_foo', '... $attr->writer == set_foo');
126
127 ok(!$attr->has_accessor, '... $attr does not have an accessor');
2eb717d5 128
5659d76e 129 my $attr_clone = $attr->clone();
130 isa_ok($attr_clone, 'Class::MOP::Attribute');
a740253a 131 isnt($attr, $attr_clone, '... but they are different instances');
1d68af04 132
133 is_deeply($attr, $attr_clone, '... but they are the same inside');
2eb717d5 134}
22286063 135
136{
137 my $attr = Class::MOP::Attribute->new('$foo');
138 isa_ok($attr, 'Class::MOP::Attribute');
1d68af04 139
22286063 140 my $attr_clone = $attr->clone('name' => '$bar');
141 isa_ok($attr_clone, 'Class::MOP::Attribute');
142 isnt($attr, $attr_clone, '... but they are different instances');
1d68af04 143
22286063 144 isnt($attr->name, $attr_clone->name, '... we changes the name parameter');
1d68af04 145
22286063 146 is($attr->name, '$foo', '... $attr->name == $foo');
1d68af04 147 is($attr_clone->name, '$bar', '... $attr_clone->name == $bar');
22286063 148}
149
1d68af04 150{
151 my $attr = Class::MOP::Attribute->new('$foo', (builder => 'foo_builder'));
152 isa_ok($attr, 'Class::MOP::Attribute');
153
154 ok(!$attr->has_default, '... $attr does not have a default');
155 ok($attr->has_builder, '... $attr does have a builder');
156 is($attr->builder, 'foo_builder', '... $attr->builder == foo_builder');
157
158}