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