Commit | Line | Data |
0dd9c65b |
1 | use Test::More; |
8055a641 |
2 | use Test::Fatal; |
779ca481 |
3 | |
36bf5c4d |
4 | use MooseX::UndefTolerant::Attribute (); |
779ca481 |
5 | |
36bf5c4d |
6 | { |
7 | package Foo; |
8 | use Moose; |
779ca481 |
9 | |
36bf5c4d |
10 | has 'attr1' => ( |
11 | traits => [ qw(MooseX::UndefTolerant::Attribute)], |
12 | is => 'ro', |
13 | isa => 'Num', |
14 | predicate => 'has_attr1', |
15 | default => 1, |
16 | ); |
17 | has 'attr2' => ( |
18 | is => 'ro', |
19 | isa => 'Num', |
20 | predicate => 'has_attr2', |
21 | default => 2, |
22 | ); |
3991a5d2 |
23 | has 'attr3' => ( |
24 | is => 'ro', |
25 | isa => 'Maybe[Num]', |
26 | predicate => 'has_attr3', |
27 | default => 3, |
28 | ); |
36bf5c4d |
29 | } |
779ca481 |
30 | |
36bf5c4d |
31 | { |
32 | package Bar; |
33 | use Moose; |
34 | use MooseX::UndefTolerant; |
35 | |
36 | has 'attr1' => ( |
37 | is => 'ro', |
38 | isa => 'Num', |
39 | predicate => 'has_attr1', |
40 | default => 1, |
41 | ); |
42 | has 'attr2' => ( |
43 | is => 'ro', |
44 | isa => 'Num', |
45 | predicate => 'has_attr2', |
46 | default => 2, |
47 | ); |
3991a5d2 |
48 | has 'attr3' => ( |
49 | is => 'ro', |
50 | isa => 'Maybe[Num]', |
51 | predicate => 'has_attr3', |
52 | default => 3, |
53 | ); |
36bf5c4d |
54 | } |
779ca481 |
55 | |
779ca481 |
56 | |
57 | package main; |
58 | |
8055a641 |
59 | sub do_tests |
36bf5c4d |
60 | { |
8055a641 |
61 | note 'Testing class with a single UndefTolerant attribute'; |
581c4a18 |
62 | do_tests_with_class('Foo'); |
8055a641 |
63 | |
64 | note ''; |
65 | note 'Testing class with the entire class being UndefTolerant'; |
581c4a18 |
66 | do_tests_with_class('Bar'); |
67 | } |
68 | |
69 | sub do_tests_with_class |
70 | { |
71 | my $class = shift; |
72 | |
8055a641 |
73 | { |
581c4a18 |
74 | my $obj = $class->new; |
8055a641 |
75 | ok($obj->has_attr1, 'attr1 has a value'); |
76 | ok($obj->has_attr2, 'attr2 has a value'); |
3991a5d2 |
77 | ok($obj->has_attr3, 'attr3 has a value'); |
78 | |
8055a641 |
79 | is($obj->attr1, 1, 'attr1\'s value is its default'); |
80 | is($obj->attr2, 2, 'attr2\'s value is its default'); |
3991a5d2 |
81 | is($obj->attr3, 3, 'attr3\'s value is its default'); |
8055a641 |
82 | } |
83 | |
84 | { |
3991a5d2 |
85 | my $obj = $class->new(attr1 => undef, attr3 => undef); |
8055a641 |
86 | ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor'); |
3991a5d2 |
87 | ok($obj->has_attr3, 'attr3 retains its undef value when assigned undef in constructor'); |
88 | |
8055a641 |
89 | is($obj->attr1, 1, 'attr1\'s value is its default'); |
90 | is($obj->attr2, 2, 'attr2\'s value is its default'); |
3991a5d2 |
91 | is($obj->attr3, undef, 'attr3\'s value is not its default (explicitly set)'); |
8055a641 |
92 | } |
93 | |
94 | { |
3991a5d2 |
95 | my $obj = $class->new(attr1 => 1234, attr2 => 5678, attr3 => 9012); |
8055a641 |
96 | is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); |
97 | ok($obj->has_attr1, '...and the predicate returns true as normal'); |
3991a5d2 |
98 | |
8055a641 |
99 | is($obj->attr2, 5678, 'assigning a defined value during construction works as normal'); |
100 | ok($obj->has_attr2, '...and the predicate returns true as normal'); |
3991a5d2 |
101 | |
102 | is($obj->attr3, 9012, 'assigning a defined value during construction works as normal'); |
103 | ok($obj->has_attr3, '...and the predicate returns true as normal'); |
8055a641 |
104 | } |
36bf5c4d |
105 | } |
106 | |
8055a641 |
107 | note 'Default behaviour: mutable classes'; |
108 | note ''; |
109 | do_tests; |
36bf5c4d |
110 | |
111 | note ''; |
8055a641 |
112 | note 'Default behaviour: immutable classes'; |
113 | note ''; |
114 | Foo->meta->make_immutable; |
115 | Bar->meta->make_immutable; |
36bf5c4d |
116 | |
8055a641 |
117 | TODO: { |
118 | local $TODO = 'some immutable cases are not handled yet'; |
119 | # for now, catch errors |
015b9167 |
120 | is (exception { do_tests }, undef, 'tests do not die'); |
36bf5c4d |
121 | |
8055a641 |
122 | is(Test::More->builder->current_test, 44, 'if we got here, we can declare victory!'); |
36bf5c4d |
123 | } |
779ca481 |
124 | |
8055a641 |
125 | done_testing; |
126 | |