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