Test-Mojo-WithRoles

 view release on metacpan or  search on metacpan

lib/Test/Mojo/WithRoles.pm  view on Meta::CPAN

package Test::Mojo::WithRoles;

use Mojo::Base -strict;

our $VERSION = '0.02';
$VERSION = eval $VERSION;

use Role::Tiny ();
use Test::Mojo;

use Mojo::JSON 'j';

sub import {
  my ($class, @roles) = @_;
  @roles = map { s/^\+// ? $_ : "Test::Mojo::Role::$_" } @roles;
  $^H{'Test::Mojo::WithRoles/enabled'} = j(\@roles);
}

sub unimport {
  my ($class) = @_;
  $^H{'Test::Mojo::WithRoles/enabled'} = '[]';
}

sub new {
  my $class = shift;
  my $hints = (caller(0))[10];
  my $roles = j($hints->{'Test::Mojo::WithRoles/enabled'} || '[]');
  @$roles = 'Test::Mojo::Role::Null' unless @$roles;
  return Role::Tiny->create_class_with_roles('Test::Mojo', @$roles)->new(@_);
}

1;

=head1 NAME

Test::Mojo::WithRoles - Use Test::Mojo roles cleanly and safely

=head1 SYNOPSIS

  package Test::Mojo::Role::MyRole;

  use Role::Tiny;

  sub is_awesome {
    my ($t, ...) = @_;
    # do some test
  }

  ---

  # myapp.t

  use Test::More;
  use Test::Mojo::WithRoles 'MyRole';
  my $t = Test::Mojo::WithRoles->new('MyApp');

  $t->get_ok(...)
    ->is_awesome(...);

  done_testing;

=head1 DESCRIPTION

L<Test::Mojo::WithRoles> builds composite subclasses of L<Test::Mojo> based on a lexically specified set of roles.
This is easy to use and plays nicely with others.

Of course this is all just sugar for the mechanisms provided by L<Role::Tiny>.

=head1 IMPORTING

  {
    use Test::Mojo::WithRoles qw/MyRole +Test::MyRole/;
    my $t = Test::Mojo::WithRoles->new('MyApp');
    $t->does('Test::Mojo::Role::MyRole'); # true
    $t->does('Test::MyRole'); # true
  }

  my $t = Test::Mojo::WithRoles->new;
  $t->does('Test::Mojo::Role::MyRole'); # false

Pass a list of roles when you import L<Test::Mojo::WithRoles>.



( run in 1.991 second using v1.01-cache-2.11-cpan-8dfa8b56332 )