File Coverage

blib/lib/Test/Routine/Runner.pm
Criterion Covered Total %
statement 43 44 97.7
branch 4 6 66.6
condition n/a
subroutine 11 12 91.6
pod 0 1 0.0
total 58 63 92.0


line stmt bran cond sub pod time code
1 11     11   136 use v5.12.0;
  11         47  
2             package Test::Routine::Runner 0.031;
3             # ABSTRACT: tools for running Test::Routine tests
4              
5 11     11   2277 use Moose;
  11         1262090  
  11         70  
6              
7             #pod =head1 OVERVIEW
8             #pod
9             #pod A Test::Routine::Runner takes a callback for building test instances, then uses
10             #pod it to build instances and run the tests on it. The Test::Routine::Runner
11             #pod interface is still undergoing work, but the Test::Routine::Util exports for
12             #pod running tests, described in L<Test::Routine|Test::Routine/Running Tests>, are
13             #pod more stable. Please use those instead, unless you are willing to deal with
14             #pod interface breakage.
15             #pod
16             #pod =cut
17              
18 11     11   79141 use Carp qw(confess croak);
  11         31  
  11         648  
19 11     11   94 use Scalar::Util qw(reftype);
  11         22  
  11         1847  
20 11     11   1564 use Test2::API 1.302045 ();
  11         148009  
  11         276  
21 11     11   89 use Try::Tiny;
  11         32  
  11         667  
22              
23 11     11   1329 use Moose::Util::TypeConstraints;
  11         2678  
  11         1443  
24              
25 11     11   25353 use namespace::clean;
  11         24  
  11         96  
26              
27             subtype 'Test::Routine::_InstanceBuilder', as 'CodeRef';
28             subtype 'Test::Routine::_Instance',
29             as 'Object',
30             where { $_->does('Test::Routine::Common') };
31              
32             coerce 'Test::Routine::_InstanceBuilder',
33             from 'Test::Routine::_Instance',
34             via { my $instance = $_; sub { $instance } };
35              
36             has _instance_builder => (
37             is => 'ro',
38             isa => 'Test::Routine::_InstanceBuilder',
39             coerce => 1,
40             traits => [ 'Code' ],
41             init_arg => 'instance_from',
42             required => 1,
43             handles => {
44             'build_test_instance' => 'execute_method',
45             },
46             );
47              
48             has description => (
49             is => 'ro',
50             isa => 'Str',
51             required => 1,
52             );
53              
54             sub run {
55 22     22 0 65 my ($self) = @_;
56              
57 22         851 my $test_instance = $self->build_test_instance;
58              
59 22         192 my @tests = grep { Moose::Util::does_role($_, 'Test::Routine::Test::Role') }
  411         47880  
60             $test_instance->meta->get_all_methods;
61              
62 22         1108 my $re = $ENV{TEST_METHOD};
63 22 100       96 if (length $re) {
64 3     3   163 my $filter = try { qr/$re/ } # compile the the regex separately ...
65 3     0   19 catch { croak("TEST_METHOD ($re) is not a valid regular expression: $_") };
  0         0  
66 3         79 $filter = qr/\A$filter\z/; # ... so it can't mess with the anchoring
67 3         8 @tests = grep { $_->description =~ $filter } @tests;
  12         356  
68             }
69              
70             # As a side note, I wonder whether there is any way to format the code below
71             # to not look stupid. -- rjbs, 2010-09-28
72             my @ordered_tests = sort {
73 22         104 $a->_origin->{file} cmp $b->_origin->{file}
74             || $a->_origin->{nth} <=> $b->_origin->{nth}
75 31 0       935 } @tests;
76              
77             Test2::API::run_subtest($self->description, sub {
78 22     22   17600 TEST: for my $test (@ordered_tests) {
79 47         846 my $ctx = Test2::API::context;
80 47 100       4436 if (my $reason = $test->skip_reason($test_instance)) {
81 3         430 $ctx->skip($test->name, $reason);
82             } else {
83 44         595 $test_instance->run_test( $test );
84             }
85              
86 47         2934 $ctx->release;
87             }
88 22         648 });
89             }
90              
91             1;
92              
93             __END__
94              
95             =pod
96              
97             =encoding UTF-8
98              
99             =head1 NAME
100              
101             Test::Routine::Runner - tools for running Test::Routine tests
102              
103             =head1 VERSION
104              
105             version 0.031
106              
107             =head1 OVERVIEW
108              
109             A Test::Routine::Runner takes a callback for building test instances, then uses
110             it to build instances and run the tests on it. The Test::Routine::Runner
111             interface is still undergoing work, but the Test::Routine::Util exports for
112             running tests, described in L<Test::Routine|Test::Routine/Running Tests>, are
113             more stable. Please use those instead, unless you are willing to deal with
114             interface breakage.
115              
116             =head1 PERL VERSION
117              
118             This module should work on any version of perl still receiving updates from
119             the Perl 5 Porters. This means it should work on any version of perl released
120             in the last two to three years. (That is, if the most recently released
121             version is v5.40, then this module should work on both v5.40 and v5.38.)
122              
123             Although it may work on older versions of perl, no guarantee is made that the
124             minimum required version will not be increased. The version may be increased
125             for any reason, and there is no promise that patches will be accepted to lower
126             the minimum required perl.
127              
128             =head1 AUTHOR
129              
130             Ricardo Signes <cpan@semiotic.systems>
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2010 by Ricardo Signes.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut