From: Dave Rolsky Date: Mon, 29 Nov 2010 22:34:17 +0000 (-0600) Subject: Convert to Test::Fatal and tidy resulting code X-Git-Tag: v0.15~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=37088308c844e2ea22f3b2af621b028d029fe759;p=gitmo%2FMooseX-Params-Validate.git Convert to Test::Fatal and tidy resulting code --- diff --git a/Makefile.PL b/Makefile.PL index 984f6e2..7f906c2 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,8 +13,8 @@ requires 'Params::Validate' => '0.88'; requires 'Scalar::Util' => '0'; requires 'Sub::Exporter' => '0'; -build_requires 'Test::More' => '0.88'; -build_requires 'Test::Exception' => '0.21'; +build_requires 'Test::Fatal' => '0.001'; +build_requires 'Test::More' => '0.88'; license 'Perl'; diff --git a/t/001_basic.t b/t/001_basic.t index 2b5a53d..97203db 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal; { package Roles::Blah; @@ -45,7 +45,7 @@ use Test::Exception; my %params = validated_hash( \@_, foo => { - isa => subtype( 'Object' => where { $_->isa('Foo') } ), + isa => subtype( 'Object' => where { $_->isa('Foo') } ), optional => 1 }, bar => { does => 'Roles::Blah', optional => 1 }, @@ -77,46 +77,77 @@ my $foo = Foo->new; isa_ok( $foo, 'Foo' ); is( $foo->foo, 'Horray for Moose!', '... got the right return value' ); -is( $foo->foo( bar => 'Rolsky' ), 'Horray for Rolsky!', - '... got the right return value' ); +is( + $foo->foo( bar => 'Rolsky' ), 'Horray for Rolsky!', + '... got the right return value' +); is( $foo->baz( foo => $foo ), $foo, '... foo param must be a Foo instance' ); -throws_ok { $foo->baz( foo => 10 ) } qr/\QThe 'foo' parameter ("10")/, - '... the foo param in &baz must be a Foo instance'; -throws_ok { $foo->baz( foo => "foo" ) } qr/\QThe 'foo' parameter ("foo")/, - '... the foo param in &baz must be a Foo instance'; -throws_ok { $foo->baz( foo => [] ) } qr/\QThe 'foo' parameter/, - '... the foo param in &baz must be a Foo instance'; +like( + exception { $foo->baz( foo => 10 ) }, qr/\QThe 'foo' parameter ("10")/, + '... the foo param in &baz must be a Foo instance' +); +like( + exception { $foo->baz( foo => "foo" ) }, + qr/\QThe 'foo' parameter ("foo")/, + '... the foo param in &baz must be a Foo instance' +); +like( + exception { $foo->baz( foo => [] ) }, qr/\QThe 'foo' parameter/, + '... the foo param in &baz must be a Foo instance' +); is( $foo->baz( bar => $foo ), $foo, '... bar param must do Roles::Blah' ); -throws_ok { $foo->baz( bar => 10 ) } qr/\QThe 'bar' parameter ("10")/, -'... the bar param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( bar => "foo" ) } qr/\QThe 'bar' parameter ("foo")/, -'... the bar param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( bar => [] ) } qr/\QThe 'bar' parameter/, -'... the bar param in &baz must be do Roles::Blah'; +like( + exception { $foo->baz( bar => 10 ) }, qr/\QThe 'bar' parameter ("10")/, + '... the bar param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( bar => "foo" ) }, + qr/\QThe 'bar' parameter ("foo")/, + '... the bar param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( bar => [] ) }, qr/\QThe 'bar' parameter/, + '... the bar param in &baz must be do Roles::Blah' +); is( $foo->baz( boo => $foo ), $foo, '... boo param must do Roles::Blah' ); -throws_ok { $foo->baz( boo => 10 ) } qr/\QThe 'boo' parameter ("10")/, -'... the boo param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( boo => "foo" ) } qr/\QThe 'boo' parameter ("foo")/, -'... the boo param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( boo => [] ) } qr/\QThe 'boo' parameter/, -'... the boo param in &baz must be do Roles::Blah'; - -throws_ok { $foo->bar } qr/\QMandatory parameter 'foo'/, - '... bar has a required param'; -throws_ok { $foo->bar( foo => 10 ) } qr/\QThe 'foo' parameter ("10")/, - '... the foo param in &bar must be a Foo instance'; -throws_ok { $foo->bar( foo => "foo" ) } qr/\QThe 'foo' parameter ("foo")/, - '... the foo param in &bar must be a Foo instance'; -throws_ok { $foo->bar( foo => [] ) } qr/\QThe 'foo' parameter/, - '... the foo param in &bar must be a Foo instance'; -throws_ok { $foo->bar( baz => [] ) } qr/\QMandatory parameter 'foo'/,, - '... bar has a required foo param'; +like( + exception { $foo->baz( boo => 10 ) }, qr/\QThe 'boo' parameter ("10")/, + '... the boo param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( boo => "foo" ) }, + qr/\QThe 'boo' parameter ("foo")/, + '... the boo param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( boo => [] ) }, qr/\QThe 'boo' parameter/, + '... the boo param in &baz must be do Roles::Blah' +); + +like( + exception { $foo->bar }, qr/\QMandatory parameter 'foo'/, + '... bar has a required param' +); +like( + exception { $foo->bar( foo => 10 ) }, qr/\QThe 'foo' parameter ("10")/, + '... the foo param in &bar must be a Foo instance' +); +like( + exception { $foo->bar( foo => "foo" ) }, + qr/\QThe 'foo' parameter ("foo")/, + '... the foo param in &bar must be a Foo instance' +); +like( + exception { $foo->bar( foo => [] ) }, qr/\QThe 'foo' parameter/, + '... the foo param in &bar must be a Foo instance' +); +like( exception { $foo->bar( baz => [] ) }, qr/\QMandatory parameter 'foo'/ ); is_deeply( $foo->bar( foo => $foo ), @@ -136,18 +167,25 @@ is_deeply( '... the foo param and baz param in &bar got a correct args' ); -throws_ok { $foo->bar( foo => $foo, baz => undef ) } -qr/\QThe 'baz' parameter (undef)/, - '... baz requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( foo => $foo, baz => 10 ) } -qr/\QThe 'baz' parameter ("10")/, - '... baz requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( foo => $foo, baz => 'Foo' ) } -qr/\QThe 'baz' parameter ("Foo")/, - '... baz requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( foo => $foo, baz => \( my $var ) ) } -qr/\QThe 'baz' parameter/, - '... baz requires a ArrayRef | HashRef'; +like( + exception { $foo->bar( foo => $foo, baz => undef ) }, + qr/\QThe 'baz' parameter (undef)/, + '... baz requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( foo => $foo, baz => 10 ) }, + qr/\QThe 'baz' parameter ("10")/, + '... baz requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( foo => $foo, baz => 'Foo' ) }, + qr/\QThe 'baz' parameter ("Foo")/, + '... baz requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( foo => $foo, baz => \( my $var ) ) }, + qr/\QThe 'baz' parameter/, '... baz requires a ArrayRef | HashRef' +); is_deeply( $foo->bar( foo => $foo, gorch => [ 1, 2, 3 ] ), @@ -155,28 +193,40 @@ is_deeply( '... the foo param in &bar got a Foo instance' ); -throws_ok { $foo->bar( foo => $foo, gorch => undef ) } -qr/\QThe 'gorch' parameter (undef)/, - '... gorch requires a ArrayRef[Int]'; -throws_ok { $foo->bar( foo => $foo, gorch => 10 ) } -qr/\QThe 'gorch' parameter ("10")/, - '... gorch requires a ArrayRef[Int]'; -throws_ok { $foo->bar( foo => $foo, gorch => 'Foo' ) } -qr/\QThe 'gorch' parameter ("Foo")/, - '... gorch requires a ArrayRef[Int]'; -throws_ok { $foo->bar( foo => $foo, gorch => \( my $var ) ) } -qr/\QThe 'gorch' parameter/, - '... gorch requires a ArrayRef[Int]'; -throws_ok { $foo->bar( foo => $foo, gorch => [qw/one two three/] ) } -qr/\QThe 'gorch' parameter/, - '... gorch requires a ArrayRef[Int]'; - -throws_ok { $foo->quux( foo => '123456790' ) } -qr/\QThe 'foo' parameter\E.+\Qchecking type constraint/, -'... foo parameter must be an ArrayRef'; - -throws_ok { $foo->quux( foo => [ 1, 2, 3, 4 ] ) } -qr/\QThe 'foo' parameter\E.+\Qsome random callback/, -'... foo parameter additional callback requires that arrayref be 0-2 elements'; +like( + exception { $foo->bar( foo => $foo, gorch => undef ) }, + qr/\QThe 'gorch' parameter (undef)/, + '... gorch requires a ArrayRef[Int]' +); +like( + exception { $foo->bar( foo => $foo, gorch => 10 ) }, + qr/\QThe 'gorch' parameter ("10")/, + '... gorch requires a ArrayRef[Int]' +); +like( + exception { $foo->bar( foo => $foo, gorch => 'Foo' ) }, + qr/\QThe 'gorch' parameter ("Foo")/, + '... gorch requires a ArrayRef[Int]' +); +like( + exception { $foo->bar( foo => $foo, gorch => \( my $var ) ) }, + qr/\QThe 'gorch' parameter/, '... gorch requires a ArrayRef[Int]' +); +like( + exception { $foo->bar( foo => $foo, gorch => [qw/one two three/] ) }, + qr/\QThe 'gorch' parameter/, '... gorch requires a ArrayRef[Int]' +); + +like( + exception { $foo->quux( foo => '123456790' ) }, + qr/\QThe 'foo' parameter\E.+\Qchecking type constraint/, + '... foo parameter must be an ArrayRef' +); + +like( + exception { $foo->quux( foo => [ 1, 2, 3, 4 ] ) }, + qr/\QThe 'foo' parameter\E.+\Qsome random callback/, + '... foo parameter additional callback requires that arrayref be 0-2 elements' +); done_testing(); diff --git a/t/002_basic_list.t b/t/002_basic_list.t index 6759847..75e0629 100644 --- a/t/002_basic_list.t +++ b/t/002_basic_list.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal; { package Roles::Blah; @@ -44,7 +44,7 @@ use Test::Exception; my ( $foo, $bar, $boo ) = validated_list( \@_, foo => { - isa => subtype( 'Object' => where { $_->isa('Foo') } ), + isa => subtype( 'Object' => where { $_->isa('Foo') } ), optional => 1 }, bar => { does => 'Roles::Blah', optional => 1 }, @@ -61,46 +61,77 @@ my $foo = Foo->new; isa_ok( $foo, 'Foo' ); is( $foo->foo, 'Horray for Moose!', '... got the right return value' ); -is( $foo->foo( bar => 'Rolsky' ), 'Horray for Rolsky!', - '... got the right return value' ); +is( + $foo->foo( bar => 'Rolsky' ), 'Horray for Rolsky!', + '... got the right return value' +); is( $foo->baz( foo => $foo ), $foo, '... foo param must be a Foo instance' ); -throws_ok { $foo->baz( foo => 10 ) } qr/\QThe 'foo' parameter ("10")/, - '... the foo param in &baz must be a Foo instance'; -throws_ok { $foo->baz( foo => "foo" ) } qr/\QThe 'foo' parameter ("foo")/, - '... the foo param in &baz must be a Foo instance'; -throws_ok { $foo->baz( foo => [] ) } qr/\QThe 'foo' parameter/, - '... the foo param in &baz must be a Foo instance'; +like( + exception { $foo->baz( foo => 10 ) }, qr/\QThe 'foo' parameter ("10")/, + '... the foo param in &baz must be a Foo instance' +); +like( + exception { $foo->baz( foo => "foo" ) }, + qr/\QThe 'foo' parameter ("foo")/, + '... the foo param in &baz must be a Foo instance' +); +like( + exception { $foo->baz( foo => [] ) }, qr/\QThe 'foo' parameter/, + '... the foo param in &baz must be a Foo instance' +); is( $foo->baz( bar => $foo ), $foo, '... bar param must do Roles::Blah' ); -throws_ok { $foo->baz( bar => 10 ) } qr/\QThe 'bar' parameter ("10")/, -'... the bar param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( bar => "foo" ) } qr/\QThe 'bar' parameter ("foo")/, -'... the bar param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( bar => [] ) } qr/\QThe 'bar' parameter/, -'... the bar param in &baz must be do Roles::Blah'; +like( + exception { $foo->baz( bar => 10 ) }, qr/\QThe 'bar' parameter ("10")/, + '... the bar param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( bar => "foo" ) }, + qr/\QThe 'bar' parameter ("foo")/, + '... the bar param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( bar => [] ) }, qr/\QThe 'bar' parameter/, + '... the bar param in &baz must be do Roles::Blah' +); is( $foo->baz( boo => $foo ), $foo, '... boo param must do Roles::Blah' ); -throws_ok { $foo->baz( boo => 10 ) } qr/\QThe 'boo' parameter ("10")/, -'... the boo param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( boo => "foo" ) } qr/\QThe 'boo' parameter ("foo")/, -'... the boo param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( boo => [] ) } qr/\QThe 'boo' parameter/, -'... the boo param in &baz must be do Roles::Blah'; - -throws_ok { $foo->bar } qr/\QMandatory parameter 'foo'/, - '... bar has a required param'; -throws_ok { $foo->bar( foo => 10 ) } qr/\QThe 'foo' parameter ("10")/, - '... the foo param in &bar must be a Foo instance'; -throws_ok { $foo->bar( foo => "foo" ) } qr/\QThe 'foo' parameter ("foo")/, - '... the foo param in &bar must be a Foo instance'; -throws_ok { $foo->bar( foo => [] ) } qr/\QThe 'foo' parameter/, - '... the foo param in &bar must be a Foo instance'; -throws_ok { $foo->bar( baz => [] ) } qr/\QMandatory parameter 'foo'/,, - '... bar has a required foo param'; +like( + exception { $foo->baz( boo => 10 ) }, qr/\QThe 'boo' parameter ("10")/, + '... the boo param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( boo => "foo" ) }, + qr/\QThe 'boo' parameter ("foo")/, + '... the boo param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( boo => [] ) }, qr/\QThe 'boo' parameter/, + '... the boo param in &baz must be do Roles::Blah' +); + +like( + exception { $foo->bar }, qr/\QMandatory parameter 'foo'/, + '... bar has a required param' +); +like( + exception { $foo->bar( foo => 10 ) }, qr/\QThe 'foo' parameter ("10")/, + '... the foo param in &bar must be a Foo instance' +); +like( + exception { $foo->bar( foo => "foo" ) }, + qr/\QThe 'foo' parameter ("foo")/, + '... the foo param in &bar must be a Foo instance' +); +like( + exception { $foo->bar( foo => [] ) }, qr/\QThe 'foo' parameter/, + '... the foo param in &bar must be a Foo instance' +); +like( exception { $foo->bar( baz => [] ) }, qr/\QMandatory parameter 'foo'/ ); is_deeply( $foo->bar( foo => $foo ), @@ -120,17 +151,24 @@ is_deeply( '... the foo param and baz param in &bar got a correct args' ); -throws_ok { $foo->bar( foo => $foo, baz => undef ) } -qr/\QThe 'baz' parameter (undef)/, - '... baz requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( foo => $foo, baz => 10 ) } -qr/\QThe 'baz' parameter ("10")/, - '... baz requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( foo => $foo, baz => 'Foo' ) } -qr/\QThe 'baz' parameter ("Foo")/, - '... baz requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( foo => $foo, baz => \( my $var ) ) } -qr/\QThe 'baz' parameter/, - '... baz requires a ArrayRef | HashRef'; +like( + exception { $foo->bar( foo => $foo, baz => undef ) }, + qr/\QThe 'baz' parameter (undef)/, + '... baz requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( foo => $foo, baz => 10 ) }, + qr/\QThe 'baz' parameter ("10")/, + '... baz requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( foo => $foo, baz => 'Foo' ) }, + qr/\QThe 'baz' parameter ("Foo")/, + '... baz requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( foo => $foo, baz => \( my $var ) ) }, + qr/\QThe 'baz' parameter/, '... baz requires a ArrayRef | HashRef' +); done_testing(); diff --git a/t/003_nocache_flag.t b/t/003_nocache_flag.t index a1ae689..5ef7bab 100644 --- a/t/003_nocache_flag.t +++ b/t/003_nocache_flag.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal; { package Foo; @@ -21,19 +21,28 @@ use Test::Exception; my $foo = Foo->new; isa_ok( $foo, 'Foo' ); -lives_ok { - $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); -} -'... successfully applied the parameter validation'; - -lives_ok { - $foo->bar( [ baz => [ 1, 2, 3 ] ], { baz => { isa => 'ArrayRef' } } ); -} -'... successfully applied the parameter validation (look mah no cache)'; - -lives_ok { - $foo->bar( [ baz => { one => 1 } ], { baz => { isa => 'HashRef' } } ); -} -'... successfully applied the parameter validation (look mah no cache) (just checkin)'; +is( + exception { + $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); + }, + undef, + '... successfully applied the parameter validation' +); + +is( + exception { + $foo->bar( [ baz => [ 1, 2, 3 ] ], { baz => { isa => 'ArrayRef' } } ); + }, + undef, + '... successfully applied the parameter validation (look mah no cache)' +); + +is( + exception { + $foo->bar( [ baz => { one => 1 } ], { baz => { isa => 'HashRef' } } ); + }, + undef, + '... successfully applied the parameter validation (look mah no cache) (just checkin)' +); done_testing(); diff --git a/t/004_custom_cache_key.t b/t/004_custom_cache_key.t index a4c3110..e444690 100644 --- a/t/004_custom_cache_key.t +++ b/t/004_custom_cache_key.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal; use Scalar::Util; { @@ -23,32 +23,50 @@ use Scalar::Util; my $foo = Foo->new; isa_ok( $foo, 'Foo' ); -lives_ok { - $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); -} -'... successfully applied the parameter validation'; +is( + exception { + $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); + }, + undef, + '... successfully applied the parameter validation' +); -throws_ok { - $foo->bar( [ baz => [ 1, 2, 3 ] ], { baz => { isa => 'ArrayRef' } } ); -} qr/\QThe 'baz' parameter/, -'... successfully re-used the parameter validation for this instance'; +like( + exception { + $foo->bar( [ baz => [ 1, 2, 3 ] ], { baz => { isa => 'ArrayRef' } } ); + }, + qr/\QThe 'baz' parameter/, + '... successfully re-used the parameter validation for this instance' +); my $foo2 = Foo->new; isa_ok( $foo2, 'Foo' ); -lives_ok { - $foo2->bar( [ baz => [ 1, 2, 3 ] ], { baz => { isa => 'ArrayRef' } } ); -} -'... successfully applied the parameter validation'; +is( + exception { + $foo2->bar( + [ baz => [ 1, 2, 3 ] ], + { baz => { isa => 'ArrayRef' } } + ); + }, + undef, + '... successfully applied the parameter validation' +); -throws_ok { - $foo2->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); -} qr/\QThe 'baz' parameter/, -'... successfully re-used the parameter validation for this instance'; +like( + exception { + $foo2->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); + }, + qr/\QThe 'baz' parameter/, + '... successfully re-used the parameter validation for this instance' +); -lives_ok { - $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); -} -'... successfully applied the parameter validation (just checking)'; +is( + exception { + $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); + }, + undef, + '... successfully applied the parameter validation (just checking)' +); done_testing(); diff --git a/t/005_coercion.t b/t/005_coercion.t index 4e36c05..9172fae 100644 --- a/t/005_coercion.t +++ b/t/005_coercion.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal; # Note that setting coerce => 1 for the Num type tests that we don't try to do # coercions for a type which doesn't have any coercions. @@ -31,7 +31,7 @@ use Test::Exception; # added to test 'optional' on validated_hash sub baropt { - my $self = shift; + my $self = shift; my %params = validated_hash( \@_, size1 => { isa => 'Size', coerce => 1, optional => 1 }, @@ -41,7 +41,6 @@ use Test::Exception; [ $params{size1}, $params{size2}, $params{number} ]; } - sub baz { my $self = shift; my ( $size1, $size2, $number ) = validated_list( @@ -53,7 +52,6 @@ use Test::Exception; [ $size1, $size2, $number ]; } - sub quux { my $self = shift; my ( $size1, $size2, $number ) = validated_list( @@ -92,13 +90,15 @@ is_deeply( 'got the return value right with coercions for size1' ); -throws_ok { $foo->bar( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) } -qr/\QThe 'size2' parameter/, - '... the size2 param cannot be coerced'; +like( + exception { $foo->bar( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) } + , qr/\QThe 'size2' parameter/, '... the size2 param cannot be coerced' ); -throws_ok { $foo->bar( size1 => 30, size2 => 10, number => 'something' ) } -qr/\QThe 'number' parameter/, - '... the number param cannot be coerced because there is no coercion defined for Num'; +like( + exception { $foo->bar( size1 => 30, size2 => 10, number => 'something' ) } + , qr/\QThe 'number' parameter/, + '... the number param cannot be coerced because there is no coercion defined for Num' +); is_deeply( $foo->baz( size1 => 10, size2 => 20, number => 30 ), @@ -112,13 +112,15 @@ is_deeply( 'got the return value right with coercions for size1' ); -throws_ok { $foo->baz( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) } -qr/\QThe 'size2' parameter/, - '... the size2 param cannot be coerced'; +like( + exception { $foo->baz( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) } + , qr/\QThe 'size2' parameter/, '... the size2 param cannot be coerced' ); -throws_ok { $foo->baz( size1 => 30, size2 => 10, number => 'something' ) } -qr/\QThe 'number' parameter/, - '... the number param cannot be coerced'; +like( + exception { $foo->baz( size1 => 30, size2 => 10, number => 'something' ) } + , qr/\QThe 'number' parameter/, + '... the number param cannot be coerced' +); is_deeply( $foo->baropt( size2 => 4 ), @@ -144,10 +146,10 @@ is_deeply( 'got the return value right with coercion for the first param' ); -throws_ok { $foo->ran_out( [ 1, 2 ], [ 1, 2 ] ) } -qr/\QParameter #2/, - '... did not attempt to coerce the second parameter'; - +like( + exception { $foo->ran_out( [ 1, 2 ], [ 1, 2 ] ) }, qr/\QParameter #2/, + '... did not attempt to coerce the second parameter' +); is_deeply( $foo->ran_out(), diff --git a/t/006_not_moose.t b/t/006_not_moose.t index f48b685..6d91e66 100644 --- a/t/006_not_moose.t +++ b/t/006_not_moose.t @@ -4,7 +4,6 @@ use strict; use warnings; use Test::More; -use Test::Exception; eval <<'EOF'; { @@ -17,6 +16,9 @@ is( $@, '', 'loading MX::Params::Validate in a non-Moose class does not blow up' ); -ok( Foo->can('validated_hash'), 'validated_hash() sub was added to Foo package' ); +ok( + Foo->can('validated_hash'), + 'validated_hash() sub was added to Foo package' +); done_testing(); diff --git a/t/007_deprecated.t b/t/007_deprecated.t index b079957..38ad1d1 100644 --- a/t/007_deprecated.t +++ b/t/007_deprecated.t @@ -2,14 +2,12 @@ use strict; use warnings; use Test::More; -use Test::Exception; { package Foo; use Moose; use MooseX::Params::Validate qw( :deprecated ); - } ok( Foo->can('validate'), ':deprecated tag exports validate' ); diff --git a/t/008_positional.t b/t/008_positional.t index 6c05da0..df23dbe 100644 --- a/t/008_positional.t +++ b/t/008_positional.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal; { package Roles::Blah; @@ -45,8 +45,7 @@ use Test::Exception; my $self = shift; return [ pos_validated_list( - \@_, - { + \@_, { isa => subtype( 'Object' => where { $_->isa('Foo') } ), optional => 1 }, @@ -65,43 +64,76 @@ isa_ok( $foo, 'Foo' ); is( $foo->baz($foo)->[0], $foo, '... first param must be a Foo instance' ); -throws_ok { $foo->baz(10) } qr/\QParameter #1 ("10")/, - '... the first param in &baz must be a Foo instance'; -throws_ok { $foo->baz('foo') } qr/\QParameter #1 ("foo")/, - '... the first param in &baz must be a Foo instance'; -throws_ok { $foo->baz( [] ) } qr/\QParameter #1/, - '... the first param in &baz must be a Foo instance'; - -is( $foo->baz( $foo, $foo )->[1], $foo, - '... second param must do Roles::Blah' ); - -throws_ok { $foo->baz( $foo, 10 ) } qr/\QParameter #2 ("10")/, - '... the second param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( $foo, 'foo' ) } qr/\QParameter #2 ("foo")/, - '... the second param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( $foo, [] ) } qr/\QParameter #2/, - '... the second param in &baz must be do Roles::Blah'; - -is( $foo->baz( $foo, $foo, $foo )->[2], $foo, - '... third param must do Roles::Blah' ); - -throws_ok { $foo->baz( $foo, $foo, 10 ) } qr/\QParameter #3 ("10")/, - '... the third param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( $foo, $foo, "foo" ) } qr/\QParameter #3 ("foo")/, - '... the third param in &baz must be do Roles::Blah'; -throws_ok { $foo->baz( $foo, $foo, [] ) } qr/\QParameter #3/, - '... the third param in &baz must be do Roles::Blah'; - -throws_ok { $foo->bar } qr/\Q0 parameters were passed/, - '... bar has a required params'; -throws_ok { $foo->bar(10) } qr/\QParameter #1 ("10")/, - '... the first param in &bar must be a Foo instance'; -throws_ok { $foo->bar('foo') } qr/\QParameter #1 ("foo")/, - '... the first param in &bar must be a Foo instance'; -throws_ok { $foo->bar( [] ) } qr/\QParameter #1/, - '... the first param in &bar must be a Foo instance'; -throws_ok { $foo->bar() } qr/\Q0 parameters were passed/, - '... bar has a required first param'; +like( + exception { $foo->baz(10) }, qr/\QParameter #1 ("10")/, + '... the first param in &baz must be a Foo instance' +); +like( + exception { $foo->baz('foo') }, qr/\QParameter #1 ("foo")/, + '... the first param in &baz must be a Foo instance' +); +like( + exception { $foo->baz( [] ) }, qr/\QParameter #1/, + '... the first param in &baz must be a Foo instance' +); + +is( + $foo->baz( $foo, $foo )->[1], $foo, + '... second param must do Roles::Blah' +); + +like( + exception { $foo->baz( $foo, 10 ) }, qr/\QParameter #2 ("10")/, + '... the second param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( $foo, 'foo' ) }, qr/\QParameter #2 ("foo")/, + '... the second param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( $foo, [] ) }, qr/\QParameter #2/, + '... the second param in &baz must be do Roles::Blah' +); + +is( + $foo->baz( $foo, $foo, $foo )->[2], $foo, + '... third param must do Roles::Blah' +); + +like( + exception { $foo->baz( $foo, $foo, 10 ) }, qr/\QParameter #3 ("10")/, + '... the third param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( $foo, $foo, "foo" ) }, + qr/\QParameter #3 ("foo")/, + '... the third param in &baz must be do Roles::Blah' +); +like( + exception { $foo->baz( $foo, $foo, [] ) }, qr/\QParameter #3/, + '... the third param in &baz must be do Roles::Blah' +); + +like( + exception { $foo->bar }, qr/\Q0 parameters were passed/, + '... bar has a required params' +); +like( + exception { $foo->bar(10) }, qr/\QParameter #1 ("10")/, + '... the first param in &bar must be a Foo instance' +); +like( + exception { $foo->bar('foo') }, qr/\QParameter #1 ("foo")/, + '... the first param in &bar must be a Foo instance' +); +like( + exception { $foo->bar( [] ) }, qr/\QParameter #1/, + '... the first param in &bar must be a Foo instance' +); +like( + exception { $foo->bar() }, qr/\Q0 parameters were passed/, + '... bar has a required first param' +); is_deeply( $foo->bar($foo), @@ -121,14 +153,22 @@ is_deeply( '... the first param and baz param in &bar got correct args' ); -throws_ok { $foo->bar( $foo, undef ) } qr/\QParameter #2 (undef)/, - '... second param requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( $foo, 10 ) } qr/\QParameter #2 ("10")/, - '... second param requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( $foo, 'Foo' ) } qr/\QParameter #2 ("Foo")/, - '... second param requires a ArrayRef | HashRef'; -throws_ok { $foo->bar( $foo, \( my $var ) ) } qr/\QParameter #2/, - '... second param requires a ArrayRef | HashRef'; +like( + exception { $foo->bar( $foo, undef ) }, qr/\QParameter #2 (undef)/, + '... second param requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( $foo, 10 ) }, qr/\QParameter #2 ("10")/, + '... second param requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( $foo, 'Foo' ) }, qr/\QParameter #2 ("Foo")/, + '... second param requires a ArrayRef | HashRef' +); +like( + exception { $foo->bar( $foo, \( my $var ) ) }, qr/\QParameter #2/, + '... second param requires a ArrayRef | HashRef' +); is_deeply( $foo->bar( $foo, {}, [ 1, 2, 3 ] ), @@ -136,15 +176,25 @@ is_deeply( '... the first param in &bar got a Foo instance' ); -throws_ok { $foo->bar( $foo, {}, undef ) } qr/\QParameter #3 (undef)/, -'... third param a ArrayRef[Int]'; -throws_ok { $foo->bar( $foo, {}, 10 ) } qr/\QParameter #3 ("10")/, -'... third param a ArrayRef[Int]'; -throws_ok { $foo->bar( $foo, {}, 'Foo' ) } qr/\QParameter #3 ("Foo")/, -'... third param a ArrayRef[Int]'; -throws_ok { $foo->bar( $foo, {}, \( my $var ) ) } qr/\QParameter #3/, -'... third param a ArrayRef[Int]'; -throws_ok { $foo->bar( $foo, {}, [qw/one two three/] ) } qr/\QParameter #3/, -'... third param a ArrayRef[Int]'; +like( + exception { $foo->bar( $foo, {}, undef ) }, qr/\QParameter #3 (undef)/, + '... third param a ArrayRef[Int]' +); +like( + exception { $foo->bar( $foo, {}, 10 ) }, qr/\QParameter #3 ("10")/, + '... third param a ArrayRef[Int]' +); +like( + exception { $foo->bar( $foo, {}, 'Foo' ) }, qr/\QParameter #3 ("Foo")/, + '... third param a ArrayRef[Int]' +); +like( + exception { $foo->bar( $foo, {}, \( my $var ) ) }, qr/\QParameter #3/, + '... third param a ArrayRef[Int]' +); +like( + exception { $foo->bar( $foo, {}, [qw/one two three/] ) }, + qr/\QParameter #3/, '... third param a ArrayRef[Int]' +); done_testing(); diff --git a/t/009_wrapped.t b/t/009_wrapped.t index 40d875c..e328b5d 100644 --- a/t/009_wrapped.t +++ b/t/009_wrapped.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use Test::Exception; +use Test::Fatal; { package Foo; @@ -15,7 +15,7 @@ use Test::Exception; my $self = shift; my %params = validated_hash( \@_, - foo => { isa => 'Str' }, + foo => { isa => 'Str' }, ); return $params{foo}; } @@ -28,9 +28,9 @@ use Test::Exception; my @args = ( bar => delete $p{bar} ); my %params = validated_hash( - \@args, - bar => { isa => 'Str' }, - ); + \@args, + bar => { isa => 'Str' }, + ); $params{bar}, $self->$orig(%p); }; @@ -43,9 +43,9 @@ use Test::Exception; my @args = ( quux => delete $p{quux} ); my %params = validated_hash( - \@args, - quux => { isa => 'Str' }, - ); + \@args, + quux => { isa => 'Str' }, + ); $params{quux}, $self->$orig(%p); }; @@ -54,13 +54,17 @@ use Test::Exception; { my $foo = Foo->new; - is_deeply( [ $foo->foo( foo => 1, bar => 2, quux => 3 ) ], - [ 3, 2, 1 ], - 'multiple around wrappers can safely be cached' ); - - is_deeply( [ $foo->foo( foo => 1, bar => 2, quux => 3 ) ], - [ 3, 2, 1 ], - 'multiple around wrappers can safely be cached (2nd time)' ); + is_deeply( + [ $foo->foo( foo => 1, bar => 2, quux => 3 ) ], + [ 3, 2, 1 ], + 'multiple around wrappers can safely be cached' + ); + + is_deeply( + [ $foo->foo( foo => 1, bar => 2, quux => 3 ) ], + [ 3, 2, 1 ], + 'multiple around wrappers can safely be cached (2nd time)' + ); } done_testing(); diff --git a/t/010_overloaded.t b/t/010_overloaded.t index b939243..165d5a4 100644 --- a/t/010_overloaded.t +++ b/t/010_overloaded.t @@ -30,8 +30,10 @@ use warnings; # 1.10.100 => 0001.0010.0100 my $id = $args{padded} - ? join( '.', - map { sprintf( "%04d", $_ ) } split( /\./, $self->id ) ) + ? join( + '.', + map { sprintf( "%04d", $_ ) } split( /\./, $self->id ) + ) : $self->id; return $id;