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