Regenerate test files
[gitmo/Mouse.git] / t / 200_examples / 004_example_w_DCS.t
CommitLineData
7a50b450 1#!/usr/bin/perl
fde8e43f 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
7a50b450 5
6use strict;
7use warnings;
8
9use Test::More;
10
11=pod
12
13This tests how well Mouse type constraints
14play with Declare::Constraints::Simple.
15
16Pretty well if I do say so myself :)
17
18=cut
19
fde8e43f 20use Test::Requires {
21 'Declare::Constraints::Simple' => '0.01', # skip all if not installed
22};
7a50b450 23
24use Test::Exception;
25
26{
27 package Foo;
28 use Mouse;
29 use Mouse::Util::TypeConstraints;
30 use Declare::Constraints::Simple -All;
31
32 # define your own type ...
33 type( 'HashOfArrayOfObjects',
73766a27 34 {
7a50b450 35 where => IsHashRef(
36 -keys => HasLength,
37 -values => IsArrayRef(IsObject)
38 )
73766a27 39 } );
7a50b450 40
41 has 'bar' => (
42 is => 'rw',
43 isa => 'HashOfArrayOfObjects',
44 );
45
46 # inline the constraints as anon-subtypes
47 has 'baz' => (
48 is => 'rw',
73766a27 49 isa => subtype( { as => 'ArrayRef', where => IsArrayRef(IsInt) } ),
7a50b450 50 );
51
52 package Bar;
53 use Mouse;
54}
55
56my $hash_of_arrays_of_objs = {
57 foo1 => [ Bar->new ],
58 foo2 => [ Bar->new, Bar->new ],
59};
60
61my $array_of_ints = [ 1 .. 10 ];
62
63my $foo;
64lives_ok {
65 $foo = Foo->new(
66 'bar' => $hash_of_arrays_of_objs,
67 'baz' => $array_of_ints,
68 );
69} '... construction succeeded';
70isa_ok($foo, 'Foo');
71
72is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly');
73is_deeply($foo->baz, $array_of_ints, '... got our value correctly');
74
75dies_ok {
76 $foo->bar([]);
77} '... validation failed correctly';
78
79dies_ok {
80 $foo->bar({ foo => 3 });
81} '... validation failed correctly';
82
83dies_ok {
84 $foo->bar({ foo => [ 1, 2, 3 ] });
85} '... validation failed correctly';
86
7a50b450 87dies_ok {
88 $foo->baz([ "foo" ]);
89} '... validation failed correctly';
90
91dies_ok {
92 $foo->baz({});
93} '... validation failed correctly';
73766a27 94
fde8e43f 95done_testing;