/
Sugar.pm
415 lines (297 loc) · 12.4 KB
/
Sugar.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package Test::Class::Sugar;
use Modern::Perl;
use Devel::Declare ();
use Devel::Declare::Context::Simple;
use B::Hooks::EndOfScope;
use Test::Class::Sugar::Context;
use Test::Class::Sugar::CodeGenerator;
use Carp qw/croak/;
use namespace::clean;
our $VERSION = '0.0100';
my %PARSER_FOR = (
testclass => '_parse_testclass',
startup => '_parse_inner_keyword',
setup => '_parse_inner_keyword',
test => '_parse_inner_keyword',
teardown => '_parse_inner_keyword',
shutdown => '_parse_inner_keyword',
);
use Sub::Exporter -setup => {
exports => [qw/testclass startup setup test teardown shutdown/],
groups => {default => [qw/testclass/],
inner => [qw/startup setup test teardown shutdown/]},
installer => sub {
my ($args, $to_export) = @_;
my $pack = $args->{into};
foreach my $name (@$to_export) {
if (my $parser = __PACKAGE__->can($PARSER_FOR{$name}//'-NOTHING-')) {
Devel::Declare->setup_for(
$pack,
{ $name => { const => sub { $parser->($pack, @_) } } },
);
}
}
Sub::Exporter::default_installer(@_);
}
};
sub _test_generator {
my($ctx, $name, $plan) = @_;
Test::Class::Sugar::CodeGenerator->new(
context => $ctx,
name => $name,
plan => $plan,
);
}
sub _testclass_generator {
my($ctx, $classname, $options) = @_;
my $ret = Test::Class::Sugar::CodeGenerator->new(
context => $ctx,
options => $options,
);
$ret->classname($classname) if $classname;
return $ret;
}
sub _parse_inner_keyword {
my $pack = shift;
local $Carp::Internal{'Devel::Declare'} = 1;
my $ctx = Test::Class::Sugar::Context->new->init(@_);
my $preamble = '';
$ctx->skip_declarator;
my $name = $ctx->strip_test_name
|| croak "Can't make a test without a name";
my $plan = $ctx->strip_plan;
_test_generator($ctx, $name, $plan)->install_test();
return;
}
sub _testclass_preamble {
my($ctx, $classname, $options) = @_;
my $preamble = '';
unless ($classname) {
$options->{class_under_test}
// croak "Must specify a testclass name or a class to exercise";
$classname = "Test::" . $options->{class_under_test} ;
}
$preamble .= "package ${classname}; use strict; use warnings;";
$preamble .= "use " . __PACKAGE__ . " qw/-inner/;";
my $baseclasses = $options->{base} || "Test::Class";
$options->{helpers} //= ['Test::Most'];
$preamble .= "use base qw/${baseclasses}/;";
foreach my $helper (@{$options->{helpers}}) {
$preamble .= "use ${helper};";
}
if (my $testedclass = $options->{class_under_test}) {
$preamble .= "require ${testedclass} unless \%${testedclass}::; sub subject { \"${testedclass}\" };"
}
$ctx->scope_injector_call() . $preamble;
}
sub _parse_testclass {
my $pack = shift;
local $Carp::Internal{'Devel::Declare'} = 1;
my $ctx = Test::Class::Sugar::Context->new->init(@_);
$ctx->skip_declarator;
my $classname = $ctx->strip_testclass_name;
_testclass_generator($ctx, $classname, $ctx->strip_options)
->install_testclass;
}
sub testclass (&) {}
sub startup (&) {}
sub setup (&) {}
sub test (&) { croak "Should not be called" }
sub teardown (&) {}
sub shutdown (&) {}
1;
__END__
=head1 NAME
Test::Class::Sugar - Helper syntax for writing Test::Class tests
=head1 SYNOPSIS
use Test::Class::Sugar;
testclass exercises Person {
# Test::Most has been magically included
startup >> 1 {
use_ok $test->subject;
}
test autonaming {
is ref($test), 'Test::Person';
}
test the naming of parts {
is $test->current_method, 'test_the_naming_of_parts';
}
test multiple assertions >> 2 {
is ref($test), 'Test::Person';
is $test->current_method, 'test_multiple_assertions';
}
}
Test::Class->runtests;
=head1 DESCRIPTION
Test::Class::Sugar provides a new syntax for setting up your Test::Class based
tests. The idea is that we bundle up all the tedious boilerplate involved in
writing a class in favour of getting to the meat of what you're testing. We
made warranted assumptions about what you want to do, and we do them for
you. So, when you write
testclass exercises Person {
...
}
What Perl sees, after Test::Class::Sugar has done its work, is roughly:
{
package Test::Person;
use base qw/Test::Class/;
use strict; use warnings;
require Person;
sub subject { 'Person' };
...
}
Some of the assumptions we made are overrideable, others aren't. Yet. Most of
them will be though.
=head2 Why you shouldn't use Test::Class::Sugar
Test::Class::Sugar is very new, pretty untested and is inadvertently hostile
to you if you confuse its parser. Don't use it if you want to live.
=head2 Why you should use Test::Class::Sugar
But it's so shiny. Test::Class::Sugar was written to scratch an itch I had
when writing some tests for a L<MooseX::Declare> based module. Switching from
the implementation code to the test code was like shifting from fifth to first
gear in one fell swoop. Not fun. This is my attempt to sprinkle some
C<Devel::Declare> magic dust over the testing experience.
=head2 Bear this in mind
B<Test::Class::Sugar is not a source filter>
I know it looks like a source filter in the right light, but it isn't. Source
filters fall down because only perl can parse Perl, so it's easy to confuse
them. Devel::Declare based modules work by letting perl parse Perl until it
comes across a new keyword, at which point it temporarily hands parsing duty
over to a new parser which has the job of parsing the little language
introduced by the keyword, turning it into real Perl, and handing the
responsibility for parsing that back to Perl. Obviously, it's still possible
for that to screw things up royally, but there are fewer opportunities to fuck
up.
We now return you to your regularly schedule documentation.
=head1 SYNTAX
Essentially, Test::Class::Sugar adds some new keywords to perl. Here's what
they do, and what they expect.
=over
=item B<testclass>
testclass NAME?
( exercises CLASS
| extends CLASS (, CLASS)*
| +?uses HELPER (, HELPER)*
)*
Where B<NAME> is is an optional test class name - the sort of thing you're
used to writing after C<package>. You don't have to name your C<testclass>,
but if you don't supply a name, you MUST supply an exercises clause.
=over
=item exercises CLASS
You can supply at most one C<exercises> clause. This specifies the class under
test. We use it to autoname the class if you haven't provided a NAME of your
own (the default name of the class would be C<< Test::<CLASS> >>). Also, if
you supply an exercises clause, the class will be autorequired and your test
class will have a C<subject> helper method, which will return the name of the
class under test.
=item extends CLASS (, CLASS)*
Sometimes, you don't want to inherit directly from B<Test::Class>. If that's
the case, add an C<extends> clause, and your worries will be over. The extends
clause supports, but emphatically does not encourage, multiple
inheritance. Friends don't let friends do multiple inheritance, but
Test::Class::Sugar's not a friend, it's a robot servant which will provide you
with more than enough rope.
=item +?uses HELPER (, HELPER)*
Ah, the glory that is the C<uses> clause. If you don't provide a uses clause,
Test::Class::Sugar will assume that you want to use L<Test::Most> as your
testing only testing helper library. If you would rather use, say,
L<Test::More> then you can do:
testclass ExampleTest uses -More {...}
Hang on, C<-More>, what's that about? It's a simple shortcut. Instead of
making you write C<uses Test::This, Test::That, Test::TheOther>, you can write
C<uses -This, -That, -TheOther> and we'll expand the C<-> into C<Test::> and
do the right thing.
If, like me, you like Test::Most, but you want to use some other helper
modules, then you'd do:
testclass AnotherExample +uses -MockObject, Moose {...}
C<+uses> extends the list of helper classes, so as well as autousing
Test::Most, you'll get L<Test::MockObject> and L<Moose>.
Note that, if you need to do anything special in the way of import arguments,
you should do the C<use> yourself. We're all about the 80:20 rule here.
=back
=item B<test>
test WORD ( WORD )* (>> PLAN) { ... }
I may be fooling myself, but I hope its obvious what this does. Here's a few
examples to show you what's happening:
test with multiple subtests >> 3 {...}
test with no_plan >> no_plan {...}
test 'a complicated description with "symbols" in it' {...}
Gets translated to:
sub test_with_multiple_subtests : Test(3) {...}
sub test_with_no_plan : Test(no_plan) {...}
sub a_complicated_description_with_symbols_in_it : Test {...}
See L<Test::Class/Test> for details of C<PLAN>'s semantics.
=head2 Lifecycle Methods
=item B<startup>
=item B<setup>
=item B<teardown>
=item B<shutdown>
These lifecycle helpers work in pretty much the same way as L</test>, but with
the added wrinkle that, if you don't supply a name, they generate method names
derived from the name of the test class and the name of the helper, so, for
instance:
testclass Test::Lifecycle::Autonaming {
setup { ... }
}
is equivalent to writing:
testclass Test::Lifecycle::Autonaming {
setup 'setup_Test_Lifecycle_Autonaming' {...}
}
Other than that, the lifecycle helpers behave pretty much as described in
L<Test::Class|Test::Class/Test>. In particular, you can still give them names, so
testclass {
setup with a name {...}
}
works just fine.
=back
=head1 DIAGNOSTICS
Right now, Test::Class::Sugar's diagnostics range from the confusing to the
downright misleading. Expect progress on this in the future, tuit supply
permitting.
Patches welcome.
=head1 BUGS AND LIMITATIONS
=head2 Known bugs
=over
=item Screwy line numbers
Test::Class::Sugar can screw up the accord between the line perl thinks some
code is on and the line the code is I<actually> on. This makes debugging test
classes harder than it should be. Our error reporting is bad enough already
without making things worse.
=back
=head2 Unknown bugs
There's bound to be some.
=head2 Patches welcome.
Please report any bugs or feature requests to me. It's unlikely you'll get any
response if you use L<http://rt.cpan.org> though. Your best course of action
is to fork the project L<http://www.github.com/pdcawley/test-class-sugar>,
write at least one failing test (Write something in C<testclass> form that
should work, but doesn't. If you can arrange for it to fail gracefully, then
please do, but if all you do is write something that blows up spectacularly,
that's good too. Failing/exploding tests are like manna to a maintenance
programmer.
=head1 AUTHOR
Piers Cawley C<< <pdcawley@bofh.org.uk> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2009, Piers Cawley C<< <pdcawley@bofh.org.uk> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
=head1 DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.