We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / examples / 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
4d438a84 17use Test::Requires {
18 'Declare::Constraints::Simple' => '0.01', # skip all if not installed
19};
b71d68ed 20
b10dde3a 21use Test::Fatal;
b71d68ed 22
b71d68ed 23{
24 package Foo;
25 use Moose;
26 use Moose::Util::TypeConstraints;
27 use Declare::Constraints::Simple -All;
9e856c83 28
b71d68ed 29 # define your own type ...
9e856c83 30 type( 'HashOfArrayOfObjects',
31 {
32 where => IsHashRef(
b71d68ed 33 -keys => HasLength,
9e856c83 34 -values => IsArrayRef(IsObject)
35 )
36 } );
37
b71d68ed 38 has 'bar' => (
39 is => 'rw',
40 isa => 'HashOfArrayOfObjects',
41 );
d03bd989 42
b71d68ed 43 # inline the constraints as anon-subtypes
44 has 'baz' => (
45 is => 'rw',
9e856c83 46 isa => subtype( { as => 'ArrayRef', where => IsArrayRef(IsInt) } ),
b71d68ed 47 );
48
49 package Bar;
50 use Moose;
51}
52
53my $hash_of_arrays_of_objs = {
54 foo1 => [ Bar->new ],
d03bd989 55 foo2 => [ Bar->new, Bar->new ],
b71d68ed 56};
57
58my $array_of_ints = [ 1 .. 10 ];
59
60my $foo;
b10dde3a 61is( exception {
b71d68ed 62 $foo = Foo->new(
63 'bar' => $hash_of_arrays_of_objs,
64 'baz' => $array_of_ints,
d03bd989 65 );
b10dde3a 66}, undef, '... construction succeeded' );
b71d68ed 67isa_ok($foo, 'Foo');
68
69is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly');
70is_deeply($foo->baz, $array_of_ints, '... got our value correctly');
71
b10dde3a 72isnt( exception {
b71d68ed 73 $foo->bar([]);
b10dde3a 74}, undef, '... validation failed correctly' );
b71d68ed 75
b10dde3a 76isnt( exception {
b71d68ed 77 $foo->bar({ foo => 3 });
b10dde3a 78}, undef, '... validation failed correctly' );
b71d68ed 79
b10dde3a 80isnt( exception {
b71d68ed 81 $foo->bar({ foo => [ 1, 2, 3 ] });
b10dde3a 82}, undef, '... validation failed correctly' );
b71d68ed 83
b10dde3a 84isnt( exception {
b71d68ed 85 $foo->baz([ "foo" ]);
b10dde3a 86}, undef, '... validation failed correctly' );
b71d68ed 87
b10dde3a 88isnt( exception {
b71d68ed 89 $foo->baz({});
b10dde3a 90}, undef, '... validation failed correctly' );
b71d68ed 91
a28e50e4 92done_testing;