5 use MooseX::UndefTolerant::Attribute ();
12 traits => [ qw(MooseX::UndefTolerant::Attribute)],
15 predicate => 'has_attr1',
21 predicate => 'has_attr2',
27 predicate => 'has_attr3',
35 use MooseX::UndefTolerant;
40 predicate => 'has_attr1',
46 predicate => 'has_attr2',
52 predicate => 'has_attr3',
62 note 'Default behaviour: ',
63 (Foo->meta->is_immutable ? 'im' : '') . 'mutable classes', "\n";
65 note 'Testing class with a single UndefTolerant attribute';
66 do_tests_with_class('Foo');
69 note 'Testing class with the entire class being UndefTolerant';
70 do_tests_with_class('Bar');
73 sub do_tests_with_class
78 my $obj = $class->new;
79 ok($obj->has_attr1, 'attr1 has a value');
80 ok($obj->has_attr2, 'attr2 has a value');
81 ok($obj->has_attr3, 'attr3 has a value');
83 is($obj->attr1, 1, 'attr1\'s value is its default');
84 is($obj->attr2, 2, 'attr2\'s value is its default');
85 is($obj->attr3, 3, 'attr3\'s value is its default');
90 my $obj = $class->new(attr1 => undef, attr3 => undef);
92 local $TODO = 'not sure why this fails still... needs attr trait rewrite' if $obj->meta->is_immutable;
93 # FIXME: the object is successfully constructed, and the value
94 # for attr1 is properly removed, but the default is not then
96 # note "### constructed object: ", $obj->dump(2);
97 ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
98 is($obj->attr1, 1, 'attr1\'s value is its default');
100 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
102 is($obj->attr2, 2, 'attr2\'s value is its default');
103 is($obj->attr3, undef, 'attr3\'s value is not its default (explicitly set)');
105 local $TODO = 'some immutable cases are not handled yet; see CAVEATS'
106 if $class->meta->is_immutable and $class eq 'Foo';
108 is($e, undef, 'these tests do not die');
112 my $obj = $class->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
113 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
114 ok($obj->has_attr1, '...and the predicate returns true as normal');
116 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
117 ok($obj->has_attr2, '...and the predicate returns true as normal');
119 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
120 ok($obj->has_attr3, '...and the predicate returns true as normal');
129 local $TODO = 'some cases are still not handled yet; see CAVEATS';
130 is(Test::More->builder->current_test, 98, 'if we got here, we can declare victory!');