Make M::I::ExtraTests fail more obvious
[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 $@;
b71d68ed 20}
21
22use Test::Exception;
23
b71d68ed 24{
25 package Foo;
26 use Moose;
27 use Moose::Util::TypeConstraints;
28 use Declare::Constraints::Simple -All;
9e856c83 29
b71d68ed 30 # define your own type ...
9e856c83 31 type( 'HashOfArrayOfObjects',
32 {
33 where => IsHashRef(
b71d68ed 34 -keys => HasLength,
9e856c83 35 -values => IsArrayRef(IsObject)
36 )
37 } );
38
b71d68ed 39 has 'bar' => (
40 is => 'rw',
41 isa => 'HashOfArrayOfObjects',
42 );
d03bd989 43
b71d68ed 44 # inline the constraints as anon-subtypes
45 has 'baz' => (
46 is => 'rw',
9e856c83 47 isa => subtype( { as => 'ArrayRef', where => IsArrayRef(IsInt) } ),
b71d68ed 48 );
49
50 package Bar;
51 use Moose;
52}
53
54my $hash_of_arrays_of_objs = {
55 foo1 => [ Bar->new ],
d03bd989 56 foo2 => [ Bar->new, Bar->new ],
b71d68ed 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,
d03bd989 66 );
b71d68ed 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
85dies_ok {
86 $foo->baz([ "foo" ]);
87} '... validation failed correctly';
88
89dies_ok {
90 $foo->baz({});
91} '... validation failed correctly';
92
a28e50e4 93done_testing;