File Coverage

blib/lib/Test/Able/Role.pm
Criterion Covered Total %
statement 35 37 94.5
branch 1 2 50.0
condition n/a
subroutine 11 13 84.6
pod 5 6 83.3
total 52 58 89.6


line stmt bran cond sub pod time code
1             package Test::Able::Role;
2              
3 1     1   13686 use Moose::Role;
  1         2  
  1         8  
4 1     1   3971 use Moose::Exporter;
  1         1  
  1         5  
5 1     1   42 use Moose::Util::MetaRole;
  1         1  
  1         17  
6 1     1   3 use strict;
  1         2  
  1         23  
7 1     1   4 use Test::Able::Role::Meta::Method;
  1         2  
  1         15  
8 1     1   4 use warnings;
  1         1  
  1         284  
9              
10             =head1 NAME
11              
12             Test::Able::Role -The Test::Able Role
13              
14             =head1 SYNOPSIS
15              
16             package MyTest::SomeRole;
17              
18             use Test::Able::Role;
19              
20             test some_test => sub {};
21              
22             =head1 DESCRIPTION
23              
24             This is the Test::Able Role. It is an extension of Moose::Role in the same
25             way as Test::Able is an extension of Moose for the purpose of handling
26             test-related methods.
27              
28             =head1 EXPORTED FUNCTIONS
29              
30             In addition to exporting for Moose::Role, Test::Able::Role will export a
31             handful of functions that can be used to declare test-related methods.
32             These functions are the same functions that Test::Able exports.
33              
34             =cut
35              
36             Moose::Exporter->setup_import_methods(
37             with_caller => [
38             qw( startup setup test teardown shutdown ),
39             ],
40             also => 'Moose::Role',
41             );
42              
43             sub init_meta {
44 2     2 0 5359 shift;
45 2         6 my %options = @_;
46              
47 2         8 my $m = Moose::Role->init_meta( %options, );
48              
49 2         4782 return Moose::Util::MetaRole::apply_metaroles(
50             for => $options{for_class},
51             role_metaroles => {
52             method => [ 'Test::Able::Role::Meta::Method', ],
53             },
54             );
55             }
56              
57             =over
58              
59             =item startup/setup/test/teardown/shutdown
60              
61             A more Moose-like way to do method declaration. The syntax is similar to
62             L<Moose/has> except its for test-related methods.
63              
64             These start with one of startup/setup/test/teardown/shutdown depending on what
65             type of method you are defining. Then comes any attribute name/value pairs to
66             set in the L<Test::Able::Role::Meta::Method>-based mehod metaclass object.
67             The last pair must always be the method name and the coderef. This is to
68             disambiguate between the method name/code pair and any another attribute in
69             the method metaclass that happens to take a coderef. See the synopsis or the
70             tests for examples.
71              
72             =back
73              
74             =cut
75              
76 0     0 1 0 sub startup { return __add_method( type => 'startup', @_, ); }
77 1     1 1 1818 sub setup { return __add_method( type => 'setup', @_, ); }
78 2     2 1 3409 sub test { return __add_method( type => 'test', @_, ); }
79 1     1 1 9 sub teardown { return __add_method( type => 'teardown', @_, ); }
80 0     0 1 0 sub shutdown { return __add_method( type => 'shutdown', @_, ); }
81              
82             sub __add_method {
83 4     4   9 my $class = splice( @_, 2, 1, );
84 4         7 my ( $code, $name, ) = ( pop, pop, );
85              
86 4         14 my $meta = Moose::Meta::Class->initialize( $class, );
87 4         58 $meta->add_method( $name, $code, );
88              
89 4 50       185 if ( @_ ) {
90 4         9 my $method = $meta->get_method( $name, );
91 4         3887 my %args = @_;
92 4         16 while ( my ( $k, $v ) = each %args ) {
93 6         179 $method->$k( $v );
94             }
95             }
96              
97 4         9 return;
98             }
99              
100             =head1 AUTHOR
101              
102             Justin DeVuyst, C<justin@devuyst.com>
103              
104             =head1 COPYRIGHT AND LICENSE
105              
106             Copyright 2009 by Justin DeVuyst.
107              
108             This library is free software, you can redistribute it and/or modify it under
109             the same terms as Perl itself.
110              
111             =cut
112              
113             1;