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 'Testing class with a single UndefTolerant attribute';
63 do_tests_with_class('Foo');
66 note 'Testing class with the entire class being UndefTolerant';
67 do_tests_with_class('Bar');
70 sub do_tests_with_class
75 my $obj = $class->new;
76 ok($obj->has_attr1, 'attr1 has a value');
77 ok($obj->has_attr2, 'attr2 has a value');
78 ok($obj->has_attr3, 'attr3 has a value');
80 is($obj->attr1, 1, 'attr1\'s value is its default');
81 is($obj->attr2, 2, 'attr2\'s value is its default');
82 is($obj->attr3, 3, 'attr3\'s value is its default');
86 my $obj = $class->new(attr1 => undef, attr3 => undef);
87 ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
88 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
90 is($obj->attr1, 1, 'attr1\'s value is its default');
91 is($obj->attr2, 2, 'attr2\'s value is its default');
92 is($obj->attr3, undef, 'attr3\'s value is not its default (explicitly set)');
96 my $obj = $class->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
97 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
98 ok($obj->has_attr1, '...and the predicate returns true as normal');
100 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
101 ok($obj->has_attr2, '...and the predicate returns true as normal');
103 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
104 ok($obj->has_attr3, '...and the predicate returns true as normal');
108 note 'Default behaviour: mutable classes';
113 note 'Default behaviour: immutable classes';
115 Foo->meta->make_immutable;
116 Bar->meta->make_immutable;
119 local $TODO = 'some immutable cases are not handled yet';
120 # for now, catch errors
121 is (exception { do_tests }, undef, 'tests do not die');
123 is(Test::More->builder->current_test, 44, 'if we got here, we can declare victory!');