File Coverage

blib/lib/Test/Routine/Compositor.pm
Criterion Covered Total %
statement 43 49 87.7
branch 14 22 63.6
condition 4 10 40.0
subroutine 12 13 92.3
pod 0 1 0.0
total 73 95 76.8


line stmt bran cond sub pod time code
1 11     11   188 use v5.12.0;
  11         57  
2 11     11   63 use warnings;
  11         20  
  11         965  
3             package Test::Routine::Compositor 0.032;
4             # ABSTRACT: the tool for turning test routines into runnable classes
5              
6 11     11   84 use Carp qw(confess);
  11         25  
  11         836  
7 11     11   2501 use Class::Load;
  11         72991  
  11         569  
8 11     11   2956 use Moose::Meta::Class;
  11         613447  
  11         492  
9 11     11   73 use Params::Util qw(_CLASS);
  11         32  
  11         634  
10 11     11   59 use Scalar::Util qw(blessed);
  11         19  
  11         534  
11              
12 11     11   2450 use namespace::clean;
  11         28343  
  11         136  
13              
14             sub _invocant_for {
15 0     0   0 my ($self, $thing, $arg) = @_;
16              
17 0 0 0     0 confess "can't supply preconstructed object for running tests"
18             if $arg and blessed $thing;
19              
20 0 0       0 return $thing if blessed $thing;
21              
22 0   0     0 $arg //= {};
23 0         0 my $new_class = $self->_class_for($thing);
24 0         0 $new_class->name->new($arg);
25             }
26              
27             sub _class_for {
28 21     21   60 my ($class, $inv) = @_;
29              
30 21 50       85 confess "can't supply preconstructed object for test class construction"
31             if blessed $inv;
32              
33 21 100       154 $inv = [ $inv ] if _CLASS($inv);
34              
35 21         330 my @bases;
36             my @roles;
37              
38 21         66 for my $item (@$inv) {
39 25         137 Class::Load::load_class($item);
40 25 50       1516 my $target = $item->meta->isa('Moose::Meta::Class') ? \@bases
    100          
41             : $item->meta->isa('Moose::Meta::Role') ? \@roles
42             : confess "can't run tests for this weird thing: $item";
43              
44 25         1185 push @$target, $item;
45             }
46              
47 21 50       80 confess "can't build a test class from multiple base classes" if @bases > 1;
48 21 100       89 @bases = 'Moose::Object' unless @bases;
49              
50 21 100       176 my $new_class = Moose::Meta::Class->create_anon_class(
51             superclasses => \@bases,
52             cache => 1,
53             (@roles ? (roles => \@roles) : ()),
54             );
55              
56 21         133922 return $new_class->name;
57             }
58              
59             sub instance_builder {
60 22     22 0 76 my ($class, $inv, $arg) = @_;
61              
62 22 50 66     115 confess "can't supply preconstructed object and constructor arguments"
63             if $arg and blessed $inv;
64              
65 22 100   1   76 return sub { $inv } if blessed $inv;
  1         3  
66              
67 21         116 my $new_class = $class->_class_for($inv);
68 21   100     186 $arg //= {};
69              
70 21     21   141 return sub { $new_class->new($arg); };
  21         173  
71             }
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Test::Routine::Compositor - the tool for turning test routines into runnable classes
84              
85             =head1 VERSION
86              
87             version 0.032
88              
89             =head1 PERL VERSION
90              
91             This module should work on any version of perl still receiving updates from
92             the Perl 5 Porters. This means it should work on any version of perl
93             released in the last two to three years. (That is, if the most recently
94             released version is v5.40, then this module should work on both v5.40 and
95             v5.38.)
96              
97             Although it may work on older versions of perl, no guarantee is made that the
98             minimum required version will not be increased. The version may be increased
99             for any reason, and there is no promise that patches will be accepted to
100             lower the minimum required perl.
101              
102             =head1 AUTHOR
103              
104             Ricardo Signes <cpan@semiotic.systems>
105              
106             =head1 COPYRIGHT AND LICENSE
107              
108             This software is copyright (c) 2010 by Ricardo Signes.
109              
110             This is free software; you can redistribute it and/or modify it under
111             the same terms as the Perl 5 programming language system itself.
112              
113             =cut