Import tests for attribute from Mouse's tests
[gitmo/Mouse.git] / t / 020_attributes / failing / 001_attribute_reader_generation.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 20;
7 use Test::Exception;
8
9
10
11 {
12     package Foo;
13     use Mouse;
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;
63     isa_ok($foo, 'Foo');
64
65     my $attr = $foo->meta->find_attribute_by_name("lazy_foo");
66
67     isa_ok( $attr, "Mouse::Meta::Attribute" );
68
69     ok( $attr->is_lazy, "it's lazy" );
70
71     is( $attr->get_raw_value($foo), undef, "raw value" );
72
73     is( $attr->get_value($foo), 10, "lazy value" );
74
75     is( $attr->get_raw_value($foo), 10, "raw value" );
76 }
77
78 {
79     my $foo = Foo->new(foo => 10, lazy_foo => 100);
80     isa_ok($foo, 'Foo');
81
82     is($foo->get_foo(), 10, '... got the correct value');
83     is($foo->get_lazy_foo(), 100, '... got the correct value');
84 }
85
86
87