Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / attributes / attribute_accessor_generation.t
CommitLineData
ca01a97b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
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 accessor => 'foo',
19 );
20 };
21 ::ok(!$@, '... created the accessor method okay');
d03bd989 22
ca01a97b 23 eval {
24 has 'lazy_foo' => (
d03bd989 25 accessor => 'lazy_foo',
26 lazy => 1,
ca01a97b 27 default => sub { 10 }
28 );
29 };
d03bd989 30 ::ok(!$@, '... created the lazy accessor method okay');
31
ca01a97b 32
33 eval {
34 has 'foo_required' => (
35 accessor => 'foo_required',
36 required => 1,
37 );
38 };
39 ::ok(!$@, '... created the required accessor method okay');
40
41 eval {
42 has 'foo_int' => (
43 accessor => 'foo_int',
44 isa => 'Int',
45 );
46 };
d03bd989 47 ::ok(!$@, '... created the accessor method with type constraint okay');
48
ca01a97b 49 eval {
50 has 'foo_weak' => (
51 accessor => 'foo_weak',
52 weak_ref => 1
53 );
54 };
d03bd989 55 ::ok(!$@, '... created the accessor method with weak_ref okay');
1a563243 56
57 eval {
58 has 'foo_deref' => (
59 accessor => 'foo_deref',
60 isa => 'ArrayRef',
61 auto_deref => 1,
62 );
63 };
d03bd989 64 ::ok(!$@, '... created the accessor method with auto_deref okay');
1a563243 65
66 eval {
67 has 'foo_deref_ro' => (
68 reader => 'foo_deref_ro',
69 isa => 'ArrayRef',
70 auto_deref => 1,
71 );
72 };
d03bd989 73 ::ok(!$@, '... created the reader method with auto_deref okay');
1a563243 74
75 eval {
76 has 'foo_deref_hash' => (
77 accessor => 'foo_deref_hash',
78 isa => 'HashRef',
79 auto_deref => 1,
80 );
81 };
d03bd989 82 ::ok(!$@, '... created the reader method with auto_deref okay');
ca01a97b 83}
84
85{
86 my $foo = Foo->new(foo_required => 'required');
87 isa_ok($foo, 'Foo');
88
89 # regular accessor
90
91 can_ok($foo, 'foo');
92 is($foo->foo(), undef, '... got an unset value');
b10dde3a 93 is( exception {
ca01a97b 94 $foo->foo(100);
b10dde3a 95 }, undef, '... foo wrote successfully' );
d03bd989 96 is($foo->foo(), 100, '... got the correct set value');
97
98 ok(!isweak($foo->{foo}), '... it is not a weak reference');
99
ca01a97b 100 # required writer
d03bd989 101
b10dde3a 102 isnt( exception {
ca01a97b 103 Foo->new;
b10dde3a 104 }, undef, '... cannot create without the required attribute' );
ca01a97b 105
106 can_ok($foo, 'foo_required');
107 is($foo->foo_required(), 'required', '... got an unset value');
b10dde3a 108 is( exception {
ca01a97b 109 $foo->foo_required(100);
b10dde3a 110 }, undef, '... foo_required wrote successfully' );
d03bd989 111 is($foo->foo_required(), 100, '... got the correct set value');
112
b10dde3a 113 is( exception {
ca01a97b 114 $foo->foo_required(undef);
b10dde3a 115 }, undef, '... foo_required did not die with undef' );
b6af66f8 116
117 is($foo->foo_required, undef, "value is undef");
ca01a97b 118
d03bd989 119 ok(!isweak($foo->{foo_required}), '... it is not a weak reference');
120
ca01a97b 121 # lazy
d03bd989 122
ca01a97b 123 ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot');
d03bd989 124
ca01a97b 125 can_ok($foo, 'lazy_foo');
d03bd989 126 is($foo->lazy_foo(), 10, '... got an deferred value');
127
ca01a97b 128 # with type constraint
d03bd989 129
ca01a97b 130 can_ok($foo, 'foo_int');
131 is($foo->foo_int(), undef, '... got an unset value');
b10dde3a 132 is( exception {
ca01a97b 133 $foo->foo_int(100);
b10dde3a 134 }, undef, '... foo_int wrote successfully' );
d03bd989 135 is($foo->foo_int(), 100, '... got the correct set value');
136
b10dde3a 137 isnt( exception {
ca01a97b 138 $foo->foo_int("Foo");
b10dde3a 139 }, undef, '... foo_int died successfully' );
d03bd989 140
141 ok(!isweak($foo->{foo_int}), '... it is not a weak reference');
142
ca01a97b 143 # with weak_ref
d03bd989 144
ca01a97b 145 my $test = [];
d03bd989 146
ca01a97b 147 can_ok($foo, 'foo_weak');
148 is($foo->foo_weak(), undef, '... got an unset value');
b10dde3a 149 is( exception {
ca01a97b 150 $foo->foo_weak($test);
b10dde3a 151 }, undef, '... foo_weak wrote successfully' );
d03bd989 152 is($foo->foo_weak(), $test, '... got the correct set value');
153
ca01a97b 154 ok(isweak($foo->{foo_weak}), '... it is a weak reference');
155
1a563243 156 can_ok( $foo, 'foo_deref');
3f7376b0 157 is_deeply( [$foo->foo_deref()], [], '... default default value');
1a563243 158 my @list;
b10dde3a 159 is( exception {
1a563243 160 @list = $foo->foo_deref();
b10dde3a 161 }, undef, "... doesn't deref undef value" );
1a563243 162 is_deeply( \@list, [], "returns empty list in list context");
163
b10dde3a 164 is( exception {
1a563243 165 $foo->foo_deref( [ qw/foo bar gorch/ ] );
b10dde3a 166 }, undef, '... foo_deref wrote successfully' );
1a563243 167
168 is( Scalar::Util::reftype( scalar $foo->foo_deref() ), "ARRAY", "returns an array reference in scalar context" );
169 is_deeply( scalar($foo->foo_deref()), [ qw/foo bar gorch/ ], "correct array" );
170
171 is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" );
172 is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" );
173
174
175 can_ok( $foo, 'foo_deref' );
3f7376b0 176 is_deeply( [$foo->foo_deref_ro()], [], "... default default value" );
1a563243 177
b10dde3a 178 isnt( exception {
1a563243 179 $foo->foo_deref_ro( [] );
b10dde3a 180 }, undef, "... read only" );
1a563243 181
182 $foo->{foo_deref_ro} = [qw/la la la/];
183
184 is_deeply( scalar($foo->foo_deref_ro()), [qw/la la la/], "scalar context ro" );
185 is_deeply( [ $foo->foo_deref_ro() ], [qw/la la la/], "list context ro" );
186
187 can_ok( $foo, 'foo_deref_hash' );
3f7376b0 188 is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" );
1a563243 189
190 my %hash;
b10dde3a 191 is( exception {
1a563243 192 %hash = $foo->foo_deref_hash();
b10dde3a 193 }, undef, "... doesn't deref undef value" );
1a563243 194 is_deeply( \%hash, {}, "returns empty list in list context");
195
b10dde3a 196 is( exception {
1a563243 197 $foo->foo_deref_hash( { foo => 1, bar => 2 } );
b10dde3a 198 }, undef, '... foo_deref_hash wrote successfully' );
1a563243 199
200 is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" );
201
202 %hash = $foo->foo_deref_hash;
203 is_deeply( \%hash, { foo => 1, bar => 2 }, "list context");
ca01a97b 204}
205
a28e50e4 206done_testing;