remove unneeded use of Test::Fatal
[gitmo/Moose.git] / t / 040_type_constraints / 013_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');
56
57## Nested Containers ...
58
59# Array of Ints
60
0fbd4b0a 61my $array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int]');
62isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 63isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
64
65ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
66ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
67ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
68
69ok(!$array_of_ints->check(1), '... 1 failed successfully');
70ok(!$array_of_ints->check({}), '... {} failed successfully');
71ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
72
73# Array of Array of Ints
74
0fbd4b0a 75my $array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[Int]]');
76isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 77isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
78
79ok($array_of_array_of_ints->check(
80 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
81), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
82ok(!$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
0fbd4b0a 88my $array_of_array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]');
89isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
f1917f58 90isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
91
92ok($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');
95ok(!$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
a28e50e4 99done_testing;