4 use MooseX::UndefTolerant::Attribute ();
11 traits => [ qw(MooseX::UndefTolerant::Attribute)],
14 predicate => 'has_attr1',
20 predicate => 'has_attr2',
26 predicate => 'has_attr3',
34 use MooseX::UndefTolerant;
39 predicate => 'has_attr1',
45 predicate => 'has_attr2',
51 predicate => 'has_attr3',
61 note 'Testing class with a single UndefTolerant attribute';
62 do_tests_with_class('Foo');
65 note 'Testing class with the entire class being UndefTolerant';
66 do_tests_with_class('Bar');
69 sub do_tests_with_class
74 my $obj = $class->new;
75 ok($obj->has_attr1, 'attr1 has a value');
76 ok($obj->has_attr2, 'attr2 has a value');
77 ok($obj->has_attr3, 'attr3 has a value');
79 is($obj->attr1, 1, 'attr1\'s value is its default');
80 is($obj->attr2, 2, 'attr2\'s value is its default');
81 is($obj->attr3, 3, 'attr3\'s value is its default');
85 my $obj = $class->new(attr1 => undef, attr3 => undef);
86 ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
87 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
89 is($obj->attr1, 1, 'attr1\'s value is its default');
90 is($obj->attr2, 2, 'attr2\'s value is its default');
91 is($obj->attr3, undef, 'attr3\'s value is not its default (explicitly set)');
95 my $obj = $class->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
96 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
97 ok($obj->has_attr1, '...and the predicate returns true as normal');
99 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
100 ok($obj->has_attr2, '...and the predicate returns true as normal');
102 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
103 ok($obj->has_attr3, '...and the predicate returns true as normal');
107 note 'Default behaviour: mutable classes';
112 note 'Default behaviour: immutable classes';
114 Foo->meta->make_immutable;
115 Bar->meta->make_immutable;
118 local $TODO = 'some immutable cases are not handled yet';
119 # for now, catch errors
120 is (exception { do_tests }, undef, 'tests do not die');
122 is(Test::More->builder->current_test, 44, 'if we got here, we can declare victory!');