We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / attributes / method_generation_rules.t
CommitLineData
21f1e231 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
21f1e231 8
7ff56534 9
21f1e231 10=pod
11
12 is => rw, writer => _foo # turns into (reader => foo, writer => _foo)
13 is => ro, writer => _foo # turns into (reader => foo, writer => _foo) as before
14 is => rw, accessor => _foo # turns into (accessor => _foo)
15 is => ro, accessor => _foo # error, accesor is rw
16
17=cut
18
19sub make_class {
9978e85e 20 my ($is, $attr, $class) = @_;
21f1e231 21
9978e85e 22 eval "package $class; use Moose; has 'foo' => ( is => '$is', $attr => '_foo' );";
21f1e231 23
9978e85e 24 return $@ ? die $@ : $class;
21f1e231 25}
26
27my $obj;
28my $class;
29
30$class = make_class('rw', 'writer', 'Test::Class::WriterRW');
31ok($class, "Can define attr with rw + writer");
32
33$obj = $class->new();
34
35can_ok($obj, qw/foo _foo/);
b10dde3a 36is( exception {$obj->_foo(1)}, undef, "$class->_foo is writer" );
21f1e231 37is($obj->foo(), 1, "$class->foo is reader");
b10dde3a 38isnt( exception {$obj->foo(2)}, undef, "$class->foo is not writer" ); # this should fail
d03bd989 39ok(!defined $obj->_foo(), "$class->_foo is not reader");
21f1e231 40
41$class = make_class('ro', 'writer', 'Test::Class::WriterRO');
42ok($class, "Can define attr with ro + writer");
43
44$obj = $class->new();
45
46can_ok($obj, qw/foo _foo/);
b10dde3a 47is( exception {$obj->_foo(1)}, undef, "$class->_foo is writer" );
21f1e231 48is($obj->foo(), 1, "$class->foo is reader");
b10dde3a 49isnt( exception {$obj->foo(1)}, undef, "$class->foo is not writer" );
21f1e231 50isnt($obj->_foo(), 1, "$class->_foo is not reader");
51
52$class = make_class('rw', 'accessor', 'Test::Class::AccessorRW');
53ok($class, "Can define attr with rw + accessor");
54
55$obj = $class->new();
56
57can_ok($obj, qw/_foo/);
b10dde3a 58is( exception {$obj->_foo(1)}, undef, "$class->_foo is writer" );
21f1e231 59is($obj->_foo(), 1, "$class->foo is reader");
60
b10dde3a 61isnt( exception { make_class('ro', 'accessor', "Test::Class::AccessorRO"); }, undef, "Cant define attr with ro + accessor" );
21f1e231 62
a28e50e4 63done_testing;