From: Stevan Little Date: Fri, 9 Mar 2007 22:25:16 +0000 (+0000) Subject: test with Test::Deep::eq_deeply X-Git-Tag: 0_19~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=703e92fb181fe1ee2c23322ab7338da6e16a43cd;p=gitmo%2FMoose.git test with Test::Deep::eq_deeply --- diff --git a/MANIFEST b/MANIFEST index ee1e988..1434e12 100644 --- a/MANIFEST +++ b/MANIFEST @@ -94,6 +94,7 @@ t/201_example.t t/202_example_Moose_POOP.t t/203_example.t t/204_example_w_DCS.t +t/205_example_w_TestDeep.t t/300_immutable_moose.t t/pod.t t/pod_coverage.t diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 8b9a24e..fa582e7 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -316,6 +316,40 @@ Suggestions for improvement are welcome. B The C type constraint does not work correctly in every occasion, please use it sparringly. + +=head2 Use with Other Constraint Modules + +This module should play fairly nicely with other constraint +modules with only some slight tweaking. The C clause +in types is expected to be a C reference which checks +it's first argument and returns a bool. Since most constraint +modules work in a similar way, it should be simple to adapt +them to work with Moose. + +For instance, this is how you could use it with +L to declare a completely new type. + + type 'HashOfArrayOfObjects' + => IsHashRef( + -keys => HasLength, + -values => IsArrayRef( IsObject )); + +For more examples see the F test file. + +Here is an example of using L and it's non-test +related C function. + + type 'ArrayOfHashOfBarsAndRandomNumbers' + => where { + eq_deeply($_, + array_each(subhashof({ + bar => isa('Bar'), + random_number => ignore() + }))) + }; + +For a complete example see the F +test file. =head1 FUNCTIONS diff --git a/t/204_example_w_DCS.t b/t/204_example_w_DCS.t index 6078c06..e8d1d25 100644 --- a/t/204_example_w_DCS.t +++ b/t/204_example_w_DCS.t @@ -5,6 +5,15 @@ use warnings; use Test::More; +=pod + +This tests how well Moose type constraints +play with Declare::Constraints::Simple. + +Pretty well if I do say so myself :) + +=cut + BEGIN { eval "use Declare::Constraints::Simple;"; plan skip_all => "Declare::Constraints::Simple is required for this test" if $@; diff --git a/t/205_example_w_TestDeep.t b/t/205_example_w_TestDeep.t new file mode 100644 index 0000000..53daaa8 --- /dev/null +++ b/t/205_example_w_TestDeep.t @@ -0,0 +1,83 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +=pod + +This tests how well Moose type constraints +play with Test::Deep. + +Its not as pretty as Declare::Constraints::Simple, +but it is not completely horrid either. + +=cut + +BEGIN { + eval "use Test::Deep;"; + plan skip_all => "Test::Deep is required for this test" if $@; + plan tests => 7; +} + +use Test::Exception; + +BEGIN { + use_ok('Moose'); + use_ok('Moose::Util::TypeConstraints'); +} + +{ + package Foo; + use Moose; + use Moose::Util::TypeConstraints; + + use Test::Deep qw[ + eq_deeply array_each subhashof ignore + ]; + + # define your own type ... + type 'ArrayOfHashOfBarsAndRandomNumbers' + => where { + eq_deeply($_, + array_each( + subhashof({ + bar => Test::Deep::isa('Bar'), + random_number => ignore() + }) + ) + ) + }; + + has 'bar' => ( + is => 'rw', + isa => 'ArrayOfHashOfBarsAndRandomNumbers', + ); + + package Bar; + use Moose; +} + +my $array_of_hashes = [ + { bar => Bar->new, random_number => 10 }, + { bar => Bar->new }, +]; + +my $foo; +lives_ok { + $foo = Foo->new('bar' => $array_of_hashes); +} '... construction succeeded'; +isa_ok($foo, 'Foo'); + +is_deeply($foo->bar, $array_of_hashes, '... got our value correctly'); + +dies_ok { + $foo->bar({}); +} '... validation failed correctly'; + +dies_ok { + $foo->bar([{ foo => 3 }]); +} '... validation failed correctly'; + +