8 use Moose::Util::TypeConstraints;
9 use Moose::Meta::TypeConstraint::Parameterized;
11 my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
13 ## Containers in unions ...
15 # Array of Ints or Strings
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');
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');
24 ok(!$array_of_ints_or_strings->check([ 1, [], 'three' ]), '... this didnt pass the type check');
26 $r->add_type_constraint($array_of_ints_or_strings);
28 # Array of Ints or HashRef
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');
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');
37 ok(!$array_of_ints_or_hash_ref->check([ {}, [], 3 ]), '... this didnt pass the type check');
39 $r->add_type_constraint($array_of_ints_or_hash_ref);
41 # union of Arrays of Str | Int or Arrays of Int | Hash
43 # we can't build this using the simplistic parser
44 # we have, so we have to do it by hand - SL
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');
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');
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');
55 ## Nested Containers ...
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');
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');
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');
71 # Array of Array of Ints
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');
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');
84 # Array of Array of Array of Ints
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');
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');