1 package # hide from PAUSE
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',
48 package # hide from PAUSE
59 note 'Testing ', (Foo->meta->is_immutable ? 'im' : '') . 'mutable ',
60 'class with a single UndefTolerant attribute';
63 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
64 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
65 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
70 $TODO = 'some immutable cases are not handled yet; see CAVEATS' if Foo->meta->is_immutable;
74 my $obj = Foo->new(attr1 => undef);
75 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
77 exception { $obj = Foo->new(attr2 => undef) },
78 qr/\QAttribute (attr2) does not pass the type constraint because: Validation failed for 'Num' with value undef\E/,
79 'But assigning undef to attr2 generates a type constraint error');
81 is (exception { $obj = Foo->new(attr3 => undef) }, undef,
82 'assigning undef to attr3 is acceptable');
83 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
86 'successfully tested spot-application of UT trait in '
87 . (Foo->meta->is_immutable ? 'im' : '') . 'mutable classes',
92 my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
93 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
94 ok($obj->has_attr1, '...and the predicate returns true as normal');
96 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
97 ok($obj->has_attr2, '...and the predicate returns true as normal');
99 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
100 ok($obj->has_attr3, '...and the predicate returns true as normal');
104 note 'Testing class with the entire ',
105 (Foo->meta->is_immutable ? 'im' : '') . 'mutable ',
106 'class being UndefTolerant';
109 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
110 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
111 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
115 my $obj = Bar->new(attr1 => undef);
116 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
117 # note this test differs from the Foo case above
118 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
119 'assigning undef to attr2 does not produce an error');
120 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
122 is( exception { $obj = Foo->new(attr3 => undef) }, undef,
123 'assigning undef to attr3 is acceptable');
124 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
128 my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
129 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
130 ok($obj->has_attr1, '...and the predicate returns true as normal');
132 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
133 ok($obj->has_attr2, '...and the predicate returns true as normal');
135 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
136 ok($obj->has_attr3, '...and the predicate returns true as normal');