Commit | Line | Data |
37acc770 |
1 | package # hide from PAUSE |
2 | ConstructorTests; |
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 # hide from PAUSE |
49 | ConstructorTests; |
50 | |
51 | use strict; |
52 | use warnings; |
53 | |
54 | use Test::More; |
55 | use Test::Fatal; |
56 | |
57 | sub do_tests |
58 | { |
4f5c4e45 |
59 | note 'Testing ', (Foo->meta->is_immutable ? 'im' : '') . 'mutable ', |
60 | 'class with a single UndefTolerant attribute'; |
37acc770 |
61 | { |
62 | my $obj = Foo->new; |
63 | ok(!$obj->has_attr1, 'attr1 has no value before it is assigned'); |
64 | ok(!$obj->has_attr2, 'attr2 has no value before it is assigned'); |
65 | ok(!$obj->has_attr3, 'attr3 has no value before it is assigned'); |
66 | } |
67 | |
68 | TODO: { |
69 | local $TODO; |
70 | $TODO = 'some immutable cases are not handled yet; see CAVEATS' if Foo->meta->is_immutable; |
71 | is( |
72 | exception { |
37acc770 |
73 | my $obj = Foo->new(attr1 => undef); |
74 | ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor'); |
75 | like( |
76 | exception { $obj = Foo->new(attr2 => undef) }, |
77 | qr/\QAttribute (attr2) does not pass the type constraint because: Validation failed for 'Num' with value undef\E/, |
78 | 'But assigning undef to attr2 generates a type constraint error'); |
79 | |
80 | is (exception { $obj = Foo->new(attr3 => undef) }, undef, |
81 | 'assigning undef to attr3 is acceptable'); |
82 | ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor'); |
83 | }, |
84 | undef, |
4f5c4e45 |
85 | 'successfully tested spot-application of UT trait in ' |
86 | . (Foo->meta->is_immutable ? 'im' : '') . 'mutable classes', |
37acc770 |
87 | ); |
88 | } |
89 | |
90 | { |
91 | my $obj = Foo->new(attr1 => 1234, attr2 => 5678, attr3 => 9012); |
92 | is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); |
93 | ok($obj->has_attr1, '...and the predicate returns true as normal'); |
94 | |
95 | is($obj->attr2, 5678, 'assigning a defined value during construction works as normal'); |
96 | ok($obj->has_attr2, '...and the predicate returns true as normal'); |
97 | |
98 | is($obj->attr3, 9012, 'assigning a defined value during construction works as normal'); |
99 | ok($obj->has_attr3, '...and the predicate returns true as normal'); |
100 | } |
101 | |
37acc770 |
102 | note ''; |
4f5c4e45 |
103 | note 'Testing class with the entire ', |
104 | (Foo->meta->is_immutable ? 'im' : '') . 'mutable ', |
105 | 'class being UndefTolerant'; |
37acc770 |
106 | { |
107 | my $obj = Bar->new; |
108 | ok(!$obj->has_attr1, 'attr1 has no value before it is assigned'); |
109 | ok(!$obj->has_attr2, 'attr2 has no value before it is assigned'); |
110 | ok(!$obj->has_attr3, 'attr3 has no value before it is assigned'); |
111 | } |
112 | |
113 | { |
114 | my $obj = Bar->new(attr1 => undef); |
115 | ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor'); |
116 | # note this test differs from the Foo case above |
117 | is (exception { $obj = Bar->new(attr2 => undef) }, undef, |
118 | 'assigning undef to attr2 does not produce an error'); |
119 | ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor'); |
120 | |
121 | is( exception { $obj = Foo->new(attr3 => undef) }, undef, |
122 | 'assigning undef to attr3 is acceptable'); |
123 | ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor'); |
124 | } |
125 | |
126 | { |
127 | my $obj = Bar->new(attr1 => 1234, attr2 => 5678, attr3 => 9012); |
128 | is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); |
129 | ok($obj->has_attr1, '...and the predicate returns true as normal'); |
130 | |
131 | is($obj->attr2, 5678, 'assigning a defined value during construction works as normal'); |
132 | ok($obj->has_attr2, '...and the predicate returns true as normal'); |
133 | |
134 | is($obj->attr3, 9012, 'assigning a defined value during construction works as normal'); |
135 | ok($obj->has_attr3, '...and the predicate returns true as normal'); |
136 | } |
137 | } |
138 | |
139 | 1; |