File Coverage

blib/lib/Test/Able.pm
Criterion Covered Total %
statement 47 47 100.0
branch 1 2 50.0
condition n/a
subroutine 16 16 100.0
pod 5 6 83.3
total 69 71 97.1


line stmt bran cond sub pod time code
1             package Test::Able;
2              
3 8     8   11172 use 5.008;
  8         25  
  8         307  
4 8     8   4567 use Moose;
  8         2991212  
  8         57  
5 8     8   44836 use Moose::Exporter;
  8         17  
  8         38  
6 8     8   335 use Moose::Util::MetaRole;
  8         9  
  8         157  
7 8     8   32 use strict;
  8         9  
  8         177  
8 8     8   4274 use Test::Able::Object;
  8         79  
  8         236  
9 8     8   3819 use Test::Able::Role::Meta::Class;
  8         20  
  8         346  
10 8     8   53 use Test::Able::Role::Meta::Method;
  8         11  
  8         143  
11 8     8   26 use warnings;
  8         10  
  8         3000  
12              
13             =head1 NAME
14              
15             Test::Able - xUnit with Moose
16              
17             =head1 VERSION
18              
19             0.10
20              
21             =cut
22              
23             our $VERSION = '0.11';
24              
25             =head1 SYNOPSIS
26              
27             package MyTest;
28              
29             use Test::Able;
30             use Test::More;
31              
32             startup some_startup => sub { ... };
33             setup some_setup => sub { ... };
34             test plan => 1, foo => sub { ok( 1 ); };
35             test bar => sub {
36             my @runtime_list = 1 .. 42;
37             $_[ 0 ]->meta->current_method->plan( scalar @runtime_list );
38             ok( 1 ) for @runtime_list;
39             };
40             teardown some_teardown => sub { ... };
41             shutdown some_shutdown => sub { ... };
42              
43             MyTest->run_tests;
44              
45             =head1 DESCRIPTION
46              
47             An xUnit style testing framework inspired by L<Test::Class> and built using
48             L<Moose>. It can do all the important things Test::Class can do and more.
49             The prime advantages of using this module instead of Test::Class are
50             flexibility and power. Namely, Moose.
51              
52             This module was created for a few of reasons:
53              
54             =over
55              
56             =item *
57              
58             To address perceived limitations in, and downfalls of, Test::Class.
59              
60             =item *
61              
62             To leverage existing Moose expertise for testing.
63              
64             =item *
65              
66             To bring Moose to the Perl testing game.
67              
68             =back
69              
70             =head1 EXPORTED FUNCTIONS
71              
72             In addition to exporting for Moose, Test::Able will export a handful
73             of functions that can be used to declare test-related methods.
74              
75             =cut
76              
77             Moose::Exporter->setup_import_methods(
78             with_caller => [
79             qw( startup setup test teardown shutdown ),
80             ],
81             also => 'Moose',
82             );
83              
84             sub init_meta {
85 15     15 0 34581 shift;
86 15         57 my %options = @_;
87 15         29 $options{base_class} = 'Test::Able::Object';
88              
89 15         70 Moose->init_meta( %options, );
90              
91 15         54844 return Moose::Util::MetaRole::apply_metaroles(
92             for => $options{for_class},
93             class_metaroles => {
94             class => [ 'Test::Able::Role::Meta::Class', ],
95             method => [ 'Test::Able::Role::Meta::Method', ],
96             },
97             );
98             }
99              
100             =over
101              
102             =item startup/setup/test/teardown/shutdown
103              
104             A more Moose-like way to do method declaration. The syntax is similar to
105             L<Moose/has> except its for test-related methods.
106              
107             These start with one of startup/setup/test/teardown/shutdown depending on what
108             type of method you are defining. Then comes any attribute name/value pairs to
109             set in the L<Test::Able::Role::Meta::Method>-based method metaclass object.
110             The last pair must always be the method name and the coderef. This is to
111             disambiguate between the method name/code pair and any another attribute in
112             the method metaclass that happens to take a coderef. See the synopsis or the
113             tests for examples.
114              
115             =back
116              
117             =cut
118              
119 33     33 1 85127 sub startup { return __add_method( type => 'startup', @_, ); }
120 28     28 1 269 sub setup { return __add_method( type => 'setup', @_, ); }
121 32     32 1 1179 sub test { return __add_method( type => 'test', @_, ); }
122 28     28 1 261 sub teardown { return __add_method( type => 'teardown', @_, ); }
123 29     29 1 263 sub shutdown { return __add_method( type => 'shutdown', @_, ); }
124              
125             sub __add_method {
126 169     169   3424 my $class = splice( @_, 2, 1, );
127 169         239 my ( $code, $name, ) = ( pop, pop, );
128              
129 169         522 my $meta = Moose::Meta::Class->initialize( $class, );
130 169         2325 $meta->add_method( $name, $code, );
131              
132 169 50       6788 if ( @_ ) {
133 169         374 my $method = $meta->get_method( $name, );
134 169         145199 my %args = @_;
135 169         526 while ( my ( $k, $v ) = each %args ) {
136 261         7908 $method->$k( $v );
137             }
138             }
139              
140 169         380 return;
141             }
142              
143             =head1 Overview
144              
145             1. Build some test classes: a, b, & c. The classes just have to be based on
146             Test::Able.
147              
148             2. Fire up an instance of any of them to be the runner object. Any test
149             object can serve as the test_runner_object including itself.
150              
151             my $b_obj = b->new;
152              
153             3. Setup the test_objects in the test_runner_object.
154              
155             $b_obj->test_objects( [
156             a->new,
157             $b_obj,
158             c->new,
159             ] );
160              
161             4. Do the test run. The test_objects will be run in order.
162              
163             $b_obj->run_tests;
164              
165             =head1 SEE ALSO
166              
167             =over
168              
169             =item L<Test::Able::Cookbook>
170              
171             =item support
172              
173             #moose on irc.perl.org
174              
175             =item code
176              
177             http://github.com/jdv/test-able/tree/master
178              
179             =item L<Moose>
180              
181             =item L<Test::Class>
182              
183             =item L<Test::Builder>
184              
185             =back
186              
187             =head1 AUTHOR
188              
189             Justin DeVuyst, C<justin@devuyst.com>
190              
191             =head1 COPYRIGHT AND LICENSE
192              
193             Copyright 2009 by Justin DeVuyst.
194              
195             This library is free software, you can redistribute it and/or modify it under
196             the same terms as Perl itself.
197              
198             =cut
199              
200             1;