1 use Test::More tests => 14;
4 # TODO: this test should be renamed constructor.t, since all it tests is
5 # UT behaviour during construction.
12 traits => [ qw(MooseX::UndefTolerant::Attribute)],
15 predicate => 'has_attr1',
21 predicate => 'has_attr2',
28 use MooseX::UndefTolerant;
33 predicate => 'has_attr1',
38 predicate => 'has_attr2',
44 note 'Constructor behaviour';
47 note 'Testing class with a single UndefTolerant attribute';
50 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
51 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
55 my $obj = Foo->new(attr1 => undef);
56 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
57 ok (exception { $obj = Foo->new(attr2 => undef) },
58 'But assigning undef to attr2 generates a type constraint error');
62 my $obj = Foo->new(attr1 => 1234, attr2 => 5678);
63 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
64 ok($obj->has_attr1, '...and the predicate returns true as normal');
65 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
66 ok($obj->has_attr2, '...and the predicate returns true as normal');
71 note 'Testing class with the entire class being UndefTolerant';
74 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
78 my $obj = Bar->new(attr1 => undef);
79 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
80 ok (!exception { $obj = Bar->new(attr2 => undef) },
81 'assigning undef to attr2 does not produce an error');
82 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
86 my $obj = Bar->new(attr1 => 1234);
87 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
88 ok($obj->has_attr1, '...and the predicate returns true as normal');