Commit | Line | Data |
82cfc049 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
09eff61e |
6 | use Test::More tests => 11; |
82cfc049 |
7 | |
8 | BEGIN { |
b3cebad5 |
9 | use_ok('Moose::Policy'); |
b14c8df5 |
10 | } |
11 | |
12 | { |
13 | package My::Moose::Meta::Attribute; |
14 | use Moose; |
b3cebad5 |
15 | |
b14c8df5 |
16 | extends 'Moose::Meta::Attribute'; |
b3cebad5 |
17 | |
bfacd619 |
18 | # this method (mostly stolen from M::M::Attribute) just rebuilds the |
19 | # options so anything with 'is' gets PBP accessors |
2756232e |
20 | before '_process_options' => sub { |
b14c8df5 |
21 | my ($class, $name, $options) = @_; |
b3cebad5 |
22 | if (exists $options->{is}) { |
23 | if ($options->{is} eq 'ro') { |
24 | $options->{reader} = 'get_' . $name; |
25 | } |
26 | elsif ($options->{is} eq 'rw') { |
27 | $options->{reader} = 'get_' . $name; |
28 | $options->{writer} = 'set_' . $name; |
29 | } |
30 | delete $options->{is}; |
31 | } |
32 | |
b9238462 |
33 | $class->SUPER::_process_options($name, $options); |
34 | } |
b14c8df5 |
35 | } |
36 | |
b14c8df5 |
37 | { |
38 | package My::Moose::Policy; |
bfacd619 |
39 | # policy just specifies metaclass delegates |
5d1afb58 |
40 | use constant attribute_metaclass => 'My::Moose::Meta::Attribute'; |
41 | } |
42 | |
43 | { |
44 | package Foo; |
b3cebad5 |
45 | |
5d1afb58 |
46 | use Moose::Policy 'My::Moose::Policy'; |
47 | use Moose; |
b3cebad5 |
48 | |
c5fc6242 |
49 | has 'bar' => (is => 'rw', default => 'Foo::bar'); |
bfacd619 |
50 | has 'baz' => (is => 'ro', default => 'Foo::baz'); |
5d1afb58 |
51 | } |
52 | |
b9238462 |
53 | isa_ok(Foo->meta, 'Moose::Meta::Class'); |
54 | is(Foo->meta->attribute_metaclass, 'My::Moose::Meta::Attribute', '... got our custom attr metaclass'); |
55 | |
7efb3207 |
56 | isa_ok(Foo->meta->get_attribute('bar'), 'My::Moose::Meta::Attribute'); |
5d1afb58 |
57 | |
58 | my $foo = Foo->new; |
59 | isa_ok($foo, 'Foo'); |
60 | |
61 | can_ok($foo, 'get_bar'); |
62 | can_ok($foo, 'set_bar'); |
63 | |
bfacd619 |
64 | can_ok($foo, 'get_baz'); |
2ae39004 |
65 | ok(! $foo->can('set_baz'), 'without setter'); |
bfacd619 |
66 | |
5d1afb58 |
67 | is($foo->get_bar, 'Foo::bar', '... got the right default value'); |
bfacd619 |
68 | is($foo->get_baz, 'Foo::baz', '... got the right default value'); |
5d1afb58 |
69 | |
b3cebad5 |
70 | # vim:ts=4:sw=4:et:sta |