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   182 use v5.12.0;
  11         49  
2 11     11   101 use warnings;
  11         39  
  11         471  
3             package Test::Routine::Compositor 0.031;
4             # ABSTRACT: the tool for turning test routines into runnable classes
5              
6 11     11   75 use Carp qw(confess);
  11         20  
  11         542  
7 11     11   1926 use Class::Load;
  11         37025  
  11         480  
8 11     11   2351 use Moose::Meta::Class;
  11         511274  
  11         445  
9 11     11   119 use Params::Util qw(_CLASS);
  11         23  
  11         566  
10 11     11   82 use Scalar::Util qw(blessed);
  11         44  
  11         511  
11              
12 11     11   2126 use namespace::clean;
  11         24947  
  11         99  
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   57 my ($class, $inv) = @_;
29              
30 21 50       71 confess "can't supply preconstructed object for test class construction"
31             if blessed $inv;
32              
33 21 100       91 $inv = [ $inv ] if _CLASS($inv);
34              
35 21         300 my @bases;
36             my @roles;
37              
38 21         59 for my $item (@$inv) {
39 25         108 Class::Load::load_class($item);
40 25 50       1104 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         1114 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       68 @bases = 'Moose::Object' unless @bases;
49              
50 21 100       129 my $new_class = Moose::Meta::Class->create_anon_class(
51             superclasses => \@bases,
52             cache => 1,
53             (@roles ? (roles => \@roles) : ()),
54             );
55              
56 21         105933 return $new_class->name;
57             }
58              
59             sub instance_builder {
60 22     22 0 65 my ($class, $inv, $arg) = @_;
61              
62 22 50 66     97 confess "can't supply preconstructed object and constructor arguments"
63             if $arg and blessed $inv;
64              
65 22 100   1   119 return sub { $inv } if blessed $inv;
  1         3  
66              
67 21         83 my $new_class = $class->_class_for($inv);
68 21   100     140 $arg //= {};
69              
70 21     21   137 return sub { $new_class->new($arg); };
  21         119  
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.031
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 released
93             in the last two to three years. (That is, if the most recently released
94             version is v5.40, then this module should work on both v5.40 and v5.38.)
95              
96             Although it may work on older versions of perl, no guarantee is made that the
97             minimum required version will not be increased. The version may be increased
98             for any reason, and there is no promise that patches will be accepted to lower
99             the minimum required perl.
100              
101             =head1 AUTHOR
102              
103             Ricardo Signes <cpan@semiotic.systems>
104              
105             =head1 COPYRIGHT AND LICENSE
106              
107             This software is copyright (c) 2010 by Ricardo Signes.
108              
109             This is free software; you can redistribute it and/or modify it under
110             the same terms as the Perl 5 programming language system itself.
111              
112             =cut