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 10     10   134 use v5.12.0;
  10         37  
2 10     10   56 use warnings;
  10         18  
  10         417  
3             package Test::Routine::Compositor 0.030;
4             # ABSTRACT: the tool for turning test routines into runnable classes
5              
6 10     10   56 use Carp qw(confess);
  10         20  
  10         517  
7 10     10   1486 use Class::Load;
  10         27462  
  10         432  
8 10     10   1694 use Moose::Meta::Class;
  10         391018  
  10         356  
9 10     10   75 use Params::Util qw(_CLASS);
  10         19  
  10         502  
10 10     10   76 use Scalar::Util qw(blessed);
  10         38  
  10         487  
11              
12 10     10   1597 use namespace::clean;
  10         18704  
  10         73  
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 20     20   63 my ($class, $inv) = @_;
29              
30 20 50       62 confess "can't supply preconstructed object for test class construction"
31             if blessed $inv;
32              
33 20 100       82 $inv = [ $inv ] if _CLASS($inv);
34              
35 20         273 my @bases;
36             my @roles;
37              
38 20         56 for my $item (@$inv) {
39 24         100 Class::Load::load_class($item);
40 24 50       1045 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 24         959 push @$target, $item;
45             }
46              
47 20 50       73 confess "can't build a test class from multiple base classes" if @bases > 1;
48 20 100       73 @bases = 'Moose::Object' unless @bases;
49              
50 20 100       127 my $new_class = Moose::Meta::Class->create_anon_class(
51             superclasses => \@bases,
52             cache => 1,
53             (@roles ? (roles => \@roles) : ()),
54             );
55              
56 20         95213 return $new_class->name;
57             }
58              
59             sub instance_builder {
60 21     21 0 59 my ($class, $inv, $arg) = @_;
61              
62 21 50 66     105 confess "can't supply preconstructed object and constructor arguments"
63             if $arg and blessed $inv;
64              
65 21 100   1   83 return sub { $inv } if blessed $inv;
  1         4  
66              
67 20         62 my $new_class = $class->_class_for($inv);
68 20   100     142 $arg //= {};
69              
70 20     20   111 return sub { $new_class->new($arg); };
  20         111  
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.030
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