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');
62 my $obj = Foo->new(attr1 => undef);
63 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
64 isnt (exception { $obj = Foo->new(attr2 => undef) }, undef,
65 'But assigning undef to attr2 generates a type constraint error');
67 is (exception { $obj = Foo->new(attr3 => undef) }, undef,
68 'assigning undef to attr3 is acceptable');
69 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
73 my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
74 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
75 ok($obj->has_attr1, '...and the predicate returns true as normal');
77 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
78 ok($obj->has_attr2, '...and the predicate returns true as normal');
80 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
81 ok($obj->has_attr3, '...and the predicate returns true as normal');
86 note 'Testing class with the entire class being UndefTolerant';
89 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
90 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
91 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
95 my $obj = Bar->new(attr1 => undef);
96 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
97 # note this test differs from the Foo case above
98 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
99 'assigning undef to attr2 does not produce an error');
100 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
102 is( exception { $obj = Foo->new(attr3 => undef) }, undef,
103 'assigning undef to attr3 is acceptable');
104 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
108 my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
109 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
110 ok($obj->has_attr1, '...and the predicate returns true as normal');
112 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
113 ok($obj->has_attr2, '...and the predicate returns true as normal');
115 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
116 ok($obj->has_attr3, '...and the predicate returns true as normal');
121 note 'Constructor behaviour: mutable classes';
126 note 'Constructor behaviour: immutable classes';
128 Foo->meta->make_immutable;
129 Bar->meta->make_immutable;
131 local $TODO = 'some immutable cases are not handled yet';
132 # for now, catch errors
133 is(exception { do_tests }, undef, 'tests do not die');
135 is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!');