Simplistic implementation of type intersections, modeled after the implementation...
[gitmo/Moose.git] / t / type_constraints / advanced_type_creation.t
CommitLineData
f1917f58 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
f1917f58 7
d03bd989 8BEGIN {
9 use_ok('Moose::Util::TypeConstraints');
10 use_ok('Moose::Meta::TypeConstraint::Parameterized');
f1917f58 11}
12
13my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
14
15## Containers in unions ...
16
17# Array of Ints or Strings
18
3796382a 19my $array_of_ints_or_strings = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int|Str]');
0fbd4b0a 20isa_ok($array_of_ints_or_strings, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 21
22ok($array_of_ints_or_strings->check([ 1, 'two', 3 ]), '... this passed the type check');
23ok($array_of_ints_or_strings->check([ 1, 2, 3 ]), '... this passed the type check');
24ok($array_of_ints_or_strings->check([ 'one', 'two', 'three' ]), '... this passed the type check');
25
26ok(!$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
0fbd4b0a 32my $array_of_ints_or_hash_ref = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int | HashRef]');
33isa_ok($array_of_ints_or_hash_ref, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 34
35ok($array_of_ints_or_hash_ref->check([ 1, {}, 3 ]), '... this passed the type check');
36ok($array_of_ints_or_hash_ref->check([ 1, 2, 3 ]), '... this passed the type check');
37ok($array_of_ints_or_hash_ref->check([ {}, {}, {} ]), '... this passed the type check');
38
39ok(!$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
d03bd989 45# we can't build this using the simplistic parser
f1917f58 46# we have, so we have to do it by hand - SL
47
3796382a 48my $pure_insanity = Moose::Util::TypeConstraints::create_type_constraint_union('ArrayRef[Int|Str] | ArrayRef[Int | HashRef]');
f1917f58 49isa_ok($pure_insanity, 'Moose::Meta::TypeConstraint::Union');
50
51ok($pure_insanity->check([ 1, {}, 3 ]), '... this passed the type check');
52ok($pure_insanity->check([ 1, 'Str', 3 ]), '... this passed the type check');
53
54ok(!$pure_insanity->check([ 1, {}, 'foo' ]), '... this didnt pass the type check');
55ok(!$pure_insanity->check([ [], {}, 1 ]), '... this didnt pass the type check');
8aab053a 56
57# intersection of Arrays of Int | Str or Arrays of Str | Int
58
59my $sheer_insanity = Moose::Util::TypeConstraints::create_type_constraint_intersection('ArrayRef[Int|Str] & ArrayRef[Str | Int]');
60isa_ok($sheer_insanity, 'Moose::Meta::TypeConstraint::Intersection');
61
62ok($sheer_insanity->check([ 1, 4, 'foo' ]), '... this passed the type check');
63ok($sheer_insanity->check([ 1, 'Str', 'foo' ]), '... this passed the type check');
64
65ok(!$sheer_insanity->check([ 1, {}, 'foo' ]), '... this didnt pass the type check');
66ok(!$sheer_insanity->check([ [], {}, 1 ]), '... this didnt pass the type check');
f1917f58 67
68## Nested Containers ...
69
70# Array of Ints
71
0fbd4b0a 72my $array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int]');
73isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 74isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
75
76ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
77ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
78ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
79
80ok(!$array_of_ints->check(1), '... 1 failed successfully');
81ok(!$array_of_ints->check({}), '... {} failed successfully');
82ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
83
84# Array of Array of Ints
85
0fbd4b0a 86my $array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[Int]]');
87isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 88isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
89
90ok($array_of_array_of_ints->check(
91 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
92), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
93ok(!$array_of_array_of_ints->check(
94 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
95), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
96
97# Array of Array of Array of Ints
98
0fbd4b0a 99my $array_of_array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]');
100isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 101isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
102
103ok($array_of_array_of_array_of_ints->check(
104 [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]]
105), '... [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]] passed successfully');
106ok(!$array_of_array_of_array_of_ints->check(
107 [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]]
108), '... [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]] failed successfully');
109
a28e50e4 110done_testing;