File Coverage

blib/lib/Test/Able/Helpers.pm
Criterion Covered Total %
statement 44 44 100.0
branch 5 10 50.0
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 59 64 92.1


line stmt bran cond sub pod time code
1             package Test::Able::Helpers;
2              
3 1     1   839 use List::Util qw( shuffle );
  1         2  
  1         68  
4 1     1   5 use strict;
  1         1  
  1         44  
5 1         11 use Sub::Exporter -setup => {
6             exports => [ qw(
7             prune_super_methods
8             shuffle_methods
9             get_loop_plan
10             ), ],
11             groups => {
12             default => [ qw(
13             prune_super_methods
14             shuffle_methods
15             get_loop_plan
16             ), ],
17             },
18 1     1   5 };
  1         1  
19 1     1   389 use warnings;
  1         2  
  1         311  
20              
21             =head1 NAME
22              
23             Test::Able::Helpers
24              
25             =head1 SYNOPSIS
26              
27             use Test::Able::Helpers;
28              
29             my $t = MyTest;
30             $t->shuffle_methods;
31             $t->run_tests;
32              
33             =head1 DESCRIPTION
34              
35             Test::Able::Helpers are a collection of mixin methods that can
36             be exported into the calling test class. These are meant to
37             make doing some things with Test::Able easier.
38              
39             See L<Test::Able::Cookbook> for example usages.
40              
41             =head1 METHODS
42              
43             =over
44              
45             =item prune_super_methods
46              
47             Removes any test-related methods from the associated method list if its from
48             a superclass (literally not from $self's class).
49              
50             By default it does this for all test-related method types.
51             Type names can be optionally provided as args to limit what
52             types this is done for.
53              
54             =cut
55              
56             sub prune_super_methods {
57 1     1 1 6 my ( $self, @types, ) = @_;
58              
59 1 50       3 @types = @{ $self->meta->method_types } unless @types;
  1         5  
60              
61 1         4 my $self_pkg = ref $self;
62 1         2 for my $type ( @types ) {
63 5         8 my $accessor = $type . '_methods';
64 26         198 $self->meta->$accessor( [ grep {
65 5         96 $_->package_name eq $self_pkg;
66 5         15 } @{ $self->meta->$accessor } ] );
67             }
68              
69 1         2 return;
70             }
71              
72             =item shuffle_methods
73              
74             Randomizes the test-related method lists.
75              
76             By default it does this for all test-related method types.
77             Type names can be optionally provided as args to limit what
78             types this is done for.
79              
80             =cut
81              
82             sub shuffle_methods {
83 10     10 1 90 my ( $self, @types, ) = @_;
84              
85 10 50       29 @types = @{ $self->meta->method_types } unless @types;
  10         38  
86              
87 10         24 for my $type ( @types ) {
88 50         77 my $accessor = $type . '_methods';
89 50         120 $self->meta->$accessor( [ shuffle @{ $self->meta->$accessor } ] );
  50         653  
90             }
91              
92 10         30 return;
93             }
94              
95             =item get_loop_plan
96              
97             Calculates the plan for a test method when used in a "Loop-Driven" context.
98             This assumes the setup and teardown method lists are being explicitly
99             run as many times as the test method.
100              
101             Has two required args: the test method name and the test count.
102             The test method name is used to lookup the plan of the test method
103             itself. The test count is the number of times the test method will
104             be called.
105              
106             =back
107              
108             =cut
109              
110             sub get_loop_plan {
111 1     1 1 2 my ( $self, $test_method_name, $test_count, ) = @_;
112              
113 1         5 my $test_plan
114             = $self->meta->test_methods->{ $test_method_name }->plan;
115 1 50       6 return 'no_plan' if $test_plan eq 'no_plan';
116              
117 1         2 my $setup_plan;
118 1         1 for my $method ( @{ $self->meta->setup_methods } ) {
  1         4  
119 4 50       97 return 'no_plan' if $method->plan eq 'no_plan';
120 4         97 $setup_plan += $method->plan;
121             }
122              
123 1         3 my $teardown_plan;
124 1         1 for my $method ( @{ $self->meta->teardown_methods } ) {
  1         3  
125 4 50       95 return 'no_plan' if $method->plan eq 'no_plan';
126 4         99 $teardown_plan += $method->plan;
127             }
128              
129             return(
130 1         27 ( $test_plan + $setup_plan + $teardown_plan ) * $test_count
131             );
132             }
133              
134             =head1 AUTHOR
135              
136             Justin DeVuyst, C<justin@devuyst.com>
137              
138             =head1 COPYRIGHT AND LICENSE
139              
140             Copyright 2009 by Justin DeVuyst.
141              
142             This library is free software, you can redistribute it and/or modify it under
143             the same terms as Perl itself.
144              
145             =cut
146              
147             1;