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