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