complete re-organization of the test suite
[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
10This tests how well Moose type constraints
11play with Test::Deep.
12
13Its not as pretty as Declare::Constraints::Simple,
14but it is not completely horrid either.
15
16=cut
17
18BEGIN {
19 eval "use Test::Deep;";
20 plan skip_all => "Test::Deep is required for this test" if $@;
21 plan tests => 7;
22}
23
24use Test::Exception;
25
26BEGIN {
27 use_ok('Moose');
28 use_ok('Moose::Util::TypeConstraints');
29}
30
31{
32 package Foo;
33 use Moose;
34 use Moose::Util::TypeConstraints;
35
36 use Test::Deep qw[
37 eq_deeply array_each subhashof ignore
38 ];
39
40 # define your own type ...
41 type 'ArrayOfHashOfBarsAndRandomNumbers'
42 => where {
43 eq_deeply($_,
44 array_each(
45 subhashof({
46 bar => Test::Deep::isa('Bar'),
47 random_number => ignore()
48 })
49 )
50 )
51 };
52
53 has 'bar' => (
54 is => 'rw',
55 isa => 'ArrayOfHashOfBarsAndRandomNumbers',
56 );
57
58 package Bar;
59 use Moose;
60}
61
62my $array_of_hashes = [
63 { bar => Bar->new, random_number => 10 },
64 { bar => Bar->new },
65];
66
67my $foo;
68lives_ok {
69 $foo = Foo->new('bar' => $array_of_hashes);
70} '... construction succeeded';
71isa_ok($foo, 'Foo');
72
73is_deeply($foo->bar, $array_of_hashes, '... 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
83