stop using excludes within moose, since it's no longer necessary
[gitmo/Moose.git] / t / type_constraints / advanced_type_creation.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Moose::Util::TypeConstraints;
9 use Moose::Meta::TypeConstraint::Parameterized;
10
11 my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
12
13 ## Containers in unions ...
14
15 # Array of Ints or Strings
16
17 my $array_of_ints_or_strings = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int|Str]');
18 isa_ok($array_of_ints_or_strings, 'Moose::Meta::TypeConstraint::Parameterized');
19
20 ok($array_of_ints_or_strings->check([ 1, 'two', 3 ]), '... this passed the type check');
21 ok($array_of_ints_or_strings->check([ 1, 2, 3 ]), '... this passed the type check');
22 ok($array_of_ints_or_strings->check([ 'one', 'two', 'three' ]), '... this passed the type check');
23
24 ok(!$array_of_ints_or_strings->check([ 1, [], 'three' ]), '... this didnt pass the type check');
25
26 $r->add_type_constraint($array_of_ints_or_strings);
27
28 # Array of Ints or HashRef
29
30 my $array_of_ints_or_hash_ref = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int | HashRef]');
31 isa_ok($array_of_ints_or_hash_ref, 'Moose::Meta::TypeConstraint::Parameterized');
32
33 ok($array_of_ints_or_hash_ref->check([ 1, {}, 3 ]), '... this passed the type check');
34 ok($array_of_ints_or_hash_ref->check([ 1, 2, 3 ]), '... this passed the type check');
35 ok($array_of_ints_or_hash_ref->check([ {}, {}, {} ]), '... this passed the type check');
36
37 ok(!$array_of_ints_or_hash_ref->check([ {}, [], 3 ]), '... this didnt pass the type check');
38
39 $r->add_type_constraint($array_of_ints_or_hash_ref);
40
41 # union of Arrays of Str | Int or Arrays of Int | Hash
42
43 # we can't build this using the simplistic parser
44 # we have, so we have to do it by hand - SL
45
46 my $pure_insanity = Moose::Util::TypeConstraints::create_type_constraint_union('ArrayRef[Int|Str] | ArrayRef[Int | HashRef]');
47 isa_ok($pure_insanity, 'Moose::Meta::TypeConstraint::Union');
48
49 ok($pure_insanity->check([ 1, {}, 3 ]), '... this passed the type check');
50 ok($pure_insanity->check([ 1, 'Str', 3 ]), '... this passed the type check');
51
52 ok(!$pure_insanity->check([ 1, {}, 'foo' ]), '... this didnt pass the type check');
53 ok(!$pure_insanity->check([ [], {}, 1 ]), '... this didnt pass the type check');
54
55 ## Nested Containers ...
56
57 # Array of Ints
58
59 my $array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int]');
60 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
61 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
62
63 ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
64 ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
65 ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
66
67 ok(!$array_of_ints->check(1), '... 1 failed successfully');
68 ok(!$array_of_ints->check({}), '... {} failed successfully');
69 ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
70
71 # Array of Array of Ints
72
73 my $array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[Int]]');
74 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
75 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
76
77 ok($array_of_array_of_ints->check(
78     [[ 1, 2, 3 ], [ 4, 5, 6 ]]
79 ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
80 ok(!$array_of_array_of_ints->check(
81     [[ 1, 2, 3 ], [ qw/foo bar/ ]]
82 ), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
83
84 # Array of Array of Array of Ints
85
86 my $array_of_array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]');
87 isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
88 isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
89
90 ok($array_of_array_of_array_of_ints->check(
91     [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]]
92 ), '... [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]] passed successfully');
93 ok(!$array_of_array_of_array_of_ints->check(
94     [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]]
95 ), '... [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]] failed successfully');
96
97 done_testing;