Changelogging
[gitmo/Mouse.git] / t / 200_examples / 005_example_w_TestDeep.t
CommitLineData
7a50b450 1#!/usr/bin/perl
fde8e43f 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
7a50b450 5
6use strict;
7use warnings;
8
9use Test::More;
10
11=pod
12
13This tests how well Mouse type constraints
14play with Test::Deep.
15
16Its not as pretty as Declare::Constraints::Simple,
17but it is not completely horrid either.
18
19=cut
20
fde8e43f 21use Test::Requires {
22 'Test::Deep' => '0.01', # skip all if not installed
23};
7a50b450 24
25use Test::Exception;
26
27{
28 package Foo;
29 use Mouse;
30 use Mouse::Util::TypeConstraints;
31
32 use Test::Deep qw[
33 eq_deeply array_each subhashof ignore
34 ];
35
36 # define your own type ...
37 type 'ArrayOfHashOfBarsAndRandomNumbers'
38 => where {
39 eq_deeply($_,
40 array_each(
41 subhashof({
42 bar => Test::Deep::isa('Bar'),
43 random_number => ignore()
44 })
45 )
46 )
47 };
48
49 has 'bar' => (
50 is => 'rw',
51 isa => 'ArrayOfHashOfBarsAndRandomNumbers',
52 );
53
54 package Bar;
55 use Mouse;
56}
57
58my $array_of_hashes = [
59 { bar => Bar->new, random_number => 10 },
60 { bar => Bar->new },
61];
62
63my $foo;
64lives_ok {
65 $foo = Foo->new('bar' => $array_of_hashes);
66} '... construction succeeded';
67isa_ok($foo, 'Foo');
68
69is_deeply($foo->bar, $array_of_hashes, '... got our value correctly');
70
71dies_ok {
72 $foo->bar({});
73} '... validation failed correctly';
74
75dies_ok {
76 $foo->bar([{ foo => 3 }]);
77} '... validation failed correctly';
78
fde8e43f 79done_testing;