diff --git a/lib/Test/Class/Moose/Role.pm b/lib/Test/Class/Moose/Role.pm index ea03d05..109d49c 100755 --- a/lib/Test/Class/Moose/Role.pm +++ b/lib/Test/Class/Moose/Role.pm @@ -6,6 +6,7 @@ use 5.10.0; use Carp qw/carp croak cluck/; use Test::Class::Moose::TagRegistry; +use Class::Load qw/try_load_class/; our $NO_CAN_HAZ_ATTRIBUTES; BEGIN { @@ -50,10 +51,15 @@ sub import { my ( $class, %arg_for ) = @_; my $caller = caller; + my $role_class = delete $arg_for{parameterized} ? + 'MooseX::Role::Parameterized' : 'Moose::Role'; + + try_load_class( $role_class ) + or croak "Can't load base role class $role_class"; my $preamble = <<"END"; package $caller; -use Moose::Role; +use $role_class; use Test::Most; use strict; @@ -118,6 +124,31 @@ And to consume it: 1; +Or, if you want to use L instead of +L: + + package TestsFor::Basic::Role; + + use Test::Class::Moose::Role parameterized => 1; + + parameter message => ( is => 'ro', default => "Picked up from role" ); + + role { + my $p = shift; + method test_in_a_role => sub { + pass $p->message; + } + } + +And to consume this: + + package TestsFor::Basic::ParameterizedRole; + use Test::Class::Moose; + + with 'TestsFor::Basic::Role' => { + message => "This is picked up from a parameterized role" + }; + Note that this cannot be consumed into classes and magically make them into test classes. You must still (at the present time) inherit from C to create a test suite. diff --git a/t/taglib/TestsFor/Basic/ParameterizedRole.pm b/t/taglib/TestsFor/Basic/ParameterizedRole.pm new file mode 100644 index 0000000..91ec983 --- /dev/null +++ b/t/taglib/TestsFor/Basic/ParameterizedRole.pm @@ -0,0 +1,26 @@ +package TestsFor::Basic::ParameterizedRole; + +use Class::Load qw/ try_load_class /; + +our $mrp_available; +BEGIN { $mrp_available = try_load_class( 'MooseX::Role::Parameterized' ) } + +use Test::Class::Moose::Role parameterized => $mrp_available; + +if ( $mrp_available ) { + parameter( message => ( is => 'ro', default => "Picked up from role" )); + role( sub { + my $p = shift; + + method( + test_in_a_parameterizedrole => sub { + pass $p->message; + } + ); + }) +} else { + diag "MooseX::Role::Parameterized not available. Skipping this role"; +} + + +1; diff --git a/t/taglib/TestsFor/Basic/WithParameterizedRole.pm b/t/taglib/TestsFor/Basic/WithParameterizedRole.pm new file mode 100644 index 0000000..02e6814 --- /dev/null +++ b/t/taglib/TestsFor/Basic/WithParameterizedRole.pm @@ -0,0 +1,12 @@ +package TestsFor::Basic::WithParameterizedRole; +use Test::Class::Moose; + +with 'TestsFor::Basic::ParameterizedRole' => { + message => "This is picked up from a parameterized role" +}; + +sub test_in_withparameterizedrole { + pass "Got here"; +} + +1; diff --git a/t/tags.t b/t/tags.t index 07b9e1b..3d3ea85 100755 --- a/t/tags.t +++ b/t/tags.t @@ -36,11 +36,12 @@ subtest 'Multiple included tags' => sub { / ], 'TestsFor::MultipleExclude' => [], - 'TestsFor::Basic::WithRole' => [ - qw/ - test_in_a_role_with_tags - / - ], + 'TestsFor::Basic::WithRole' => [ + qw/ + test_in_a_role_with_tags + / + ], + 'TestsFor::Basic::WithParameterizedRole' => [], } ); }; @@ -70,13 +71,18 @@ subtest 'Simple exluded tag' => sub { test_87801_3 / ], - 'TestsFor::Basic::WithRole' => [ - qw/ - test_in_a_role - test_in_a_role_with_tags - test_in_withrole - / - ], + 'TestsFor::Basic::WithRole' => [ + qw/ + test_in_a_role + test_in_a_role_with_tags + test_in_withrole + / + ], + 'TestsFor::Basic::WithParameterizedRole' => [ + $TestsFor::Basic::ParameterizedRole::mrp_available ? + 'test_in_a_parameterizedrole' : (), + qw/test_in_withparameterizedrole/, + ], } ); }; @@ -87,7 +93,8 @@ subtest 'Simple included tag' => sub { { 'TestsFor::Basic' => [], 'TestsFor::Basic::Subclass' => [qw/ test_augment /], 'TestsFor::MultipleExclude' => [], - 'TestsFor::Basic::WithRole' => [], + 'TestsFor::Basic::WithRole' => [], + 'TestsFor::Basic::WithParameterizedRole' => [], } ); }; @@ -104,7 +111,8 @@ subtest { 'TestsFor::Basic' => [], 'TestsFor::Basic::Subclass' => [], 'TestsFor::MultipleExclude' => [qw/test_87801_3/], - 'TestsFor::Basic::WithRole' => [], + 'TestsFor::Basic::WithRole' => [], + 'TestsFor::Basic::WithParameterizedRole' => [], } ); };