Change how the TC sugar bits work so that the arguments are
[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
10This tests how well Moose type constraints
11play with Declare::Constraints::Simple.
12
13Pretty well if I do say so myself :)
14
15=cut
16
b71d68ed 17BEGIN {
18 eval "use Declare::Constraints::Simple;";
19 plan skip_all => "Declare::Constraints::Simple is required for this test" if $@;
7ff56534 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;
30
31 # define your own type ...
32 type 'HashOfArrayOfObjects'
33 => IsHashRef(
34 -keys => HasLength,
35 -values => IsArrayRef( IsObject ));
36
37 has 'bar' => (
38 is => 'rw',
39 isa => 'HashOfArrayOfObjects',
40 );
41
42 # inline the constraints as anon-subtypes
43 has 'baz' => (
44 is => 'rw',
45 isa => subtype('ArrayRef' => IsArrayRef(IsInt)),
46 );
47
48 package Bar;
49 use Moose;
50}
51
52my $hash_of_arrays_of_objs = {
53 foo1 => [ Bar->new ],
54 foo2 => [ Bar->new, Bar->new ],
55};
56
57my $array_of_ints = [ 1 .. 10 ];
58
59my $foo;
60lives_ok {
61 $foo = Foo->new(
62 'bar' => $hash_of_arrays_of_objs,
63 'baz' => $array_of_ints,
64 );
65} '... construction succeeded';
66isa_ok($foo, 'Foo');
67
68is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly');
69is_deeply($foo->baz, $array_of_ints, '... got our value correctly');
70
71dies_ok {
72 $foo->bar([]);
73} '... validation failed correctly';
74
75dies_ok {
76 $foo->bar({ foo => 3 });
77} '... validation failed correctly';
78
79dies_ok {
80 $foo->bar({ foo => [ 1, 2, 3 ] });
81} '... validation failed correctly';
82
83dies_ok {
84 $foo->baz([ "foo" ]);
85} '... validation failed correctly';
86
87dies_ok {
88 $foo->baz({});
89} '... validation failed correctly';
90
91
92
93
94
95
96