Merge branch 'master' into attribute_helpers
[gitmo/Moose.git] / t / 020_attributes / 001_attribute_reader_generation.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 14;
7 use Test::Exception;
8
9
10
11 {
12     package Foo;
13     use Moose;
14
15     eval {
16         has 'foo' => (
17             reader => 'get_foo'
18         );
19     };
20     ::ok(!$@, '... created the reader method okay');
21
22     eval {
23         has 'lazy_foo' => (
24             reader => 'get_lazy_foo',
25             lazy => 1,
26             default => sub { 10 }
27         );
28     };
29     ::ok(!$@, '... created the lazy reader method okay') or warn $@;
30
31     my $warn;
32
33     eval {
34         local $SIG{__WARN__} = sub { $warn = $_[0] };
35         has 'mtfnpy' => (
36             reder => 'get_mftnpy'
37         );
38     };
39     ::ok($warn, '... got a warning for mispelled attribute argument');
40 }
41
42 {
43     my $foo = Foo->new;
44     isa_ok($foo, 'Foo');
45
46     can_ok($foo, 'get_foo');
47     is($foo->get_foo(), undef, '... got an undefined value');
48     dies_ok {
49         $foo->get_foo(100);
50     } '... get_foo is a read-only';
51
52     ok(!exists($foo->{lazy_foo}), '... no value in get_lazy_foo slot');
53
54     can_ok($foo, 'get_lazy_foo');
55     is($foo->get_lazy_foo(), 10, '... got an deferred value');
56     dies_ok {
57         $foo->get_lazy_foo(100);
58     } '... get_lazy_foo is a read-only';
59 }
60
61 {
62     my $foo = Foo->new(foo => 10, lazy_foo => 100);
63     isa_ok($foo, 'Foo');
64
65     is($foo->get_foo(), 10, '... got the correct value');
66     is($foo->get_lazy_foo(), 100, '... got the correct value');
67 }
68
69
70