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