0.37
[gitmo/Moose.git] / t / 040_type_constraints / 023_types_and_undef.t
CommitLineData
ab76842e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 55;
7use Test::Exception;
ab76842e 8
9BEGIN
10{
11 use_ok('Moose');
12}
13
ab76842e 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#
44Moose::Util::TypeConstraints->export_type_constraints_as_functions;
45
46ok( Undef(undef), '... undef is a Undef');
47ok(!Defined(undef), '... undef is NOT a Defined');
48ok(!Int(undef), '... undef is NOT a Int');
49ok(!Number(undef), '... undef is NOT a Number');
50ok(!Str(undef), '... undef is NOT a Str');
51ok(!String(undef), '... undef is NOT a String');
52
53ok(!Undef(5), '... 5 is a NOT a Undef');
54ok(Defined(5), '... 5 is a Defined');
55ok(Int(5), '... 5 is a Int');
56ok(Number(5), '... 5 is a Number');
57ok(Str(5), '... 5 is a Str');
58ok(!String(5), '... 5 is NOT a String');
59
60ok(!Undef(0.5), '... 0.5 is a NOT a Undef');
61ok(Defined(0.5), '... 0.5 is a Defined');
62ok(!Int(0.5), '... 0.5 is NOT a Int');
63ok(Number(0.5), '... 0.5 is a Number');
64ok(Str(0.5), '... 0.5 is a Str');
65ok(!String(0.5), '... 0.5 is NOT a String');
66
67ok(!Undef('Foo'), '... "Foo" is NOT a Undef');
68ok(Defined('Foo'), '... "Foo" is a Defined');
69ok(!Int('Foo'), '... "Foo" is NOT a Int');
70ok(!Number('Foo'), '... "Foo" is NOT a Number');
71ok(Str('Foo'), '... "Foo" is a Str');
72ok(String('Foo'), '... "Foo" is a String');
73
74
75my $foo = Foo->new;
76
77lives_ok { $foo->vUndef(undef) } '... undef is a Foo->Undef';
78dies_ok { $foo->vDefined(undef) } '... undef is NOT a Foo->Defined';
79dies_ok { $foo->vInt(undef) } '... undef is NOT a Foo->Int';
80dies_ok { $foo->vNumber(undef) } '... undef is NOT a Foo->Number';
81dies_ok { $foo->vStr(undef) } '... undef is NOT a Foo->Str';
82dies_ok { $foo->vString(undef) } '... undef is NOT a Foo->String';
83
84dies_ok { $foo->vUndef(5) } '... 5 is NOT a Foo->Undef';
85lives_ok { $foo->vDefined(5) } '... 5 is a Foo->Defined';
86lives_ok { $foo->vInt(5) } '... 5 is a Foo->Int';
87lives_ok { $foo->vNumber(5) } '... 5 is a Foo->Number';
88lives_ok { $foo->vStr(5) } '... 5 is a Foo->Str';
89dies_ok { $foo->vString(5) } '... 5 is NOT a Foo->String';
90
91dies_ok { $foo->vUndef(0.5) } '... 0.5 is NOT a Foo->Undef';
92lives_ok { $foo->vDefined(0.5) } '... 0.5 is a Foo->Defined';
93dies_ok { $foo->vInt(0.5) } '... 0.5 is NOT a Foo->Int';
94lives_ok { $foo->vNumber(0.5) } '... 0.5 is a Foo->Number';
95lives_ok { $foo->vStr(0.5) } '... 0.5 is a Foo->Str';
96dies_ok { $foo->vString(0.5) } '... 0.5 is NOT a Foo->String';
97
98dies_ok { $foo->vUndef('Foo') } '... "Foo" is NOT a Foo->Undef';
99lives_ok { $foo->vDefined('Foo') } '... "Foo" is a Foo->Defined';
100dies_ok { $foo->vInt('Foo') } '... "Foo" is NOT a Foo->Int';
101dies_ok { $foo->vNumber('Foo') } '... "Foo" is NOT a Foo->Number';
102lives_ok { $foo->vStr('Foo') } '... "Foo" is a Foo->Str';
103lives_ok { $foo->vString('Foo') } '... "Foo" is a Foo->String';
104
105# the lazy tests
106
107lives_ok { $foo->v_lazy_Undef() } '... undef is a Foo->Undef';
108dies_ok { $foo->v_lazy_Defined() } '... undef is NOT a Foo->Defined';
109dies_ok { $foo->v_lazy_Int() } '... undef is NOT a Foo->Int';
110dies_ok { $foo->v_lazy_Number() } '... undef is NOT a Foo->Number';
111dies_ok { $foo->v_lazy_Str() } '... undef is NOT a Foo->Str';
112dies_ok { $foo->v_lazy_String() } '... undef is NOT a Foo->String';
113
114
115
116