Can't coerce a type that doesn't define a coercion.
[gitmo/Moose.git] / t / 100_bugs / 013_lazybuild_required_undef.t
CommitLineData
e5321a24 1package Foo;
2use Moose;
3
4## Problem:
5## lazy_build sets required => 1
6## required does not permit setting to undef
7
8## Possible solutions:
9#### remove required => 1
10#### check the attr to see if it accepts Undef (Maybe[], | Undef)
11#### or, make required accept undef and use a predicate test
12
13
14has 'foo' => ( isa => 'Int | Undef', is => 'rw', coerce => 1, lazy_build => 1 );
15has 'bar' => ( isa => 'Int | Undef', is => 'rw', coerce => 1 );
16
17sub _build_foo { undef }
18
19package main;
a28e50e4 20use Test::More;
e5321a24 21
22ok ( !defined(Foo->new->bar), 'NonLazyBuild: Undef default' );
23ok ( !defined(Foo->new->bar(undef)), 'NonLazyBuild: Undef explicit' );
24
25ok ( !defined(Foo->new->foo), 'LazyBuild: Undef default/lazy_build' );
26
27## This test fails at the time of creation.
28ok ( !defined(Foo->new->foo(undef)), 'LazyBuild: Undef explicit' );
29
a28e50e4 30done_testing;