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