File Coverage

blib/lib/Test/Sweet.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Test::Sweet;
2 2     2   3269912 use Moose ();
  2         2  
  2         44  
3 2     2   9 use Moose::Exporter;
  2         1  
  2         14  
4 2     2   70 use Moose::Util::MetaRole;
  2         8  
  2         35  
5              
6 2     2   616 use Test::Sweet::Meta::Class;
  2         10399  
  2         108  
7 2     2   19 use Test::Sweet::Meta::Method;
  2         2  
  2         76  
8              
9 2     2   10 use Devel::Declare;
  2         3  
  2         19  
10 2     2   1300 use Test::Sweet::Keyword::Test;
  0            
  0            
11              
12             our $VERSION = '0.00_01';
13              
14             Moose::Exporter->setup_import_methods();
15              
16             sub init_meta {
17             my ($me, %options) = @_;
18              
19             my $for = $options{for_class};
20              
21              
22             # work on both roles and classes
23             my $meta;
24             if ($for->can('meta')) {
25             $meta = $for->meta;
26             } else {
27             $meta = Moose::Role->init_meta(for_class => $for);
28             }
29              
30             setup_sugar_for($for);
31             load_extra_modules_into($for) unless $options{no_extra_modules};
32              
33             Moose::Util::MetaRole::apply_metaclass_roles(
34             for_class => $for,
35             metaclass_roles => ['Test::Sweet::Meta::Class'],
36             );
37              
38             if($meta->isa('Class::MOP::Class')){
39             # don't apply this to roles
40             Moose::Util::MetaRole::apply_base_class_roles(
41             for_class => $for,
42             roles => ['Test::Sweet::Runnable'],
43             );
44             }
45             }
46              
47             sub setup_sugar_for {
48             my $pkg = shift;
49             Test::Sweet::Keyword::Test->install_methodhandler(
50             into => $pkg,
51             );
52             }
53              
54             sub load_extra_modules_into {
55             my $pkg = shift;
56             eval "package $pkg; use Test::More; use Test::Exception";
57             }
58              
59             1;
60             __END__
61              
62             =head1 NAME
63              
64             Test::Sweet - Moose-based Test::Class replacement
65              
66             =head1 SYNOPSIS
67              
68             Write test classes:
69              
70             class t::RecordBasic with t::lib::FakeDatabase {
71             use Test::Sweet;
72              
73             test add_record {
74             $self->database->insert( 42 => 'OH HAI' );
75             ok $self->database->get_record('42'), 'can get record 42';
76             }
77              
78             test delete_record {
79             ok $self->database->exists('42'), 'still have record 42';
80             lives_ok {
81             $self->database->delete('42')
82             } 'deleting 42 lives';
83             ok !$self->database->exists('42'), 'record 42 is gone';
84             }
85             }
86              
87             Run them:
88              
89             $ mx-run -Ilib t::RecordBasic
90              
91             And get the valid TAP output:
92              
93             1..2
94             ok 1 - can get record 42
95             1..1
96             ok 1 - subtest add_record
97             ok 1 - still have record 42
98             ok 2 - deleting 42 lives
99             ok 3 - record 42 is gone
100             1..3
101             ok 2 - subtest delete_record
102              
103             No more counting tests; this module does it for you and ensures that
104             you are protected against premature death. (Well, your test suite,
105             anyway.)
106              
107             You can also have command-line args for your tests; they are parsed
108             with L<MooseX::Getopt|MooseX::Getopt> (if you have it installed; try
109             "mx-run t::YourTest --help").
110              
111             =head1 DESCRIPTION
112              
113             C<Test::Sweet> lets you organize tests into Moose classes and Moose
114             roles. You just need to create a normal class or role and say C<use
115             Test::Sweet> somewhere. This adds the necessary methods to your
116             metaclass, makes your class do C<Test::Sweet::Runnable> (so that you
117             can run it with L<MooseX::Runnable|MooseX::Runnable>'s
118             L<mx-run|mx-run> command), and makes the C<test> keyword available for
119             your use. (The imports are package-scoped, of course, but the C<test>
120             keyword is lexically scoped.)
121              
122             Normal methods are defined normally. Methods that run tests are
123             defined like methods, but with the C<test> keyword instead of C<sub>
124             or C<method>. In the test methods, you can use any
125             L<Test::Builder|Test::Builder>-aware test methods. You get all of
126             L<Test::More|Test::More> and L<Test::Exception|Test::Exception> by
127             default.
128              
129             Tests can be called as methods any time the test suite is running,
130             including in BUILD and DEMOLISH. Everything will Just Work. The
131             method will get the arguments you pass, you will get the return value,
132             and this module will do what's necessary to ensure that Test::Builder
133             knows what is going on. It's a Moose class and tests are just special
134             methods. Method modifiers work too. (But don't run tests directly in
135             the method modifier body yet; just call other C<test> methods.)
136              
137             To run all tests in a class (hierarchy), just call the C<run> method.
138              
139             Tests are ordered as follows. All test method from the superclasses
140             are run first, then your tests are run in the order they appear in the
141             file (this is guaranteed, not a side-effect of anything), then any
142             tests you composed in from roles are run. If anything in the
143             hierarchy overrides a test method from somewhere else in the
144             hierarchy, the overriding method will be run when the original method
145             would be.
146              
147             Here's an example:
148              
149             class A { use Test::Sweet; test first { pass 'first' } };
150             class B extends A { use Test::Sweet; test second { pass 'second' } };
151              
152             When you call C<< A->run >>, "first" will be run.
153              
154             When you call C<< B->run >>, "first" will run, then "second" will run.
155              
156             If you change B to look like:
157              
158             class B extends A {
159             test second { pass 'second' }
160             test first { pass 'blah' }
161             }
162              
163             When you run C<< B->run >>, first will be called first but will print
164             "blah", and second will be called second. (If you remove the "extends
165             A", they will run in the order they appear in B, of course; second
166             then first.)
167              
168             =head1 REPOSITORY
169              
170             L<http://github.com/jrockway/test-sweet>
171              
172             Patches (or pull requests) are very welcome. You should also discuss
173             this module on the moose irc channel at L<irc://irc.perl.org/#moose>;
174             nothing is set in stone yet, and your feedback is requested.
175              
176             =head1 TODO
177              
178             Convince C<prove> to run the <.pm> files directly.
179              
180             Write code to organize classes into test suites; and run the test
181             suites easily. (Classes and tests should be tagged, so you can run
182             C<run-test-suite t::Suite --no-slow-tests> or something.)
183              
184             More testing. There are undoubtedly corner cases that are
185             undiscovered and unhandled.
186              
187             =head1 SEE ALSO
188              
189             L<http://github.com/jrockway/test-sweet-dbic> shows what sort of
190             reusability you can get with C<Test::Sweet>... with 5 minutes of
191             hacking.
192              
193             Read this module's test suite (in the C<t/> directory) for example of
194             how to make C<prove> understand C<Test::Sweet> classes.
195              
196             =head1 AUTHOR
197              
198             Jonathan Rockway C<< <jrockway@cpan.org> >>
199              
200             =head1 COPYRIGHT
201              
202             Copyright (c) 2009 Jonathan Rockway.
203              
204             This module is free software, you may redistribute it under the same
205             terms as Perl itself.