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