We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / examples / 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
4d438a84 18use Test::Requires {
19 'Test::Deep' => '0.01', # skip all if not installed
20};
703e92fb 21
b10dde3a 22use Test::Fatal;
703e92fb 23
703e92fb 24{
25 package Foo;
26 use Moose;
27 use Moose::Util::TypeConstraints;
28
29 use Test::Deep qw[
30 eq_deeply array_each subhashof ignore
31 ];
d03bd989 32
703e92fb 33 # define your own type ...
d03bd989 34 type 'ArrayOfHashOfBarsAndRandomNumbers'
703e92fb 35 => where {
d03bd989 36 eq_deeply($_,
703e92fb 37 array_each(
38 subhashof({
39 bar => Test::Deep::isa('Bar'),
40 random_number => ignore()
41 })
42 )
43 )
d03bd989 44 };
45
703e92fb 46 has 'bar' => (
47 is => 'rw',
48 isa => 'ArrayOfHashOfBarsAndRandomNumbers',
49 );
50
51 package Bar;
52 use Moose;
53}
54
55my $array_of_hashes = [
56 { bar => Bar->new, random_number => 10 },
d03bd989 57 { bar => Bar->new },
703e92fb 58];
59
60my $foo;
b10dde3a 61is( exception {
d03bd989 62 $foo = Foo->new('bar' => $array_of_hashes);
b10dde3a 63}, undef, '... construction succeeded' );
703e92fb 64isa_ok($foo, 'Foo');
65
66is_deeply($foo->bar, $array_of_hashes, '... got our value correctly');
67
b10dde3a 68isnt( exception {
703e92fb 69 $foo->bar({});
b10dde3a 70}, undef, '... validation failed correctly' );
703e92fb 71
b10dde3a 72isnt( exception {
703e92fb 73 $foo->bar([{ foo => 3 }]);
b10dde3a 74}, undef, '... validation failed correctly' );
703e92fb 75
a28e50e4 76done_testing;