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');
64 exception { $obj = Foo->new(attr2 => undef) },
65 qr/\QAttribute (attr2) does not pass the type constraint because: Validation failed for 'Num' with value undef\E/,
66 'But assigning undef to attr2 generates a type constraint error');
68 is (exception { $obj = Foo->new(attr3 => undef) }, undef,
69 'assigning undef to attr3 is acceptable');
70 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
74 my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
75 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
76 ok($obj->has_attr1, '...and the predicate returns true as normal');
78 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
79 ok($obj->has_attr2, '...and the predicate returns true as normal');
81 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
82 ok($obj->has_attr3, '...and the predicate returns true as normal');
87 note 'Testing class with the entire class being UndefTolerant';
90 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
91 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
92 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
96 my $obj = Bar->new(attr1 => undef);
97 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
98 # note this test differs from the Foo case above
99 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
100 'assigning undef to attr2 does not produce an error');
101 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
103 is( exception { $obj = Foo->new(attr3 => undef) }, undef,
104 'assigning undef to attr3 is acceptable');
105 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
109 my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
110 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
111 ok($obj->has_attr1, '...and the predicate returns true as normal');
113 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
114 ok($obj->has_attr2, '...and the predicate returns true as normal');
116 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
117 ok($obj->has_attr3, '...and the predicate returns true as normal');
122 note 'Constructor behaviour: mutable classes';
127 note 'Constructor behaviour: immutable classes';
129 Foo->meta->make_immutable;
130 Bar->meta->make_immutable;
132 local $TODO = 'some immutable cases are not handled yet';
133 # for now, catch errors
134 is(exception { do_tests }, undef, 'tests do not die');
136 is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!');