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