2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
10 $TODO = q{Mouse is not yet completed};
14 use_ok('Mouse::Util::TypeConstraints');
15 use_ok('Mouse::Meta::TypeConstraint');
18 my $r = Mouse::Util::TypeConstraints->get_type_constraint_registry;
20 ## Containers in unions ...
22 # Array of Ints or Strings
24 my $array_of_ints_or_strings = Mouse::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int|Str]');
25 isa_ok($array_of_ints_or_strings, 'Mouse::Meta::TypeConstraint');
27 ok($array_of_ints_or_strings->check([ 1, 'two', 3 ]), '... this passed the type check');
28 ok($array_of_ints_or_strings->check([ 1, 2, 3 ]), '... this passed the type check');
29 ok($array_of_ints_or_strings->check([ 'one', 'two', 'three' ]), '... this passed the type check');
31 ok(!$array_of_ints_or_strings->check([ 1, [], 'three' ]), '... this didnt pass the type check');
33 $r->add_type_constraint($array_of_ints_or_strings);
35 # Array of Ints or HashRef
37 my $array_of_ints_or_hash_ref = Mouse::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int | HashRef]');
38 isa_ok($array_of_ints_or_hash_ref, 'Mouse::Meta::TypeConstraint');
40 ok($array_of_ints_or_hash_ref->check([ 1, {}, 3 ]), '... this passed the type check');
41 ok($array_of_ints_or_hash_ref->check([ 1, 2, 3 ]), '... this passed the type check');
42 ok($array_of_ints_or_hash_ref->check([ {}, {}, {} ]), '... this passed the type check');
44 ok(!$array_of_ints_or_hash_ref->check([ {}, [], 3 ]), '... this didnt pass the type check');
46 $r->add_type_constraint($array_of_ints_or_hash_ref);
48 # union of Arrays of Str | Int or Arrays of Int | Hash
50 # we can't build this using the simplistic parser
51 # we have, so we have to do it by hand - SL
53 my $pure_insanity = Mouse::Util::TypeConstraints::create_type_constraint_union('ArrayRef[Int|Str] | ArrayRef[Int | HashRef]');
54 isa_ok($pure_insanity, 'Mouse::Meta::TypeConstraint');
56 ok($pure_insanity->check([ 1, {}, 3 ]), '... this passed the type check');
57 ok($pure_insanity->check([ 1, 'Str', 3 ]), '... this passed the type check');
59 ok(!$pure_insanity->check([ 1, {}, 'foo' ]), '... this didnt pass the type check');
60 ok(!$pure_insanity->check([ [], {}, 1 ]), '... this didnt pass the type check');
62 ## Nested Containers ...
66 my $array_of_ints = Mouse::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int]');
67 isa_ok($array_of_ints, 'Mouse::Meta::TypeConstraint');
68 isa_ok($array_of_ints, 'Mouse::Meta::TypeConstraint');
70 ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
71 ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
72 ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
74 ok(!$array_of_ints->check(1), '... 1 failed successfully');
75 ok(!$array_of_ints->check({}), '... {} failed successfully');
76 ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
78 # Array of Array of Ints
80 my $array_of_array_of_ints = Mouse::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[Int]]');
81 isa_ok($array_of_array_of_ints, 'Mouse::Meta::TypeConstraint');
82 isa_ok($array_of_array_of_ints, 'Mouse::Meta::TypeConstraint');
84 ok($array_of_array_of_ints->check(
85 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
86 ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
87 ok(!$array_of_array_of_ints->check(
88 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
89 ), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
91 # Array of Array of Array of Ints
93 my $array_of_array_of_array_of_ints = Mouse::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]');
94 isa_ok($array_of_array_of_array_of_ints, 'Mouse::Meta::TypeConstraint');
95 isa_ok($array_of_array_of_array_of_ints, 'Mouse::Meta::TypeConstraint');
97 ok($array_of_array_of_array_of_ints->check(
98 [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]]
99 ), '... [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]] passed successfully');
100 ok(!$array_of_array_of_array_of_ints->check(
101 [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]]
102 ), '... [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]] failed successfully');