Revert most of the conversion to Test::Fatal so we can redo it
[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;
53a4d826 7use Test::Exception;
ca01a97b 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');
53a4d826 60 lives_ok {
ca01a97b 61 $foo->set_foo(100);
53a4d826 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
53a4d826 69 dies_ok {
ca01a97b 70 Foo->new;
53a4d826 71 } '... cannot create without the required attribute';
ca01a97b 72
73 can_ok($foo, 'set_foo_required');
74 is($foo->get_foo_required(), 'required', '... got an unset value');
53a4d826 75 lives_ok {
ca01a97b 76 $foo->set_foo_required(100);
53a4d826 77 } '... set_foo_required wrote successfully';
d03bd989 78 is($foo->get_foo_required(), 100, '... got the correct set value');
79
53a4d826 80 dies_ok {
b6af66f8 81 $foo->set_foo_required();
53a4d826 82 } '... set_foo_required died successfully with no value';
b6af66f8 83
53a4d826 84 lives_ok {
ca01a97b 85 $foo->set_foo_required(undef);
53a4d826 86 } '... set_foo_required did accept undef';
d03bd989 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');
53a4d826 94 lives_ok {
ca01a97b 95 $foo->set_foo_int(100);
53a4d826 96 } '... set_foo_int wrote successfully';
d03bd989 97 is($foo->get_foo_int(), 100, '... got the correct set value');
98
53a4d826 99 dies_ok {
ca01a97b 100 $foo->set_foo_int("Foo");
53a4d826 101 } '... set_foo_int died successfully';
d03bd989 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');
53a4d826 111 lives_ok {
ca01a97b 112 $foo->set_foo_weak($test);
53a4d826 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;