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