do not use Undef-Tolerant behaviour on attributes that are capable of handling undef
[gitmo/MooseX-UndefTolerant.git] / t / constructor.t
CommitLineData
3991a5d2 1#use Test::More;
2use Test::Most 'die';
5cc8d5b3 3use Test::Fatal;
5447ee45 4
5cc8d5b3 5{
6 package Foo;
7 use Moose;
8
9 has 'attr1' => (
10 traits => [ qw(MooseX::UndefTolerant::Attribute)],
11 is => 'ro',
12 isa => 'Num',
36bf5c4d 13 predicate => 'has_attr1',
5cc8d5b3 14 );
5cc8d5b3 15 has 'attr2' => (
16 is => 'ro',
17 isa => 'Num',
36bf5c4d 18 predicate => 'has_attr2',
5cc8d5b3 19 );
3991a5d2 20 has 'attr3' => (
21 is => 'ro',
22 isa => 'Maybe[Num]',
23 predicate => 'has_attr3',
24 );
5cc8d5b3 25}
5447ee45 26
5cc8d5b3 27{
28 package Bar;
29 use Moose;
30 use MooseX::UndefTolerant;
31
32 has 'attr1' => (
33 is => 'ro',
34 isa => 'Num',
36bf5c4d 35 predicate => 'has_attr1',
5cc8d5b3 36 );
37 has 'attr2' => (
38 is => 'ro',
39 isa => 'Num',
36bf5c4d 40 predicate => 'has_attr2',
5cc8d5b3 41 );
3991a5d2 42 has 'attr3' => (
43 is => 'ro',
44 isa => 'Maybe[Num]',
45 predicate => 'has_attr3',
46 );
5cc8d5b3 47}
5447ee45 48
49package main;
50
8055a641 51sub do_tests
5447ee45 52{
8055a641 53 note 'Testing class with a single UndefTolerant attribute';
54 {
55 my $obj = Foo->new;
56 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
57 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
3991a5d2 58 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
8055a641 59 }
60
61 {
62 my $obj = Foo->new(attr1 => undef);
63 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
3991a5d2 64 isnt (exception { $obj = Foo->new(attr2 => undef) }, undef,
8055a641 65 'But assigning undef to attr2 generates a type constraint error');
3991a5d2 66
67 is (exception { $obj = Foo->new(attr3 => undef) }, undef,
68 'assigning undef to attr3 is acceptable');
69 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
8055a641 70 }
71
72 {
3991a5d2 73 my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
8055a641 74 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
75 ok($obj->has_attr1, '...and the predicate returns true as normal');
3991a5d2 76
8055a641 77 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
78 ok($obj->has_attr2, '...and the predicate returns true as normal');
3991a5d2 79
80 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
81 ok($obj->has_attr3, '...and the predicate returns true as normal');
8055a641 82 }
83
84
85 note '';
86 note 'Testing class with the entire class being UndefTolerant';
87 {
88 my $obj = Bar->new;
89 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
3991a5d2 90 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
91 ok(!$obj->has_attr3, 'attr3 has no value before it is assigned');
8055a641 92 }
93
94 {
95 my $obj = Bar->new(attr1 => undef);
96 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
3991a5d2 97 # note this test differs from the Foo case above
015b9167 98 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
8055a641 99 'assigning undef to attr2 does not produce an error');
100 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
3991a5d2 101
102 is( exception { $obj = Foo->new(attr3 => undef) }, undef,
103 'assigning undef to attr3 is acceptable');
104 ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor');
8055a641 105 }
106
107 {
3991a5d2 108 my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012);
8055a641 109 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
110 ok($obj->has_attr1, '...and the predicate returns true as normal');
3991a5d2 111
112 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
113 ok($obj->has_attr2, '...and the predicate returns true as normal');
114
115 is($obj->attr3, 9012, 'assigning a defined value during construction works as normal');
116 ok($obj->has_attr3, '...and the predicate returns true as normal');
8055a641 117 }
5447ee45 118}
119
5cc8d5b3 120
8055a641 121note 'Constructor behaviour: mutable classes';
5cc8d5b3 122note '';
8055a641 123do_tests;
5447ee45 124
8055a641 125note '';
126note 'Constructor behaviour: immutable classes';
127note '';
128Foo->meta->make_immutable;
129Bar->meta->make_immutable;
130TODO: {
131 local $TODO = 'some immutable cases are not handled yet';
132 # for now, catch errors
015b9167 133 is(exception { do_tests }, undef, 'tests do not die');
8055a641 134
135 is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!');
5cc8d5b3 136}
137
8055a641 138done_testing;
5cc8d5b3 139