DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / t / 020_attributes / 002_attribute_writer_generation.t
CommitLineData
ca01a97b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7ff56534 6use Test::More tests => 29;
ca01a97b 7use Test::Exception;
8
9use Scalar::Util 'isweak';
10
7ff56534 11
ca01a97b 12
13{
14 package Foo;
ca01a97b 15 use Moose;
d03bd989 16
ca01a97b 17 eval {
18 has 'foo' => (
19 reader => 'get_foo',
20 writer => 'set_foo',
21 );
22 };
1808c2da 23 ::ok(!$@, 'created the writer method okay');
ca01a97b 24
25 eval {
26 has 'foo_required' => (
27 reader => 'get_foo_required',
28 writer => 'set_foo_required',
29 required => 1,
30 );
31 };
1808c2da 32 ::ok(!$@, 'created the required writer method okay');
ca01a97b 33
34 eval {
35 has 'foo_int' => (
36 reader => 'get_foo_int',
37 writer => 'set_foo_int',
38 isa => 'Int',
39 );
40 };
1808c2da 41 ::ok(!$@, 'created the writer method with type constraint okay');
d03bd989 42
ca01a97b 43 eval {
44 has 'foo_weak' => (
45 reader => 'get_foo_weak',
46 writer => 'set_foo_weak',
47 weak_ref => 1
48 );
49 };
1808c2da 50 ::ok(!$@, 'created the writer method with weak_ref okay');
ca01a97b 51}
52
53{
54 my $foo = Foo->new(foo_required => 'required');
55 isa_ok($foo, 'Foo');
56
57 # regular writer
58
59 can_ok($foo, 'set_foo');
1808c2da 60 is($foo->get_foo(), undef, 'got an unset value');
ca01a97b 61 lives_ok {
62 $foo->set_foo(100);
1808c2da 63 } 'set_foo wrote successfully';
64 is($foo->get_foo(), 100, 'got the correct set value');
d03bd989 65
1808c2da 66 ok(!isweak($foo->{foo}), 'it is not a weak reference');
d03bd989 67
ca01a97b 68 # required writer
d03bd989 69
ca01a97b 70 dies_ok {
71 Foo->new;
1808c2da 72 } 'cannot create without the required attribute';
ca01a97b 73
74 can_ok($foo, 'set_foo_required');
1808c2da 75 is($foo->get_foo_required(), 'required', 'got an unset value');
ca01a97b 76 lives_ok {
77 $foo->set_foo_required(100);
1808c2da 78 } 'set_foo_required wrote successfully';
79 is($foo->get_foo_required(), 100, 'got the correct set value');
d03bd989 80
ca01a97b 81 dies_ok {
b6af66f8 82 $foo->set_foo_required();
1808c2da 83 } 'set_foo_required died successfully with no value';
b6af66f8 84
85 lives_ok {
ca01a97b 86 $foo->set_foo_required(undef);
1808c2da 87 } 'set_foo_required did accept undef';
d03bd989 88
1808c2da 89 ok(!isweak($foo->{foo_required}), 'it is not a weak reference');
ca01a97b 90
ca01a97b 91 # with type constraint
d03bd989 92
ca01a97b 93 can_ok($foo, 'set_foo_int');
1808c2da 94 is($foo->get_foo_int(), undef, 'got an unset value');
ca01a97b 95 lives_ok {
96 $foo->set_foo_int(100);
1808c2da 97 } 'set_foo_int wrote successfully';
98 is($foo->get_foo_int(), 100, 'got the correct set value');
d03bd989 99
ca01a97b 100 dies_ok {
101 $foo->set_foo_int("Foo");
1808c2da 102 } 'set_foo_int died successfully';
d03bd989 103
1808c2da 104 ok(!isweak($foo->{foo_int}), 'it is not a weak reference');
d03bd989 105
ca01a97b 106 # with weak_ref
d03bd989 107
ca01a97b 108 my $test = [];
d03bd989 109
ca01a97b 110 can_ok($foo, 'set_foo_weak');
1808c2da 111 is($foo->get_foo_weak(), undef, 'got an unset value');
ca01a97b 112 lives_ok {
113 $foo->set_foo_weak($test);
1808c2da 114 } 'set_foo_weak wrote successfully';
115 is($foo->get_foo_weak(), $test, 'got the correct set value');
d03bd989 116
1808c2da 117 ok(isweak($foo->{foo_weak}), 'it is a weak reference');
ca01a97b 118}
119
120
121