Commit | Line | Data |
8055a641 |
1 | use Test::More; |
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 | ); |
23 | } |
779ca481 |
24 | |
36bf5c4d |
25 | { |
26 | package Bar; |
27 | use Moose; |
28 | use MooseX::UndefTolerant; |
29 | |
30 | has 'attr1' => ( |
31 | is => 'ro', |
32 | isa => 'Num', |
33 | predicate => 'has_attr1', |
34 | default => 1, |
35 | ); |
36 | has 'attr2' => ( |
37 | is => 'ro', |
38 | isa => 'Num', |
39 | predicate => 'has_attr2', |
40 | default => 2, |
41 | ); |
42 | } |
779ca481 |
43 | |
779ca481 |
44 | |
45 | package main; |
46 | |
8055a641 |
47 | sub do_tests |
36bf5c4d |
48 | { |
8055a641 |
49 | note 'Testing class with a single UndefTolerant attribute'; |
581c4a18 |
50 | do_tests_with_class('Foo'); |
8055a641 |
51 | |
52 | note ''; |
53 | note 'Testing class with the entire class being UndefTolerant'; |
581c4a18 |
54 | do_tests_with_class('Bar'); |
55 | } |
56 | |
57 | sub do_tests_with_class |
58 | { |
59 | my $class = shift; |
60 | |
8055a641 |
61 | { |
581c4a18 |
62 | my $obj = $class->new; |
8055a641 |
63 | ok($obj->has_attr1, 'attr1 has a value'); |
64 | ok($obj->has_attr2, 'attr2 has a value'); |
65 | is($obj->attr1, 1, 'attr1\'s value is its default'); |
66 | is($obj->attr2, 2, 'attr2\'s value is its default'); |
67 | } |
68 | |
69 | { |
581c4a18 |
70 | my $obj = $class->new(attr1 => undef); |
8055a641 |
71 | ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor'); |
72 | is($obj->attr1, 1, 'attr1\'s value is its default'); |
73 | is($obj->attr2, 2, 'attr2\'s value is its default'); |
74 | } |
75 | |
76 | { |
581c4a18 |
77 | my $obj = $class->new(attr1 => 1234, attr2 => 5678); |
8055a641 |
78 | is($obj->attr1, 1234, 'assigning a defined value during construction works as normal'); |
79 | ok($obj->has_attr1, '...and the predicate returns true as normal'); |
80 | is($obj->attr2, 5678, 'assigning a defined value during construction works as normal'); |
81 | ok($obj->has_attr2, '...and the predicate returns true as normal'); |
82 | } |
36bf5c4d |
83 | } |
84 | |
8055a641 |
85 | note 'Default behaviour: mutable classes'; |
86 | note ''; |
87 | do_tests; |
36bf5c4d |
88 | |
89 | note ''; |
8055a641 |
90 | note 'Default behaviour: immutable classes'; |
91 | note ''; |
92 | Foo->meta->make_immutable; |
93 | Bar->meta->make_immutable; |
36bf5c4d |
94 | |
8055a641 |
95 | TODO: { |
96 | local $TODO = 'some immutable cases are not handled yet'; |
97 | # for now, catch errors |
98 | ok(! exception { do_tests }, 'tests do not die'); |
36bf5c4d |
99 | |
8055a641 |
100 | is(Test::More->builder->current_test, 44, 'if we got here, we can declare victory!'); |
36bf5c4d |
101 | } |
779ca481 |
102 | |
8055a641 |
103 | done_testing; |
104 | |