9 traits => [ qw(MooseX::UndefTolerant::Attribute)],
12 predicate => 'has_attr1',
17 predicate => 'has_attr2',
22 predicate => 'has_attr3',
29 use MooseX::UndefTolerant;
34 predicate => 'has_attr1',
39 predicate => 'has_attr2',
44 predicate => 'has_attr3',
52 note 'Testing class with a single UndefTolerant attribute';
55 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
56 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
57 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
61 my $obj = Foo->new(attr1 => undef);
62 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
63 isnt (exception { $obj = Foo->new(attr2 => undef) }, undef,
64 'But assigning undef to attr2 generates a type constraint error');
66 is (exception { $obj = Foo->new(attr3 => undef) }, undef,
67 'assigning undef to attr3 is acceptable');
68 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
72 my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
73 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
74 ok($obj->has_attr1, '...and the predicate returns true as normal');
76 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
77 ok($obj->has_attr2, '...and the predicate returns true as normal');
79 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
80 ok($obj->has_attr3, '...and the predicate returns true as normal');
85 note 'Testing class with the entire class being UndefTolerant';
88 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
89 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
90 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
94 my $obj = Bar->new(attr1 => undef);
95 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
96 # note this test differs from the Foo case above
97 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
98 'assigning undef to attr2 does not produce an error');
99 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
101 is( exception { $obj = Foo->new(attr3 => undef) }, undef,
102 'assigning undef to attr3 is acceptable');
103 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
107 my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
108 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
109 ok($obj->has_attr1, '...and the predicate returns true as normal');
111 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
112 ok($obj->has_attr2, '...and the predicate returns true as normal');
114 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
115 ok($obj->has_attr3, '...and the predicate returns true as normal');
120 note 'Constructor behaviour: mutable classes';
125 note 'Constructor behaviour: immutable classes';
127 Foo->meta->make_immutable;
128 Bar->meta->make_immutable;
130 local $TODO = 'some immutable cases are not handled yet';
131 # for now, catch errors
132 is(exception { do_tests }, undef, 'tests do not die');
134 is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!');