6 use Test::More tests => 33;
10 use_ok('Moose::Util::TypeConstraints');
11 use_ok('Moose::Meta::TypeConstraint::Parameterized');
14 my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
16 ## Containers in unions ...
18 # Array of Ints or Strings
20 my $array_of_ints_or_strings = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int|Str]');
21 isa_ok($array_of_ints_or_strings, 'Moose::Meta::TypeConstraint::Parameterized');
23 ok($array_of_ints_or_strings->check([ 1, 'two', 3 ]), 'this passed the type check');
24 ok($array_of_ints_or_strings->check([ 1, 2, 3 ]), 'this passed the type check');
25 ok($array_of_ints_or_strings->check([ 'one', 'two', 'three' ]), 'this passed the type check');
27 ok(!$array_of_ints_or_strings->check([ 1, [], 'three' ]), 'this didnt pass the type check');
29 $r->add_type_constraint($array_of_ints_or_strings);
31 # Array of Ints or HashRef
33 my $array_of_ints_or_hash_ref = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int | HashRef]');
34 isa_ok($array_of_ints_or_hash_ref, 'Moose::Meta::TypeConstraint::Parameterized');
36 ok($array_of_ints_or_hash_ref->check([ 1, {}, 3 ]), 'this passed the type check');
37 ok($array_of_ints_or_hash_ref->check([ 1, 2, 3 ]), 'this passed the type check');
38 ok($array_of_ints_or_hash_ref->check([ {}, {}, {} ]), 'this passed the type check');
40 ok(!$array_of_ints_or_hash_ref->check([ {}, [], 3 ]), 'this didnt pass the type check');
42 $r->add_type_constraint($array_of_ints_or_hash_ref);
44 # union of Arrays of Str | Int or Arrays of Int | Hash
46 # we can't build this using the simplistic parser
47 # we have, so we have to do it by hand - SL
49 my $pure_insanity = Moose::Util::TypeConstraints::create_type_constraint_union('ArrayRef[Int|Str] | ArrayRef[Int | HashRef]');
50 isa_ok($pure_insanity, 'Moose::Meta::TypeConstraint::Union');
52 ok($pure_insanity->check([ 1, {}, 3 ]), 'this passed the type check');
53 ok($pure_insanity->check([ 1, 'Str', 3 ]), 'this passed the type check');
55 ok(!$pure_insanity->check([ 1, {}, 'foo' ]), 'this didnt pass the type check');
56 ok(!$pure_insanity->check([ [], {}, 1 ]), 'this didnt pass the type check');
58 ## Nested Containers ...
62 my $array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int]');
63 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
64 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
66 ok($array_of_ints->check([ 1, 2, 3, 4 ]), '[ 1, 2, 3, 4 ] passed successfully');
67 ok(!$array_of_ints->check([qw/foo bar baz/]), '[qw/foo bar baz/] failed successfully');
68 ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '[ 1, 2, 3, qw/foo bar/] failed successfully');
70 ok(!$array_of_ints->check(1), '1 failed successfully');
71 ok(!$array_of_ints->check({}), '{} failed successfully');
72 ok(!$array_of_ints->check(sub { () }), 'sub { () } failed successfully');
74 # Array of Array of Ints
76 my $array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[Int]]');
77 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
78 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
80 ok($array_of_array_of_ints->check(
81 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
82 ), '[[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
83 ok(!$array_of_array_of_ints->check(
84 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
85 ), '[[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
87 # Array of Array of Array of Ints
89 my $array_of_array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]');
90 isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
91 isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
93 ok($array_of_array_of_array_of_ints->check(
94 [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]]
95 ), '[[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]] passed successfully');
96 ok(!$array_of_array_of_array_of_ints->check(
97 [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]]
98 ), '[[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]] failed successfully');