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 class with a single UndefTolerant attribute';
62 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
63 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
64 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
69 $TODO = 'some immutable cases are not handled yet; see CAVEATS' if Foo->meta->is_immutable;
73 my $obj = Foo->new(attr1 => undef);
74 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
76 exception { $obj = Foo->new(attr2 => undef) },
77 qr/\QAttribute (attr2) does not pass the type constraint because: Validation failed for 'Num' with value undef\E/,
78 'But assigning undef to attr2 generates a type constraint error');
80 is (exception { $obj = Foo->new(attr3 => undef) }, undef,
81 'assigning undef to attr3 is acceptable');
82 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
85 'successfully tested spot-applicaction of UT trait in immutable classes',
90 my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
91 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
92 ok($obj->has_attr1, '...and the predicate returns true as normal');
94 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
95 ok($obj->has_attr2, '...and the predicate returns true as normal');
97 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
98 ok($obj->has_attr3, '...and the predicate returns true as normal');
103 note 'Testing class with the entire class being UndefTolerant';
106 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
107 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
108 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
112 my $obj = Bar->new(attr1 => undef);
113 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
114 # note this test differs from the Foo case above
115 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
116 'assigning undef to attr2 does not produce an error');
117 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
119 is( exception { $obj = Foo->new(attr3 => undef) }, undef,
120 'assigning undef to attr3 is acceptable');
121 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
125 my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
126 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
127 ok($obj->has_attr1, '...and the predicate returns true as normal');
129 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
130 ok($obj->has_attr2, '...and the predicate returns true as normal');
132 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
133 ok($obj->has_attr3, '...and the predicate returns true as normal');