Commit | Line | Data |
b2b106d7 |
1 | #!/usr/bin/perl |
c47cf415 |
2 | # This is automatically generated by author/import-moose-test.pl. |
3 | # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!! |
4 | use t::lib::MooseCompat; |
b2b106d7 |
5 | |
6 | use strict; |
7 | use warnings; |
8 | |
c47cf415 |
9 | use Test::More; |
10 | $TODO = q{Mouse is not yet completed}; |
b2b106d7 |
11 | use Test::Exception; |
12 | |
13 | BEGIN { |
14 | use_ok("Mouse::Util::TypeConstraints"); |
c47cf415 |
15 | use_ok('Mouse::Meta::TypeConstraint'); |
b2b106d7 |
16 | } |
17 | |
18 | BEGIN { |
19 | package MyList; |
20 | sub new { |
21 | my $class = shift; |
22 | bless { items => \@_ }, $class; |
23 | } |
24 | |
25 | sub items { |
26 | my $self = shift; |
27 | return @{ $self->{items} }; |
28 | } |
29 | } |
30 | |
31 | subtype 'MyList' => as 'Object' => where { $_->isa('MyList') }; |
32 | |
33 | lives_ok { |
34 | coerce 'ArrayRef' |
35 | => from 'MyList' |
36 | => via { [ $_->items ] } |
37 | } '... created the coercion okay'; |
38 | |
39 | my $mylist = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('MyList[Int]'); |
40 | |
41 | ok($mylist->check(MyList->new(10, 20, 30)), '... validated it correctly (pass)'); |
42 | ok(!$mylist->check(MyList->new(10, "two")), '... validated it correctly (fail)'); |
43 | ok(!$mylist->check([10]), '... validated it correctly (fail)'); |
44 | |
45 | subtype 'EvenList' => as 'MyList' => where { $_->items % 2 == 0 }; |
46 | |
47 | # XXX: get this to work *without* the declaration. I suspect it'll be a new |
48 | # method in Mouse::Meta::TypeCoercion that will look at the parents of the |
49 | # coerced type as well. but will that be too "action at a distance"-ey? |
50 | lives_ok { |
51 | coerce 'ArrayRef' |
52 | => from 'EvenList' |
53 | => via { [ $_->items ] } |
54 | } '... created the coercion okay'; |
55 | |
56 | my $evenlist = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('EvenList[Int]'); |
57 | |
58 | ok(!$evenlist->check(MyList->new(10, 20, 30)), '... validated it correctly (fail)'); |
59 | ok($evenlist->check(MyList->new(10, 20, 30, 40)), '... validated it correctly (pass)'); |
60 | ok(!$evenlist->check(MyList->new(10, "two")), '... validated it correctly (fail)'); |
61 | ok(!$evenlist->check([10, 20]), '... validated it correctly (fail)'); |
62 | |
c47cf415 |
63 | done_testing; |