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