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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
use strict;
use warnings;
use Scalar::Util 'reftype', 'blessed';
use Test::More;
use Test::Exception;
use Class::MOP;
use Class::MOP::Attribute;
use Class::MOP::Method;
dies_ok { Class::MOP::Attribute->name } q{... can't call name() as a class method};
{
my $attr = Class::MOP::Attribute->new('$foo');
isa_ok($attr, 'Class::MOP::Attribute');
is($attr->name, '$foo', '... $attr->name == $foo');
ok($attr->has_init_arg, '... $attr does have an init_arg');
is($attr->init_arg, '$foo', '... $attr init_arg is the name');
ok(!$attr->has_accessor, '... $attr does not have an accessor');
ok(!$attr->has_reader, '... $attr does not have an reader');
ok(!$attr->has_writer, '... $attr does not have an writer');
ok(!$attr->has_default, '... $attr does not have an default');
ok(!$attr->has_builder, '... $attr does not have a builder');
{
my $reader = $attr->get_read_method_ref;
my $writer = $attr->get_write_method_ref;
ok(!blessed($reader), '... it is a plain old sub');
ok(!blessed($writer), '... it is a plain old sub');
is(reftype($reader), 'CODE', '... it is a plain old sub');
is(reftype($writer), 'CODE', '... it is a plain old sub');
}
my $class = Class::MOP::Class->initialize('Foo');
isa_ok($class, 'Class::MOP::Class');
lives_ok {
$attr->attach_to_class($class);
} '... attached a class successfully';
is($attr->associated_class, $class, '... the class was associated correctly');
ok(!$attr->get_read_method, '... $attr does not have an read method');
ok(!$attr->get_write_method, '... $attr does not have an write method');
{
my $reader = $attr->get_read_method_ref;
my $writer = $attr->get_write_method_ref;
ok(blessed($reader), '... it is a plain old sub');
ok(blessed($writer), '... it is a plain old sub');
isa_ok($reader, 'Class::MOP::Method');
isa_ok($writer, 'Class::MOP::Method');
}
my $attr_clone = $attr->clone();
isa_ok($attr_clone, 'Class::MOP::Attribute');
isnt($attr, $attr_clone, '... but they are different instances');
is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
is($attr->associated_class, $class, '... the associated classes are the same though');
is($attr_clone->associated_class, $class, '... the associated classes are the same though');
is_deeply($attr, $attr_clone, '... but they are the same inside');
}
{
my $attr = Class::MOP::Attribute->new('$foo', (
init_arg => '-foo',
default => 'BAR'
));
isa_ok($attr, 'Class::MOP::Attribute');
is($attr->name, '$foo', '... $attr->name == $foo');
ok($attr->has_init_arg, '... $attr does have an init_arg');
is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
ok($attr->has_default, '... $attr does have an default');
is($attr->default, 'BAR', '... $attr->default == BAR');
ok(!$attr->has_builder, '... $attr does not have a builder');
ok(!$attr->has_accessor, '... $attr does not have an accessor');
ok(!$attr->has_reader, '... $attr does not have an reader');
ok(!$attr->has_writer, '... $attr does not have an writer');
ok(!$attr->get_read_method, '... $attr does not have an read method');
ok(!$attr->get_write_method, '... $attr does not have an write method');
{
my $reader = $attr->get_read_method_ref;
my $writer = $attr->get_write_method_ref;
ok(!blessed($reader), '... it is a plain old sub');
ok(!blessed($writer), '... it is a plain old sub');
is(reftype($reader), 'CODE', '... it is a plain old sub');
is(reftype($writer), 'CODE', '... it is a plain old sub');
}
my $attr_clone = $attr->clone();
isa_ok($attr_clone, 'Class::MOP::Attribute');
isnt($attr, $attr_clone, '... but they are different instances');
is($attr->associated_class, $attr_clone->associated_class, '... the associated classes are the same though');
is($attr->associated_class, undef, '... the associated class is actually undef');
is($attr_clone->associated_class, undef, '... the associated class is actually undef');
is_deeply($attr, $attr_clone, '... but they are the same inside');
}
{
my $attr = Class::MOP::Attribute->new('$foo', (
accessor => 'foo',
init_arg => '-foo',
default => 'BAR'
));
isa_ok($attr, 'Class::MOP::Attribute');
is($attr->name, '$foo', '... $attr->name == $foo');
ok($attr->has_init_arg, '... $attr does have an init_arg');
is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
ok($attr->has_default, '... $attr does have an default');
is($attr->default, 'BAR', '... $attr->default == BAR');
ok($attr->has_accessor, '... $attr does have an accessor');
is($attr->accessor, 'foo', '... $attr->accessor == foo');
ok(!$attr->has_reader, '... $attr does not have an reader');
ok(!$attr->has_writer, '... $attr does not have an writer');
is($attr->get_read_method, 'foo', '... $attr does not have an read method');
is($attr->get_write_method, 'foo', '... $attr does not have an write method');
{
my $reader = $attr->get_read_method_ref;
my $writer = $attr->get_write_method_ref;
ok(!blessed($reader), '... it is not a plain old sub');
ok(!blessed($writer), '... it is not a plain old sub');
is(reftype($reader), 'CODE', '... it is a plain old sub');
is(reftype($writer), 'CODE', '... it is a plain old sub');
}
my $attr_clone = $attr->clone();
isa_ok($attr_clone, 'Class::MOP::Attribute');
isnt($attr, $attr_clone, '... but they are different instances');
is_deeply($attr, $attr_clone, '... but they are the same inside');
}
{
my $attr = Class::MOP::Attribute->new('$foo', (
reader => 'get_foo',
writer => 'set_foo',
init_arg => '-foo',
default => 'BAR'
));
isa_ok($attr, 'Class::MOP::Attribute');
is($attr->name, '$foo', '... $attr->name == $foo');
ok($attr->has_init_arg, '... $attr does have an init_arg');
is($attr->init_arg, '-foo', '... $attr->init_arg == -foo');
ok($attr->has_default, '... $attr does have an default');
is($attr->default, 'BAR', '... $attr->default == BAR');
ok($attr->has_reader, '... $attr does have an reader');
is($attr->reader, 'get_foo', '... $attr->reader == get_foo');
ok($attr->has_writer, '... $attr does have an writer');
is($attr->writer, 'set_foo', '... $attr->writer == set_foo');
ok(!$attr->has_accessor, '... $attr does not have an accessor');
is($attr->get_read_method, 'get_foo', '... $attr does not have an read method');
is($attr->get_write_method, 'set_foo', '... $attr does not have an write method');
{
my $reader = $attr->get_read_method_ref;
my $writer = $attr->get_write_method_ref;
ok(!blessed($reader), '... it is not a plain old sub');
ok(!blessed($writer), '... it is not a plain old sub');
is(reftype($reader), 'CODE', '... it is a plain old sub');
is(reftype($writer), 'CODE', '... it is a plain old sub');
}
my $attr_clone = $attr->clone();
isa_ok($attr_clone, 'Class::MOP::Attribute');
isnt($attr, $attr_clone, '... but they are different instances');
is_deeply($attr, $attr_clone, '... but they are the same inside');
}
{
my $attr = Class::MOP::Attribute->new('$foo');
isa_ok($attr, 'Class::MOP::Attribute');
my $attr_clone = $attr->clone('name' => '$bar');
isa_ok($attr_clone, 'Class::MOP::Attribute');
isnt($attr, $attr_clone, '... but they are different instances');
isnt($attr->name, $attr_clone->name, '... we changes the name parameter');
is($attr->name, '$foo', '... $attr->name == $foo');
is($attr_clone->name, '$bar', '... $attr_clone->name == $bar');
}
{
my $attr = Class::MOP::Attribute->new('$foo', (builder => 'foo_builder'));
isa_ok($attr, 'Class::MOP::Attribute');
ok(!$attr->has_default, '... $attr does not have a default');
ok($attr->has_builder, '... $attr does have a builder');
is($attr->builder, 'foo_builder', '... $attr->builder == foo_builder');
}
{
for my $value ({}, bless({}, 'Foo')) {
throws_ok {
Class::MOP::Attribute->new('$foo', default => $value);
} qr/References are not allowed as default values/;
}
}
{
my $attr;
lives_ok {
my $meth = Class::MOP::Method->wrap(sub {shift}, name => 'foo', package_name => 'bar');
$attr = Class::MOP::Attribute->new('$foo', default => $meth);
} 'Class::MOP::Methods accepted as default';
is($attr->default(42), 42, 'passthrough for default on attribute');
}
done_testing;
|