better Test::Fatal tests
[gitmo/MooseX-UndefTolerant.git] / t / constructor.t
CommitLineData
8055a641 1use Test::More;
5cc8d5b3 2use 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 );
5447ee45 14
5cc8d5b3 15 has 'attr2' => (
16 is => 'ro',
17 isa => 'Num',
36bf5c4d 18 predicate => 'has_attr2',
5cc8d5b3 19 );
20}
5447ee45 21
5cc8d5b3 22{
23 package Bar;
24 use Moose;
25 use MooseX::UndefTolerant;
26
27 has 'attr1' => (
28 is => 'ro',
29 isa => 'Num',
36bf5c4d 30 predicate => 'has_attr1',
5cc8d5b3 31 );
32 has 'attr2' => (
33 is => 'ro',
34 isa => 'Num',
36bf5c4d 35 predicate => 'has_attr2',
5cc8d5b3 36 );
37}
5447ee45 38
39package main;
40
8055a641 41sub do_tests
5447ee45 42{
8055a641 43 note 'Testing class with a single UndefTolerant attribute';
44 {
45 my $obj = Foo->new;
46 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
47 ok(!$obj->has_attr2, 'attr2 has no value before it is assigned');
48 }
49
50 {
51 my $obj = Foo->new(attr1 => undef);
52 ok(!$obj->has_attr1, 'UT attr1 has no value when assigned undef in constructor');
015b9167 53 is (exception { $obj = Foo->new(attr2 => undef) }, undef,
8055a641 54 'But assigning undef to attr2 generates a type constraint error');
55 }
56
57 {
58 my $obj = Foo->new(attr1 => 1234, attr2 => 5678);
59 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
60 ok($obj->has_attr1, '...and the predicate returns true as normal');
61 is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
62 ok($obj->has_attr2, '...and the predicate returns true as normal');
63 }
64
65
66 note '';
67 note 'Testing class with the entire class being UndefTolerant';
68 {
69 my $obj = Bar->new;
70 ok(!$obj->has_attr1, 'attr1 has no value before it is assigned');
71 }
72
73 {
74 my $obj = Bar->new(attr1 => undef);
75 ok(!$obj->has_attr1, 'attr1 has no value when assigned undef in constructor');
015b9167 76 is (exception { $obj = Bar->new(attr2 => undef) }, undef,
8055a641 77 'assigning undef to attr2 does not produce an error');
78 ok(!$obj->has_attr2, 'attr2 has no value when assigned undef in constructor');
79 }
80
81 {
82 my $obj = Bar->new(attr1 => 1234);
83 is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
84 ok($obj->has_attr1, '...and the predicate returns true as normal');
85 }
5447ee45 86}
87
5cc8d5b3 88
8055a641 89note 'Constructor behaviour: mutable classes';
5cc8d5b3 90note '';
8055a641 91do_tests;
5447ee45 92
8055a641 93note '';
94note 'Constructor behaviour: immutable classes';
95note '';
96Foo->meta->make_immutable;
97Bar->meta->make_immutable;
98TODO: {
99 local $TODO = 'some immutable cases are not handled yet';
100 # for now, catch errors
015b9167 101 is(exception { do_tests }, undef, 'tests do not die');
8055a641 102
103 is(Test::More->builder->current_test, 28, 'if we got here, we can declare victory!');
5cc8d5b3 104}
105
8055a641 106done_testing;
5cc8d5b3 107