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