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