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