new tests for default behaviour, using classes in first test file
[gitmo/MooseX-UndefTolerant.git] / t / defaults.t
1 use Test::More tests => 22;
2
3 use MooseX::UndefTolerant::Attribute ();
4
5 {
6     package Foo;
7     use Moose;
8
9     has 'attr1' => (
10         traits => [ qw(MooseX::UndefTolerant::Attribute)],
11         is => 'ro',
12         isa => 'Num',
13         predicate => 'has_attr1',
14         default => 1,
15     );
16     has 'attr2' => (
17         is => 'ro',
18         isa => 'Num',
19         predicate => 'has_attr2',
20         default => 2,
21     );
22 }
23
24 {
25     package Bar;
26     use Moose;
27     use MooseX::UndefTolerant;
28
29     has 'attr1' => (
30         is => 'ro',
31         isa => 'Num',
32         predicate => 'has_attr1',
33         default => 1,
34     );
35     has 'attr2' => (
36         is => 'ro',
37         isa => 'Num',
38         predicate => 'has_attr2',
39         default => 2,
40     );
41 }
42
43
44 package main;
45
46 note 'Default behaviour';
47 note '';
48
49 note 'Testing class with a single UndefTolerant attribute';
50 {
51     my $obj = Foo->new;
52     ok($obj->has_attr1, 'attr1 has a value');
53     ok($obj->has_attr2, 'attr2 has a value');
54     is($obj->attr1, 1, 'attr1\'s value is its default');
55     is($obj->attr2, 2, 'attr2\'s value is its default');
56 }
57
58 {
59     my $obj = Foo->new(attr1 => undef);
60     ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
61     is($obj->attr1, 1, 'attr1\'s value is its default');
62     is($obj->attr2, 2, 'attr2\'s value is its default');
63 }
64
65 {
66     my $obj = Foo->new(attr1 => 1234, attr2 => 5678);
67     is($obj->attr1, 1234, 'assigning a defined value during construction works as normal');
68     ok($obj->has_attr1, '...and the predicate returns true as normal');
69     is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
70     ok($obj->has_attr2, '...and the predicate returns true as normal');
71 }
72
73
74 note '';
75 note 'Testing class with the entire class being UndefTolerant';
76 {
77     my $obj = Bar->new;
78     ok($obj->has_attr1, 'attr1 has a value');
79     ok($obj->has_attr2, 'attr2 has a value');
80     is($obj->attr1, 1, 'attr1\'s value is its default');
81     is($obj->attr2, 2, 'attr2\'s value is its default');
82 }
83
84 {
85     my $obj = Bar->new(attr1 => undef);
86     ok($obj->has_attr1, 'UT attr1 has a value when assigned undef in constructor');
87     is($obj->attr1, 1, 'attr1\'s value is its default');
88     is($obj->attr2, 2, 'attr2\'s value is its default');
89 }
90
91 {
92     my $obj = Bar->new(attr1 => 1234, attr2 => 5678);
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     is($obj->attr2, 5678, 'assigning a defined value during construction works as normal');
96     ok($obj->has_attr2, '...and the predicate returns true as normal');
97 }
98