From: Stevan Little Date: Fri, 9 Mar 2007 21:44:23 +0000 (+0000) Subject: test to use DCS X-Git-Tag: 0_19~19 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b71d68ed31257d06be32bbae2eacf31acbdade56;p=gitmo%2FMoose.git test to use DCS --- diff --git a/MANIFEST b/MANIFEST index c91742a..ee1e988 100644 --- a/MANIFEST +++ b/MANIFEST @@ -93,6 +93,7 @@ t/104_inline_reader_bug.t t/201_example.t t/202_example_Moose_POOP.t t/203_example.t +t/204_example_w_DCS.t t/300_immutable_moose.t t/pod.t t/pod_coverage.t diff --git a/t/204_example_w_DCS.t b/t/204_example_w_DCS.t new file mode 100644 index 0000000..6078c06 --- /dev/null +++ b/t/204_example_w_DCS.t @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +BEGIN { + eval "use Declare::Constraints::Simple;"; + plan skip_all => "Declare::Constraints::Simple is required for this test" if $@; + plan tests => 11; +} + +use Test::Exception; + +BEGIN { + use_ok('Moose'); + use_ok('Moose::Util::TypeConstraints'); +} + +{ + package Foo; + use Moose; + use Moose::Util::TypeConstraints; + use Declare::Constraints::Simple -All; + + # define your own type ... + type 'HashOfArrayOfObjects' + => IsHashRef( + -keys => HasLength, + -values => IsArrayRef( IsObject )); + + has 'bar' => ( + is => 'rw', + isa => 'HashOfArrayOfObjects', + ); + + # inline the constraints as anon-subtypes + has 'baz' => ( + is => 'rw', + isa => subtype('ArrayRef' => IsArrayRef(IsInt)), + ); + + package Bar; + use Moose; +} + +my $hash_of_arrays_of_objs = { + foo1 => [ Bar->new ], + foo2 => [ Bar->new, Bar->new ], +}; + +my $array_of_ints = [ 1 .. 10 ]; + +my $foo; +lives_ok { + $foo = Foo->new( + 'bar' => $hash_of_arrays_of_objs, + 'baz' => $array_of_ints, + ); +} '... construction succeeded'; +isa_ok($foo, 'Foo'); + +is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly'); +is_deeply($foo->baz, $array_of_ints, '... got our value correctly'); + +dies_ok { + $foo->bar([]); +} '... validation failed correctly'; + +dies_ok { + $foo->bar({ foo => 3 }); +} '... validation failed correctly'; + +dies_ok { + $foo->bar({ foo => [ 1, 2, 3 ] }); +} '... validation failed correctly'; + +dies_ok { + $foo->baz([ "foo" ]); +} '... validation failed correctly'; + +dies_ok { + $foo->baz({}); +} '... validation failed correctly'; + + + + + + +