a4b1c6e3cc9e413cfad1dd6ddef1cc5f563284af
[gitmo/Moose.git] / t / 040_type_constraints / 023_types_and_undef.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 55;
7 use Test::Exception;
8 use Test::Deep;
9
10 BEGIN
11 {
12     use_ok('Moose');
13 }
14
15 #    A MOOSE OBJECT
16 #
17 {
18     package Foo;
19     use Moose;
20     use Moose::Util::TypeConstraints;
21
22     use Scalar::Util ();
23
24     type Number
25         => where { defined($_) && !ref($_) && Scalar::Util::looks_like_number($_) };
26
27     type String
28         => where { defined($_) && !ref($_) && !Scalar::Util::looks_like_number($_) };
29
30     has vUndef   => ( is => 'rw', isa => 'Undef'   );
31     has vDefined => ( is => 'rw', isa => 'Defined' );
32     has vInt     => ( is => 'rw', isa => 'Int'     );
33     has vNumber  => ( is => 'rw', isa => 'Number'  );
34     has vStr     => ( is => 'rw', isa => 'Str'     );
35     has vString  => ( is => 'rw', isa => 'String'  );
36
37     has v_lazy_Undef   => ( is => 'rw', lazy => 1,  default => sub { undef }, isa => 'Undef'   );
38     has v_lazy_Defined => ( is => 'rw', lazy => 1,  default => sub { undef }, isa => 'Defined' );
39     has v_lazy_Int     => ( is => 'rw', lazy => 1,  default => sub { undef }, isa => 'Int'     );
40     has v_lazy_Number  => ( is => 'rw', lazy => 1,  default => sub { undef }, isa => 'Number'  );
41     has v_lazy_Str     => ( is => 'rw', lazy => 1,  default => sub { undef }, isa => 'Str'     );
42     has v_lazy_String  => ( is => 'rw', lazy => 1,  default => sub { undef }, isa => 'String'  );
43 }
44
45 #    EXPORT TYPE CONSTRAINTS
46 #
47 Moose::Util::TypeConstraints->export_type_constraints_as_functions;
48
49 ok( Undef(undef),   '... undef is a Undef');
50 ok(!Defined(undef), '... undef is NOT a Defined');
51 ok(!Int(undef),     '... undef is NOT a Int');
52 ok(!Number(undef),  '... undef is NOT a Number');
53 ok(!Str(undef),     '... undef is NOT a Str');
54 ok(!String(undef),  '... undef is NOT a String');
55     
56 ok(!Undef(5),  '... 5 is a NOT a Undef');
57 ok(Defined(5), '... 5 is a Defined');
58 ok(Int(5),     '... 5 is a Int');
59 ok(Number(5),  '... 5 is a Number');
60 ok(Str(5),     '... 5 is a Str');   
61 ok(!String(5), '... 5 is NOT a String');
62     
63 ok(!Undef(0.5),  '... 0.5 is a NOT a Undef');
64 ok(Defined(0.5), '... 0.5 is a Defined');
65 ok(!Int(0.5),    '... 0.5 is NOT a Int');
66 ok(Number(0.5),  '... 0.5 is a Number');
67 ok(Str(0.5),     '... 0.5 is a Str');
68 ok(!String(0.5), '... 0.5 is NOT a String');
69     
70 ok(!Undef('Foo'),  '... "Foo" is NOT a Undef');
71 ok(Defined('Foo'), '... "Foo" is a Defined');
72 ok(!Int('Foo'),    '... "Foo" is NOT a Int');
73 ok(!Number('Foo'), '... "Foo" is NOT a Number');
74 ok(Str('Foo'),     '... "Foo" is a Str');
75 ok(String('Foo'),  '... "Foo" is a String');
76
77
78 my $foo = Foo->new;
79
80 lives_ok { $foo->vUndef(undef) } '... undef is a Foo->Undef';
81 dies_ok { $foo->vDefined(undef) } '... undef is NOT a Foo->Defined';
82 dies_ok { $foo->vInt(undef) } '... undef is NOT a Foo->Int';        
83 dies_ok { $foo->vNumber(undef) } '... undef is NOT a Foo->Number';  
84 dies_ok { $foo->vStr(undef) } '... undef is NOT a Foo->Str';        
85 dies_ok { $foo->vString(undef) } '... undef is NOT a Foo->String';  
86
87 dies_ok { $foo->vUndef(5) } '... 5 is NOT a Foo->Undef';
88 lives_ok { $foo->vDefined(5) } '... 5 is a Foo->Defined';
89 lives_ok { $foo->vInt(5) } '... 5 is a Foo->Int';
90 lives_ok { $foo->vNumber(5) } '... 5 is a Foo->Number';
91 lives_ok { $foo->vStr(5) } '... 5 is a Foo->Str';   
92 dies_ok { $foo->vString(5) } '... 5 is NOT a Foo->String';
93
94 dies_ok { $foo->vUndef(0.5) } '... 0.5 is NOT a Foo->Undef';
95 lives_ok { $foo->vDefined(0.5) } '... 0.5 is a Foo->Defined';
96 dies_ok { $foo->vInt(0.5) } '... 0.5 is NOT a Foo->Int';
97 lives_ok { $foo->vNumber(0.5) } '... 0.5 is a Foo->Number';
98 lives_ok { $foo->vStr(0.5) } '... 0.5 is a Foo->Str';
99 dies_ok { $foo->vString(0.5) } '... 0.5 is NOT a Foo->String';
100
101 dies_ok { $foo->vUndef('Foo') } '... "Foo" is NOT a Foo->Undef';
102 lives_ok { $foo->vDefined('Foo') } '... "Foo" is a Foo->Defined';
103 dies_ok { $foo->vInt('Foo') } '... "Foo" is NOT a Foo->Int';
104 dies_ok { $foo->vNumber('Foo') } '... "Foo" is NOT a Foo->Number';
105 lives_ok { $foo->vStr('Foo') } '... "Foo" is a Foo->Str';
106 lives_ok { $foo->vString('Foo') } '... "Foo" is a Foo->String';
107
108 # the lazy tests 
109
110 lives_ok { $foo->v_lazy_Undef() } '... undef is a Foo->Undef';
111 dies_ok { $foo->v_lazy_Defined() } '... undef is NOT a Foo->Defined';
112 dies_ok { $foo->v_lazy_Int() } '... undef is NOT a Foo->Int';        
113 dies_ok { $foo->v_lazy_Number() } '... undef is NOT a Foo->Number';  
114 dies_ok { $foo->v_lazy_Str() } '... undef is NOT a Foo->Str';        
115 dies_ok { $foo->v_lazy_String() } '... undef is NOT a Foo->String';  
116
117
118
119