DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / t / 200_examples / 005_example_w_TestDeep.t
CommitLineData
703e92fb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8=pod
9
d03bd989 10This tests how well Moose type constraints
11play with Test::Deep.
703e92fb 12
d03bd989 13Its not as pretty as Declare::Constraints::Simple,
703e92fb 14but it is not completely horrid either.
15
16=cut
17
18BEGIN {
19 eval "use Test::Deep;";
d03bd989 20 plan skip_all => "Test::Deep is required for this test" if $@;
21 plan tests => 5;
703e92fb 22}
23
24use Test::Exception;
25
703e92fb 26{
27 package Foo;
28 use Moose;
29 use Moose::Util::TypeConstraints;
30
31 use Test::Deep qw[
32 eq_deeply array_each subhashof ignore
33 ];
d03bd989 34
703e92fb 35 # define your own type ...
d03bd989 36 type 'ArrayOfHashOfBarsAndRandomNumbers'
703e92fb 37 => where {
d03bd989 38 eq_deeply($_,
703e92fb 39 array_each(
40 subhashof({
41 bar => Test::Deep::isa('Bar'),
42 random_number => ignore()
43 })
44 )
45 )
d03bd989 46 };
47
703e92fb 48 has 'bar' => (
49 is => 'rw',
50 isa => 'ArrayOfHashOfBarsAndRandomNumbers',
51 );
52
53 package Bar;
54 use Moose;
55}
56
57my $array_of_hashes = [
58 { bar => Bar->new, random_number => 10 },
d03bd989 59 { bar => Bar->new },
703e92fb 60];
61
62my $foo;
63lives_ok {
d03bd989 64 $foo = Foo->new('bar' => $array_of_hashes);
1808c2da 65} 'construction succeeded';
703e92fb 66isa_ok($foo, 'Foo');
67
1808c2da 68is_deeply($foo->bar, $array_of_hashes, 'got our value correctly');
703e92fb 69
70dies_ok {
71 $foo->bar({});
1808c2da 72} 'validation failed correctly';
703e92fb 73
74dies_ok {
75 $foo->bar([{ foo => 3 }]);
1808c2da 76} 'validation failed correctly';
703e92fb 77
78