Commit | Line | Data |
461d2917 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
09eff61e |
6 | use Test::More tests => 22; |
461d2917 |
7 | |
8 | BEGIN { |
9 | use_ok('Moose::Policy'); |
10 | } |
11 | |
12 | { |
13 | package My::Moose::Meta::Attribute; |
14 | use Moose; |
15 | |
16 | extends 'Moose::Meta::Attribute'; |
17 | |
18 | # this one gives you PBP accessors |
19 | # and gives you accessors unless you said otherwise |
20 | sub _process_options { |
21 | my ($class, $name, $options) = @_; |
22 | my $is = delete $options->{is} || 'rw'; |
23 | if($is eq 'ro') { |
24 | $options->{reader} = 'get_' . $name; |
25 | } |
26 | elsif($is eq 'rw') { |
27 | $options->{reader} = 'get_' . $name; |
28 | $options->{writer} = 'set_' . $name; |
29 | } |
30 | elsif($is eq 'no') { |
31 | # only way to not get accessors |
32 | } |
33 | else { |
34 | # pass it through and let him deal with it |
35 | $options->{is} = $is; |
36 | } |
37 | |
38 | $class->SUPER::_process_options($name, $options); |
39 | } |
40 | } |
41 | |
42 | { |
43 | package My::Moose::Policy; |
44 | # policy just specifies metaclass delegates |
45 | use constant attribute_metaclass => 'My::Moose::Meta::Attribute'; |
46 | } |
47 | |
48 | my $oops; |
49 | { |
50 | package Foo; |
51 | |
52 | use Moose::Policy 'My::Moose::Policy'; |
53 | use Moose; |
54 | |
55 | has 'bar' => (default => 'Foo::bar'); |
56 | has 'baz' => (default => 'Foo::baz'); |
82a71d18 |
57 | has 'bop' => (is => 'bare', default => 'woot'); |
461d2917 |
58 | eval { has 'oops' => (is => 'thbbbt'); }; |
59 | $oops = $@; |
60 | } |
61 | |
62 | ok($oops, "thbbt got booted out"); |
63 | |
64 | isa_ok(Foo->meta, 'Moose::Meta::Class'); |
65 | is(Foo->meta->attribute_metaclass, 'My::Moose::Meta::Attribute', '... got our custom attr metaclass'); |
66 | |
67 | isa_ok(Foo->meta->get_attribute('bar'), 'My::Moose::Meta::Attribute'); |
68 | |
69 | my $foo = Foo->new; |
70 | isa_ok($foo, 'Foo'); |
71 | |
72 | can_ok($foo, 'get_bar'); |
73 | can_ok($foo, 'set_bar'); |
74 | |
75 | can_ok($foo, 'get_baz'); |
76 | can_ok($foo, 'set_baz'); |
77 | |
78 | ok(! $foo->can('bop'), 'do not want any bop'); |
79 | ok(! $foo->can('get_bop'), 'do not want any bop'); |
80 | ok(! $foo->can('set_bop'), 'do not want any bop'); |
81 | |
82 | ok(! $foo->can('oops'), 'do not want any oops'); |
83 | ok(! $foo->can('get_oops'), 'do not want any oops'); |
84 | ok(! $foo->can('set_oops'), 'do not want any oops'); |
85 | |
86 | is($foo->get_bar, 'Foo::bar', '... got the right default bar value'); |
87 | is($foo->get_baz, 'Foo::baz', '... got the right default baz value'); |
88 | ok(exists($foo->{bop}), 'we have bop'); |
89 | ok(! exists($foo->{oops}), 'do not want to have an oops'); |
90 | is($foo->{bop}, 'woot', '... got the right default bop value'); |
91 | |
92 | $foo->set_bar('the new bar'); |
93 | is($foo->get_bar, 'the new bar', 'setter works'); |
94 | |
95 | # vim:ts=4:sw=4:et:sta |
96 | |