8554a7d84c9625a9fb4093357602e497ac1ec38c
[gitmo/Moose-Policy.git] / t / 001_basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 11;
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 method (mostly stolen from M::M::Attribute) just rebuilds the
19     # options so anything with 'is' gets PBP accessors
20     before '_process_options' => sub {
21         my ($class, $name, $options) = @_;
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
33         $class->SUPER::_process_options($name, $options);
34     }
35 }
36
37 {
38     package My::Moose::Policy;
39     # policy just specifies metaclass delegates
40     use constant attribute_metaclass => 'My::Moose::Meta::Attribute';
41 }
42
43 {
44     package Foo;
45
46     use Moose::Policy 'My::Moose::Policy';
47     use Moose;
48
49     has 'bar' => (is => 'rw', default => 'Foo::bar');
50     has 'baz' => (is => 'ro', default => 'Foo::baz');
51 }
52
53 isa_ok(Foo->meta, 'Moose::Meta::Class');
54 is(Foo->meta->attribute_metaclass, 'My::Moose::Meta::Attribute', '... got our custom attr metaclass');
55
56 isa_ok(Foo->meta->get_attribute('bar'), 'My::Moose::Meta::Attribute');
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
64 can_ok($foo, 'get_baz');
65 ok(! $foo->can('set_baz'), 'without setter');
66
67 is($foo->get_bar, 'Foo::bar', '... got the right default value');
68 is($foo->get_baz, 'Foo::baz', '... got the right default value');
69
70 # vim:ts=4:sw=4:et:sta