Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 040_type_constraints / 018_custom_parameterized_types.t
CommitLineData
5a592ad7 1#!/usr/bin/perl
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;
5
6use strict;
7use warnings;
8
9use Test::More;
10use Test::Exception;
11
12BEGIN {
13 use_ok("Mouse::Util::TypeConstraints");
14 use_ok('Mouse::Meta::TypeConstraint');
15}
16
17lives_ok {
18 subtype 'AlphaKeyHash' => as 'HashRef'
19 => where {
20 # no keys match non-alpha
21 (grep { /[^a-zA-Z]/ } keys %$_) == 0
22 };
23} '... created the subtype special okay';
24
25lives_ok {
26 subtype 'Trihash' => as 'AlphaKeyHash'
27 => where {
28 keys(%$_) == 3
29 };
30} '... created the subtype special okay';
31
32lives_ok {
33 subtype 'Noncon' => as 'Item';
34} '... created the subtype special okay';
35
36{
37 my $t = find_type_constraint('AlphaKeyHash');
38 isa_ok($t, 'Mouse::Meta::TypeConstraint');
39
40 is($t->name, 'AlphaKeyHash', '... name is correct');
41
42 my $p = $t->parent;
43 isa_ok($p, 'Mouse::Meta::TypeConstraint');
44
45 is($p->name, 'HashRef', '... parent name is correct');
46
47 ok($t->check({ one => 1, two => 2 }), '... validated it correctly');
48 ok(!$t->check({ one1 => 1, two2 => 2 }), '... validated it correctly');
49
50 local $TODO = 'Mouse does not support equals()';
51 ok( $t->equals($t), "equals to self" );
52 ok( !$t->equals($t->parent), "not equal to parent" );
53}
54
55my $hoi = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('AlphaKeyHash[Int]');
56
57ok($hoi->check({ one => 1, two => 2 }), '... validated it correctly');
58ok(!$hoi->check({ one1 => 1, two2 => 2 }), '... validated it correctly');
59ok(!$hoi->check({ one => 'uno', two => 'dos' }), '... validated it correctly');
60ok(!$hoi->check({ one1 => 'un', two2 => 'deux' }), '... validated it correctly');
61{ local $TODO = 'Mouse does not support equals()';
62ok( $hoi->equals($hoi), "equals to self" );
63ok( !$hoi->equals($hoi->parent), "equals to self" );
64ok( !$hoi->equals(find_type_constraint('AlphaKeyHash')), "not equal to unparametrized self" );
65ok( $hoi->equals( Mouse::Meta::TypeConstraint->new( name => "Blah", parent => find_type_constraint("AlphaKeyHash"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" );
66ok( !$hoi->equals( Mouse::Meta::TypeConstraint->new( name => "Oink", parent => find_type_constraint("AlphaKeyHash"), type_parameter => find_type_constraint("Str") ) ), "not equal to different parameter" );
67} # end TODO
68
69my $th = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('Trihash[Bool]');
70ok(!$th->check({ one => 1, two => 1 }), '... validated it correctly');
71ok($th->check({ one => 1, two => 0, three => 1 }), '... validated it correctly');
72ok(!$th->check({ one => 1, two => 2, three => 1 }), '... validated it correctly');
73ok(!$th->check({foo1 => 1, bar2 => 0, baz3 => 1}), '... validated it correctly');
74
75dies_ok {
76 Mouse::Meta::TypeConstraint->new(
77 name => 'Str[Int]',
78 parent => find_type_constraint('Str'),
79 type_parameter => find_type_constraint('Int'),
80 );
81} 'non-containers cannot be parameterized';
82
83dies_ok {
84 Mouse::Meta::TypeConstraint->new(
85 name => 'Noncon[Int]',
86 parent => find_type_constraint('Noncon'),
87 type_parameter => find_type_constraint('Int'),
88 );
89} 'non-containers cannot be parameterized';
90
91done_testing;