test with Test::Deep::eq_deeply
[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
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 $@;
20 plan tests => 11;
21}
22
23use Test::Exception;
24
25BEGIN {
26 use_ok('Moose');
27 use_ok('Moose::Util::TypeConstraints');
28}
29
30{
31 package Foo;
32 use Moose;
33 use Moose::Util::TypeConstraints;
34 use Declare::Constraints::Simple -All;
35
36 # define your own type ...
37 type 'HashOfArrayOfObjects'
38 => IsHashRef(
39 -keys => HasLength,
40 -values => IsArrayRef( IsObject ));
41
42 has 'bar' => (
43 is => 'rw',
44 isa => 'HashOfArrayOfObjects',
45 );
46
47 # inline the constraints as anon-subtypes
48 has 'baz' => (
49 is => 'rw',
50 isa => subtype('ArrayRef' => IsArrayRef(IsInt)),
51 );
52
53 package Bar;
54 use Moose;
55}
56
57my $hash_of_arrays_of_objs = {
58 foo1 => [ Bar->new ],
59 foo2 => [ Bar->new, Bar->new ],
60};
61
62my $array_of_ints = [ 1 .. 10 ];
63
64my $foo;
65lives_ok {
66 $foo = Foo->new(
67 'bar' => $hash_of_arrays_of_objs,
68 'baz' => $array_of_ints,
69 );
70} '... construction succeeded';
71isa_ok($foo, 'Foo');
72
73is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly');
74is_deeply($foo->baz, $array_of_ints, '... got our value correctly');
75
76dies_ok {
77 $foo->bar([]);
78} '... validation failed correctly';
79
80dies_ok {
81 $foo->bar({ foo => 3 });
82} '... validation failed correctly';
83
84dies_ok {
85 $foo->bar({ foo => [ 1, 2, 3 ] });
86} '... validation failed correctly';
87
88dies_ok {
89 $foo->baz([ "foo" ]);
90} '... validation failed correctly';
91
92dies_ok {
93 $foo->baz({});
94} '... validation failed correctly';
95
96
97
98
99
100
101