remove unneeded use of Test::Fatal
[gitmo/Moose.git] / t / 040_type_constraints / 011_container_type_constraint.t
CommitLineData
d67145ed 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
d67145ed 7
d03bd989 8BEGIN {
9 use_ok('Moose::Util::TypeConstraints');
10 use_ok('Moose::Meta::TypeConstraint::Parameterized');
d67145ed 11}
12
13# Array of Ints
14
0fbd4b0a 15my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 16 name => 'ArrayRef[Int]',
17 parent => find_type_constraint('ArrayRef'),
0fbd4b0a 18 type_parameter => find_type_constraint('Int'),
d67145ed 19);
0fbd4b0a 20isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 21isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
22
23ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
24ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
25ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
26
27ok(!$array_of_ints->check(1), '... 1 failed successfully');
28ok(!$array_of_ints->check({}), '... {} failed successfully');
29ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
30
31# Hash of Ints
32
0fbd4b0a 33my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 34 name => 'HashRef[Int]',
35 parent => find_type_constraint('HashRef'),
0fbd4b0a 36 type_parameter => find_type_constraint('Int'),
d67145ed 37);
0fbd4b0a 38isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 39isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
40
41ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully');
42ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully');
43ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully');
44
45ok(!$hash_of_ints->check(1), '... 1 failed successfully');
46ok(!$hash_of_ints->check([]), '... [] failed successfully');
47ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully');
48
49# Array of Array of Ints
50
0fbd4b0a 51my $array_of_array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 52 name => 'ArrayRef[ArrayRef[Int]]',
53 parent => find_type_constraint('ArrayRef'),
0fbd4b0a 54 type_parameter => $array_of_ints,
d67145ed 55);
0fbd4b0a 56isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 57isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
58
59ok($array_of_array_of_ints->check(
60 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
61), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
62ok(!$array_of_array_of_ints->check(
63 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
64), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
65
2b6c2a5c 66{
67 my $anon_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]');
68 isa_ok( $anon_type, 'Moose::Meta::TypeConstraint::Parameterized' );
54718169 69
2b6c2a5c 70 my $param_type = $anon_type->type_parameter;
71 isa_ok( $param_type, 'Moose::Meta::TypeConstraint::Class' );
72}
a28e50e4 73
74done_testing;