File Coverage

blib/lib/Test/Routine/Compositor.pm
Criterion Covered Total %
statement 44 50 88.0
branch 14 22 63.6
condition 4 10 40.0
subroutine 12 13 92.3
pod 0 1 0.0
total 74 96 77.0


line stmt bran cond sub pod time code
1 10     10   60 use strict;
  10         19  
  10         254  
2 10     10   42 use warnings;
  10         19  
  10         381  
3             package Test::Routine::Compositor;
4             # ABSTRACT: the tool for turning test routines into runnable classes
5             $Test::Routine::Compositor::VERSION = '0.029';
6 10     10   60 use Carp qw(confess);
  10         17  
  10         406  
7 10     10   1131 use Class::Load;
  10         22016  
  10         315  
8 10     10   1361 use Moose::Meta::Class;
  10         325930  
  10         323  
9 10     10   63 use Params::Util qw(_CLASS);
  10         20  
  10         547  
10 10     10   54 use Scalar::Util qw(blessed);
  10         19  
  10         358  
11              
12 10     10   1554 use namespace::clean;
  10         16952  
  10         83  
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   49 my ($class, $inv) = @_;
29              
30 20 50       66 confess "can't supply preconstructed object for test class construction"
31             if blessed $inv;
32              
33 20 100       99 $inv = [ $inv ] if _CLASS($inv);
34              
35 20         245 my @bases;
36             my @roles;
37              
38 20         57 for my $item (@$inv) {
39 24         100 Class::Load::load_class($item);
40 24 50       1694 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         891 push @$target, $item;
45             }
46              
47 20 50       67 confess "can't build a test class from multiple base classes" if @bases > 1;
48 20 100       76 @bases = 'Moose::Object' unless @bases;
49              
50 20 100       122 my $new_class = Moose::Meta::Class->create_anon_class(
51             superclasses => \@bases,
52             cache => 1,
53             (@roles ? (roles => \@roles) : ()),
54             );
55              
56 20         81609 return $new_class->name;
57             }
58              
59             sub instance_builder {
60 21     21 0 58 my ($class, $inv, $arg) = @_;
61              
62 21 50 66     96 confess "can't supply preconstructed object and constructor arguments"
63             if $arg and blessed $inv;
64              
65 21 100   1   81 return sub { $inv } if blessed $inv;
  1         5  
66              
67 20         71 my $new_class = $class->_class_for($inv);
68 20   100     112 $arg ||= {};
69              
70 20     20   114 return sub { $new_class->new($arg); };
  20         102  
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.029
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