File Coverage

blib/lib/Test/Routine/Runner.pm
Criterion Covered Total %
statement 48 49 97.9
branch 4 6 66.6
condition n/a
subroutine 13 14 92.8
pod 0 1 0.0
total 65 70 92.8


line stmt bran cond sub pod time code
1 11     11   165 use v5.12.0;
  11         46  
2             package Test::Routine::Runner 0.032;
3             # ABSTRACT: tools for running Test::Routine tests
4              
5 11     11   3812 use Moose;
  11         1651027  
  11         80  
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   93042 use Carp qw(confess croak);
  11         28  
  11         964  
19 11     11   73 use Scalar::Util qw(reftype);
  11         24  
  11         702  
20 11     11   1649 use Test2::API 1.302045 ();
  11         157960  
  11         380  
21 11     11   69 use Try::Tiny;
  11         25  
  11         869  
22              
23 11     11   75 use Moose::Util::TypeConstraints;
  11         18  
  11         162  
24              
25 11     11   28191 use namespace::clean;
  11         27  
  11         118  
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 _get_tests {
55 22     22   67 my ($self, $test_instance) = @_;
56              
57 22         106 my @tests = grep { Moose::Util::does_role($_, 'Test::Routine::Test::Role') }
  455         60517  
58             $test_instance->meta->get_all_methods;
59              
60 22         1433 my $re = $ENV{TEST_METHOD};
61 22 100       105 if (length $re) {
62 3     3   179 my $filter = try { qr/$re/ } # compile the the regex separately ...
63 3     0   26 catch { croak("TEST_METHOD ($re) is not a valid regular expression: $_") };
  0         0  
64 3         96 $filter = qr/\A$filter\z/; # ... so it can't mess with the anchoring
65 3         10 @tests = grep { $_->description =~ $filter } @tests;
  12         430  
66             }
67              
68             # As a side note, I wonder whether there is any way to format the code below
69             # to not look stupid. -- rjbs, 2010-09-28
70             my @ordered_tests = sort {
71 22         129 $a->_origin->{file} cmp $b->_origin->{file}
72             || $a->_origin->{nth} <=> $b->_origin->{nth}
73 32 0       1477 } @tests;
74              
75 22         94 return \@ordered_tests;
76             }
77              
78             sub _run_tests {
79 22     22   72 my ($self, $test_instance, $ordered_tests) = @_;
80              
81 22         107 TEST: for my $test (@$ordered_tests) {
82 47         1293 my $ctx = Test2::API::context;
83 47 100       5874 if (my $reason = $test->skip_reason($test_instance)) {
84 3         621 $ctx->skip($test->name, $reason);
85             } else {
86 44         986 $test_instance->run_test( $test );
87             }
88              
89 47         3542 $ctx->release;
90             }
91             }
92              
93             sub run {
94 22     22 0 77 my ($self) = @_;
95              
96 22         1185 my $test_instance = $self->build_test_instance;
97              
98 22         315 my $ordered_tests = $self->_get_tests($test_instance);
99              
100             Test2::API::run_subtest($self->description, sub {
101 22     22   20382 $self->_run_tests($test_instance, $ordered_tests);
102 22         890 });
103             }
104              
105             1;
106              
107             __END__
108              
109             =pod
110              
111             =encoding UTF-8
112              
113             =head1 NAME
114              
115             Test::Routine::Runner - tools for running Test::Routine tests
116              
117             =head1 VERSION
118              
119             version 0.032
120              
121             =head1 OVERVIEW
122              
123             A Test::Routine::Runner takes a callback for building test instances, then uses
124             it to build instances and run the tests on it. The Test::Routine::Runner
125             interface is still undergoing work, but the Test::Routine::Util exports for
126             running tests, described in L<Test::Routine|Test::Routine/Running Tests>, are
127             more stable. Please use those instead, unless you are willing to deal with
128             interface breakage.
129              
130             =head1 PERL VERSION
131              
132             This module should work on any version of perl still receiving updates from
133             the Perl 5 Porters. This means it should work on any version of perl
134             released in the last two to three years. (That is, if the most recently
135             released version is v5.40, then this module should work on both v5.40 and
136             v5.38.)
137              
138             Although it may work on older versions of perl, no guarantee is made that the
139             minimum required version will not be increased. The version may be increased
140             for any reason, and there is no promise that patches will be accepted to
141             lower the minimum required perl.
142              
143             =head1 AUTHOR
144              
145             Ricardo Signes <cpan@semiotic.systems>
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is copyright (c) 2010 by Ricardo Signes.
150              
151             This is free software; you can redistribute it and/or modify it under
152             the same terms as the Perl 5 programming language system itself.
153              
154             =cut