blib/lib/Test/Reuse.pm

filter: Statementstmt 43.4% 20 / 46 Branchbran 10.0% 1 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 62.5% 5 / 8 Podpod 66.6% 2 / 3 totaltotal 34.5% 28 / 81 CC 10 SCAR 30.5
CC
10
Cov
32%
distribute
44.7
use_test
12.0
import
2.0
CRAP 21.1
↑ Summary
1 package Test::Reuse;
2  
3 =head1 NAME
4  
5 Test::Reuse - Reusable Test::More tests in classes
6  
7 =head1 DESCRIPTION
8  
9 Test::Reuse was created for the sole purpose of writing really easy-to-use, reusable tests. You can create tests in a class, C<use> your class name in the test (which will also deploy all L<Test::More> features for you), then you can use specific tests from that class. You can also pass arguments to these tests.
10  
11 =head1 SYNOPSIS
12  
13 OK, say we have the very same test running in several different tests. You don't want to just keep copy and pasting that code because it clutters up space when it doesn't need to. So we start by writing the test class to where our reusable tests will be imported from.
14  
15 package MyTestClass;
16
17 use Test::Reuse;
18  
19 subtest 'is_it_ok' => sub {
20 my $test = shift;
21
22 for (@_) {
23 ok $_, "$_ seems just fine!";
24 }
25 };
26  
27 The C<subtest> method in class that uses C<Test::Reuse> actually stores the tests that are reusable. It won't actually run a subtest. The first argument will always be the test class name. In this instance it is MyTestClass.
28 Now let's write the actual test.
29  
30 #!perl
31
32 use MyTestClass;
33
34 use_test 'is_it_ok', qw<1 2 3 4 0 5>;
35
36 runtests();
37  
38 That's it. We use C<use_test> followed by the subtest name. You can supply optional arguments afterwards if you like. Remember to always call C<runtests> when you're done, which is identical to C<done_testing>.
39 In the above example it will loop through all of numbers in the array we provided and will obviously fail on the fifth argument (the 0).
40 You can also run C<use_test> within the test class to control the flow a bit more.
41  
42 package MyTestClass;
43
44 use Test::Reuse;
45  
46 subtest 'is_it_ok' => sub {
47 my $test = shift;
48
49 if (@_) {
50 for (@_) {
51 ok $_, "$_ seems just fine!";
52 }
53 }
54 else {
55 use_test 'show_problem', 'No arguments for is_it_ok';
56 }
57 };
58  
59 subtest 'show_problem' => sub {
60 my ($test, $text) = @_;
61 note "Woops!: ${text}";
62 };
63
64 =cut
65  
66 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use base 'Test::More';
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1 $Test::Reuse::subtests = {};
71 1 $Test::Reuse::base = "";
72 1 $Test::Reuse::VERSION = '0.001';
73  
74 1 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
75 1 my $class = shift;
76 1 my $caller = caller(1);
77 1 $Test::Reuse::base = caller;
78 1 ▶ distribute($caller)
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Reuse::base->can("ok") 0 1 1
79 unless $Test::Reuse::base->can('ok');
80 }
81  
82 0 ▶sub distribute {
Subroutine distribute: not called CC=7 SCAR=38.0
Pod undocumented
83 0 my $caller = shift;
84 0 my $base = $Test::Reuse::base;
85 {
86 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
87 0 for my $method (keys %{"Test::More::"}) {
88 0 ▶ *{"${caller}::${method}"} = *{"Test::More::${method}"}
Branch: 1 branch
BranchTrueFalseTotal
unless substr($method, 0, 1) eq "_" or $method eq uc $method 0 0 0
Condition0%0/3
substr($method, 0, 1) eq "_" or $method eq uc $method
l!l&&r!l&&!r
000
MC/DC0%0/2
substr($method, 0, 1) eq "_" or $method eq uc $method
substr($method, 0, 1) eq "_" $method eq uc $method
Truth table0%0/3
A or B
A= substr($method, 0, 1) eq "_"B= $method eq uc $method
ABresult
1X1
011
000
89 unless substr($method, 0, 1) eq '_' or $method eq uc($method);
90 0 ▶ *{"${base}::${method}"} = *{"Test::More::${method}"}
Branch: 1 branch
BranchTrueFalseTotal
unless substr($method, 0, 1) eq "_" or $method eq uc $method or $method eq "subtest" 0 0 0
Condition0%0/6
substr($method, 0, 1) eq "_" or $method eq uc $method or $method eq "subtest"
l!l&&r!l&&!r
000
substr($method, 0, 1) eq "_" or $method eq uc $method
l!l&&r!l&&!r
000
MC/DC0%0/3
substr($method, 0, 1) eq "_" or $method eq uc $method or $method eq "subtest"
substr($method, 0, 1) eq "_" $method eq uc $method $method eq "subtest"
Truth table0%0/4
A or B or C
A= substr($method, 0, 1) eq "_"B= $method eq uc $methodC= $method eq "subtest"
ABCresult
1XX1
01X1
0011
0000
91 unless substr($method, 0, 1) eq '_' or $method eq uc($method) or $method eq 'subtest';
92 }
93 0 *{"${caller}::use_test"} = \&use_test;
94 0 *{"${caller}::runtests"} = *{"Test::More::done_testing"};
95 0 *{"${base}::use_test"} = \&use_test;
96 0 *{"${base}::subtest"} = \&subtest;
97 }
98 }
99  
100 0 ▶sub subtest {
Subroutine subtest: not called CC=1 SCAR=6.9
Pod documented
101 0 my ($name, $code) = @_;
102 0 $Test::Reuse::subtests->{$name} = $code;
103 }
104  
105 0 ▶sub use_test {
Subroutine use_test: not called CC=3 SCAR=24.8
Pod documented
106 0 my $base = $Test::Reuse::base;
107 0 my $test = shift;
108 __PACKAGE__->note("Can't use test '$test'. It's not included by $base")
109 0 ▶ if !$Test::Reuse::subtests->{$test};
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Reuse::subtests->{$test} 0 0 0
110 0 ▶ $Test::Reuse::subtests->{$test}->($base, @_) if $Test::Reuse::subtests->{$test};
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Reuse::subtests->{$test} 0 0 0
111 }
112  
113 =head1 METHODS
114  
115 Test::Reuse uses all the methods from L<Test::More>, but there are a couple that are used just in this module.
116  
117 =head2 use_test
118  
119 Calls a test from the test class. They must be defined in the test class using C<subtest>
120  
121 use_test 'method_name', qw<optional arguments here>;
122 use_test 'my_test';
123  
124 =head2 subtest
125  
126 Technically this works exactly the same as Test::More's subtest in your test file, but in the test class it simply defines a reusable test.
127  
128 subtest 'reusable_test_name' => sub {
129 my $test_class = shift;
130
131 note "Running from ${test_class}";
132 };
133  
134 =head2 runtests
135  
136 I don't like the way C<done_testing> looks, so swapped it for C<runtests>. But you are welcome to use either one in your code. This MUST be run at the bottom of your normal test file (.t), or it will freak out. Which is pretty normal if you don't declare a plan.
137  
138 =head1 LIMITATIONS
139  
140 This module is still new, so there are plenty. The main one at the moment being that you can only C<use> ONE reusable test class per test. It really sucks, I know. In the future I would love to be able to reuse tests from multiple classes, but at the moment it only works with one.
141  
142 =head1 AUTHOR
143  
144 Brad Haywood <brad@perlpowered.com>
145  
146 =head1 LICENSE
147  
148 You may distribute this code under the same terms as Perl itself, because Perl is awesome, and so are you for using it.
149  
150 =cut
151  
152 1 1;
↑ Summary