Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/Test/Class/Moose/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -118,6 +124,31 @@ And to consume it:

1;

Or, if you want to use L<MooseX::Role::Parameterized> instead of
L<Moose::Role>:

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<Test::Class::Moose> to create a test suite.
26 changes: 26 additions & 0 deletions t/taglib/TestsFor/Basic/ParameterizedRole.pm
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions t/taglib/TestsFor/Basic/WithParameterizedRole.pm
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 22 additions & 14 deletions t/tags.t
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
}
);
};
Expand Down Expand Up @@ -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/,
],
}
);
};
Expand All @@ -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' => [],
}
);
};
Expand All @@ -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' => [],
}
);
};
Expand Down