Convert all tests to done_testing.
[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 $@;
703e92fb 21}
22
23use Test::Exception;
24
703e92fb 25{
26 package Foo;
27 use Moose;
28 use Moose::Util::TypeConstraints;
29
30 use Test::Deep qw[
31 eq_deeply array_each subhashof ignore
32 ];
d03bd989 33
703e92fb 34 # define your own type ...
d03bd989 35 type 'ArrayOfHashOfBarsAndRandomNumbers'
703e92fb 36 => where {
d03bd989 37 eq_deeply($_,
703e92fb 38 array_each(
39 subhashof({
40 bar => Test::Deep::isa('Bar'),
41 random_number => ignore()
42 })
43 )
44 )
d03bd989 45 };
46
703e92fb 47 has 'bar' => (
48 is => 'rw',
49 isa => 'ArrayOfHashOfBarsAndRandomNumbers',
50 );
51
52 package Bar;
53 use Moose;
54}
55
56my $array_of_hashes = [
57 { bar => Bar->new, random_number => 10 },
d03bd989 58 { bar => Bar->new },
703e92fb 59];
60
61my $foo;
62lives_ok {
d03bd989 63 $foo = Foo->new('bar' => $array_of_hashes);
703e92fb 64} '... construction succeeded';
65isa_ok($foo, 'Foo');
66
67is_deeply($foo->bar, $array_of_hashes, '... got our value correctly');
68
69dies_ok {
70 $foo->bar({});
71} '... validation failed correctly';
72
73dies_ok {
74 $foo->bar([{ foo => 3 }]);
75} '... validation failed correctly';
76
a28e50e4 77done_testing;