Fix docs. The phrases "Fewer than 1%" and "over 96%" are very confusing, so I removed...
[gitmo/Mouse.git] / t / 200_examples / 005_example_w_TestDeep.t
CommitLineData
7a50b450 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8=pod
9
10This tests how well Mouse 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 => 5;
22}
23
24use Test::Exception;
25
26{
27 package Foo;
28 use Mouse;
29 use Mouse::Util::TypeConstraints;
30
31 use Test::Deep qw[
32 eq_deeply array_each subhashof ignore
33 ];
34
35 # define your own type ...
36 type 'ArrayOfHashOfBarsAndRandomNumbers'
37 => where {
38 eq_deeply($_,
39 array_each(
40 subhashof({
41 bar => Test::Deep::isa('Bar'),
42 random_number => ignore()
43 })
44 )
45 )
46 };
47
48 has 'bar' => (
49 is => 'rw',
50 isa => 'ArrayOfHashOfBarsAndRandomNumbers',
51 );
52
53 package Bar;
54 use Mouse;
55}
56
57my $array_of_hashes = [
58 { bar => Bar->new, random_number => 10 },
59 { bar => Bar->new },
60];
61
62my $foo;
63lives_ok {
64 $foo = Foo->new('bar' => $array_of_hashes);
65} '... construction succeeded';
66isa_ok($foo, 'Foo');
67
68is_deeply($foo->bar, $array_of_hashes, '... got our value correctly');
69
70dies_ok {
71 $foo->bar({});
72} '... validation failed correctly';
73
74dies_ok {
75 $foo->bar([{ foo => 3 }]);
76} '... validation failed correctly';
77
78