10 traits => [ qw(MooseX::UndefTolerant::Attribute)],
13 predicate => 'has_attr1',
18 predicate => 'has_attr2',
23 predicate => 'has_attr3',
30 use MooseX::UndefTolerant;
35 predicate => 'has_attr1',
40 predicate => 'has_attr2',
45 predicate => 'has_attr3',
53 note 'Testing class with a single UndefTolerant attribute';
56 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
57 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
58 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
63 $TODO = 'some immutable cases are not handled yet; see CAVEATS' if Foo->meta->is_immutable;
67 my $obj = Foo->new(attr1 => undef);
68 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
70 exception { $obj = Foo->new(attr2 => undef) },
71 qr/\QAttribute (attr2) does not pass the type constraint because: Validation failed for 'Num' with value undef\E/,
72 'But assigning undef to attr2 generates a type constraint error');
74 is (exception { $obj = Foo->new(attr3 => undef) }, undef,
75 'assigning undef to attr3 is acceptable');
76 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
79 'successfully tested spot-applicaction of UT trait in immutable classes',
84 my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
85 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
86 ok($obj->has_attr1, '...and the predicate returns true as normal');
88 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
89 ok($obj->has_attr2, '...and the predicate returns true as normal');
91 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
92 ok($obj->has_attr3, '...and the predicate returns true as normal');
97 note 'Testing class with the entire class being UndefTolerant';
100 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
101 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
102 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
106 my $obj = Bar->new(attr1 => undef);
107 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
108 # note this test differs from the Foo case above
109 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
110 'assigning undef to attr2 does not produce an error');
111 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
113 is( exception { $obj = Foo->new(attr3 => undef) }, undef,
114 'assigning undef to attr3 is acceptable');
115 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
119 my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
120 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
121 ok($obj->has_attr1, '...and the predicate returns true as normal');
123 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
124 ok($obj->has_attr2, '...and the predicate returns true as normal');
126 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
127 ok($obj->has_attr3, '...and the predicate returns true as normal');
132 note 'Ran ', Test::More->builder->current_test, ' tests - should have run 56';