Add various things
[gitmo/Mouse.git] / t / 200_examples / 004_example_w_DCS.t
CommitLineData
7a50b450 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8=pod
9
10This tests how well Mouse type constraints
11play with Declare::Constraints::Simple.
12
13Pretty well if I do say so myself :)
14
15=cut
16
17BEGIN {
18 eval "use Declare::Constraints::Simple;";
19 plan skip_all => "Declare::Constraints::Simple is required for this test" if $@;
20 plan tests => 9;
21}
22
23use Test::Exception;
24
25{
26 package Foo;
27 use Mouse;
28 use Mouse::Util::TypeConstraints;
29 use Declare::Constraints::Simple -All;
30
31 # define your own type ...
32 type( 'HashOfArrayOfObjects',
33 where => IsHashRef(
34 -keys => HasLength,
35 -values => IsArrayRef(IsObject)
36 )
37 );
38
39 has 'bar' => (
40 is => 'rw',
41 isa => 'HashOfArrayOfObjects',
42 );
43
44 # inline the constraints as anon-subtypes
45 has 'baz' => (
46 is => 'rw',
47 isa => subtype( as => 'ArrayRef', where => IsArrayRef(IsInt) ),
48 );
49
50 package Bar;
51 use Mouse;
52}
53
54my $hash_of_arrays_of_objs = {
55 foo1 => [ Bar->new ],
56 foo2 => [ Bar->new, Bar->new ],
57};
58
59my $array_of_ints = [ 1 .. 10 ];
60
61my $foo;
62lives_ok {
63 $foo = Foo->new(
64 'bar' => $hash_of_arrays_of_objs,
65 'baz' => $array_of_ints,
66 );
67} '... construction succeeded';
68isa_ok($foo, 'Foo');
69
70is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly');
71is_deeply($foo->baz, $array_of_ints, '... got our value correctly');
72
73dies_ok {
74 $foo->bar([]);
75} '... validation failed correctly';
76
77dies_ok {
78 $foo->bar({ foo => 3 });
79} '... validation failed correctly';
80
81dies_ok {
82 $foo->bar({ foo => [ 1, 2, 3 ] });
83} '... validation failed correctly';
84
85
86dies_ok {
87 $foo->baz([ "foo" ]);
88} '... validation failed correctly';
89
90dies_ok {
91 $foo->baz({});
92} '... validation failed correctly';