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