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