8 use MooseX::UndefTolerant::Attribute ();
15 traits => [ qw(MooseX::UndefTolerant::Attribute)],
18 predicate => 'has_attr1',
24 predicate => 'has_attr2',
30 predicate => 'has_attr3',
38 use MooseX::UndefTolerant;
43 predicate => 'has_attr1',
49 predicate => 'has_attr2',
55 predicate => 'has_attr3',
65 note 'Default behaviour: ',
66 (Foo->meta->is_immutable ? 'im' : '') . 'mutable classes', "\n";
68 note 'Testing class with a single UndefTolerant attribute';
69 do_tests_with_class('Foo');
72 note 'Testing class with the entire class being UndefTolerant';
73 do_tests_with_class('Bar');
76 sub do_tests_with_class
81 my $obj = $class->new;
82 ok($obj->has_attr1, 'attr1 has a value');
83 ok($obj->has_attr2, 'attr2 has a value');
84 ok($obj->has_attr3, 'attr3 has a value');
86 is($obj->attr1, 1, 'attr1\'s value is its default');
87 is($obj->attr2, 2, 'attr2\'s value is its default');
88 is($obj->attr3, 3, 'attr3\'s value is its default');
93 my $obj = $class->new(attr1 => undef, attr3 => undef);
95 local $TODO = 'not sure why this fails still... needs attr trait rewrite' if $obj->meta->is_immutable;
96 # FIXME: the object is successfully constructed, and the value
97 # for attr1 is properly removed, but the default is not then
99 # note "### constructed object: ", $obj->dump(2);
100 ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
101 is($obj->attr1, 1, 'attr1\'s value is its default');
103 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
105 is($obj->attr2, 2, 'attr2\'s value is its default');
106 is($obj->attr3, undef, 'attr3\'s value is not its default (explicitly set)');
108 local $TODO = 'some immutable cases are not handled yet; see CAVEATS'
109 if $class->meta->is_immutable and $class eq 'Foo';
111 is($e, undef, 'these tests do not die');
115 my $obj = $class->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
116 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
117 ok($obj->has_attr1, '...and the predicate returns true as normal');
119 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
120 ok($obj->has_attr2, '...and the predicate returns true as normal');
122 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
123 ok($obj->has_attr3, '...and the predicate returns true as normal');
132 local $TODO = 'some cases are still not handled yet; see CAVEATS';
133 is(Test::More->builder->current_test, 98, 'if we got here, we can declare victory!');