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