Routine/Manual/Demo.pm untested

filter: Statementstmt 0.0% 0 / 7 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 9 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.12.0;
2 use warnings;
3 package Test::Routine::Manual::Demo 0.032;
4 # ABSTRACT: a walkthrough, in code, of Test::Routine
5  
6 #pod =head1 The Demo
7 #pod
8 #pod =head2 t/demo/01-demo.t
9 #pod
10 #pod #!/bin/env perl
11 #pod use strict;
12 #pod use warnings;
13 #pod
14 #pod # This test is both a test and an example of how Test::Routine works! Welcome
15 #pod # to t/01-demo.t, I will be your guide, rjbs.
16 #pod
17 #pod {
18 #pod # This block defines the HashTester package. It's a Test::Routine, meaning
19 #pod # it's a role. We define state that the test will need to keep and any
20 #pod # requirements we might have.
21 #pod #
22 #pod # Before we can run this test, we'll need to compose the role into a class so
23 #pod # that we can make an instance.
24 #pod package HashTester;
25 #pod use Test::Routine;
26 #pod
27 #pod # We import stuff from Test::More because, well, who wants to re-write all
28 #pod # those really useful test routines that exist out there? Maybe somebody,
29 #pod # but not me.
30 #pod use Test::More;
31 #pod
32 #pod # ...but then we use namespace::autoclean to get rid of the routines once
33 #pod # we've bound to them. This is just standard Moose practice, anyway, right?
34 #pod use namespace::autoclean;
35 #pod
36 #pod # Finally, some state! Every test will get called as method on an instance,
37 #pod # and it will have this attribute. Here are some points of interest:
38 #pod #
39 #pod # - We're giving this attribute a builder, so it will try to get built with a
40 #pod # call to $self->build_hash_to_test -- so each class that composes this
41 #pod # role can provide means for these attributes (fixtures) to be generated as
42 #pod # needed.
43 #pod #
44 #pod # - We are not adding "requires 'build_hash_to_test'", because then we can
45 #pod # apply this role to Moose::Object and instantiate it with a given value
46 #pod # in the constructor. There will be an example of this below. This lets
47 #pod # us re-use these tests in many variations without having to write class
48 #pod # after class.
49 #pod #
50 #pod # - We don't use lazy_build because it would create a clearer. If someone
51 #pod # then cleared our lazy_build fixture, it could not be re-built in the
52 #pod # event that we'd gotten it explicitly from the constructor!
53 #pod #
54 #pod # Using Moose attributes for our state and fixtures allows us to get all of
55 #pod # their powerful behaviors like types, delegation, traits, and so on, and
56 #pod # allows us to decompose shared behavior into roles.
57 #pod #
58 #pod has hash_to_test => (
59 #pod is => 'ro',
60 #pod isa => 'HashRef',
61 #pod builder => 'build_hash_to_test',
62 #pod );
63 #pod
64 #pod # Here, we're just declaring an actual test that we will run. This sub will
65 #pod # get installed as a method with a name that won't get clobbered easily. The
66 #pod # method will be found later by run_tests so we can find and execute all
67 #pod # tests on an instance.
68 #pod #
69 #pod # There is nothing magical about this method! Calling this method is
70 #pod # performed in a Test::More subtest block. A TAP plan can be issued with
71 #pod # "plan", and we can issue TODO or SKIP directives the same way. There is
72 #pod # none of the return-to-skip magic that we find in Test::Class.
73 #pod #
74 #pod # The string after "test" is used as the method name -- which means we're
75 #pod # getting a method name with spaces in it. This can be slightly problematic
76 #pod # if you try to use, say, ::, in a method name. For the most part, it works
77 #pod # quite well -- but look at the next test for an example of how to give an
78 #pod # explicit description.
79 #pod test "only one key in hash" => sub {
80 #pod my ($self) = @_;
81 #pod
82 #pod my $hash = $self->hash_to_test;
83 #pod
84 #pod is(keys %$hash, 1, "we have one key in our test hash");
85 #pod is(2+2, 4, "universe still okay");
86 #pod };
87 #pod
88 #pod # The only thing of note here is that we're passing a hashref of extra args
89 #pod # to the test method constructor. "desc" lets us set the test's description,
90 #pod # which is used in the test output, so we can avoid weird method names being
91 #pod # installed. Also note that we order tests more or less by order of
92 #pod # definition, not by name or description.
93 #pod test second_test => { desc => "Test::Routine demo!" } => sub {
94 #pod pass("We're running this test second");
95 #pod pass("...notice that the subtest's label is the 'desc' above");
96 #pod pass("...and not the method name!");
97 #pod };
98 #pod }
99 #pod
100 #pod {
101 #pod # This package is one fixture against which we can run the HashTester
102 #pod # routine. It has the only thing it needs: a build_hash_to_test method.
103 #pod # Obviously real examples would have more to them than this.
104 #pod package ProcessHash;
105 #pod use Moose;
106 #pod with 'HashTester';
107 #pod
108 #pod use namespace::autoclean;
109 #pod
110 #pod sub build_hash_to_test { return { $$ => $^T } }
111 #pod }
112 #pod
113 #pod # Now we're into the body of the test program: where tests actually get run.
114 #pod
115 #pod # We use Test::Routine::Util to get its "run_tests" routine, which runs the
116 #pod # tests on an instance, building it if needed.
117 #pod use Test::Routine::Util;
118 #pod
119 #pod # We use Test::More to get done_testing. We don't assume that run_tests is the
120 #pod # entire test, because that way we can (as we do here) run multiple test
121 #pod # instances, and can intersperse other kinds of sanity checks amongst the
122 #pod # Test::Routine-style tests.
123 #pod use Test::More;
124 #pod
125 #pod is(2+2, 4, "universe still makes sense") or BAIL_OUT("PANIC!");
126 #pod
127 #pod # The first arg is a description for the subtest that will be run. The second,
128 #pod # here, is a class that will be instantiated and tested.
129 #pod run_tests('ProcessHash class' => 'ProcessHash');
130 #pod
131 #pod # Here, the second argument is an instance of a class to test.
132 #pod run_tests('ProcessHash obj' => ProcessHash->new({ hash_to_test => { 1 => 0 }}));
133 #pod
134 #pod # We could also just supply a class name and a set of args to pass to new.
135 #pod # The below is very nearly equivalent to the above:
136 #pod run_tests('ProcessHash new' => ProcessHash => { hash_to_test => { 1 => 0 }});
137 #pod
138 #pod # ...and here, the second arg is not a class or instance at all, but the
139 #pod # Test::Routine role (by name). Since we know we can't instantiate a role,
140 #pod # run_tests will try to compose it with Moose::Object. Then the args are used
141 #pod # as the args to ->new on the new class, as above. This lets us write
142 #pod # Test::Routines that can be tested with the right state to start with, or
143 #pod # Test::Routines that need to be composed with testing fixture classes.
144 #pod run_tests(
145 #pod 'HashTester with given state',
146 #pod HashTester => {
147 #pod hash_to_test => { a => 1 },
148 #pod },
149 #pod );
150 #pod
151 #pod # There's one more interesting way to run out tests, but it's demonstrated in
152 #pod # 02-simple.t instead of here. Go check that out.
153 #pod
154 #pod # ...and we're done!
155 #pod done_testing;
156 #pod
157 #pod
158 #pod =head2 t/demo/02-simple.t
159 #pod
160 #pod # Welcome to part two of the Test::Routine demo. This is showing how you can
161 #pod # write quick one-off tests without having to write a bunch of .pm files or
162 #pod # (worse?) embed packages in bare blocks in the odious way that 01-demo.t did.
163 #pod #
164 #pod # First off, we use Test::Routine. As it did before, this turns the current
165 #pod # package (main!) into a Test::Routine role. It also has the pleasant
166 #pod # side-effect of turning on strict and warnings.
167 #pod use Test::Routine;
168 #pod
169 #pod # Then we bring in the utils, because we'll want to run_tests later.
170 #pod use Test::Routine::Util;
171 #pod
172 #pod # And, finally, we bring in Test::More so that we can use test assertions, and
173 #pod # namespace::autoclean to clean up after us.
174 #pod use Test::More;
175 #pod use namespace::autoclean;
176 #pod
177 #pod # We're going to give our tests some state. It's nothing special.
178 #pod has counter => (
179 #pod is => 'rw',
180 #pod isa => 'Int',
181 #pod default => 0,
182 #pod );
183 #pod
184 #pod # Then another boring but useful hunk of code: a method for our test routine.
185 #pod sub counter_is_even {
186 #pod my ($self) = @_;
187 #pod return $self->counter % 2 == 0;
188 #pod }
189 #pod
190 #pod # Then we can write some tests, just like we did before. Here, we're writing
191 #pod # several tests, and they will be run in the order in which they were defined.
192 #pod # You can see that they rely on the state being maintained.
193 #pod test 'start even' => sub {
194 #pod my ($self) = @_;
195 #pod ok($self->counter_is_even, "we start with an even counter");
196 #pod
197 #pod $self->counter( $self->counter + 1);
198 #pod };
199 #pod
200 #pod test 'terminate odd' => sub {
201 #pod my ($self) = @_;
202 #pod
203 #pod ok(! $self->counter_is_even, "the counter is odd, so state was preserved");
204 #pod pass("for your information, the counter is " . $self->counter);
205 #pod };
206 #pod
207 #pod # Now we can run these tests just by saying "run_me" -- rather than expecting a
208 #pod # class or role name, it uses the caller. In this case, the calling package
209 #pod # (main!) is a Test::Routine, so the runner composes it with Moose::Object,
210 #pod # instantiating it, and running the tests on the instance.
211 #pod run_me;
212 #pod
213 #pod # Since each test run gets its own instance, we can run the test suite again,
214 #pod # possibly to verify that the test suite is not destructive of some external
215 #pod # state.
216 #pod run_me("second run");
217 #pod
218 #pod # And we can pass in args to use when constructing the object to be tested.
219 #pod # Given the tests above, we can pick any starting value for "counter" that is
220 #pod # even.
221 #pod run_me({ counter => 192 });
222 #pod
223 #pod # ...and we're done!
224 #pod done_testing;
225 #pod
226 #pod # More Test::Routine behavior is demonstrated in t/03-advice.t and t/04-misc.t
227 #pod # Go have a look at those!
228 #pod
229 #pod
230 #pod =head2 t/demo/03-advice.t
231 #pod
232 #pod use Test::Routine;
233 #pod use Test::Routine::Util;
234 #pod use Test::More;
235 #pod
236 #pod use namespace::autoclean;
237 #pod
238 #pod # xUnit style testing has the idea of setup and teardown that happens around
239 #pod # each test. With Test::Routine, we assume that you will do most of this sort
240 #pod # of thing in your BUILD, DEMOLISH, and attribute management. Still, you can
241 #pod # easily do setup and teardown by applying method modifiers to the "run_test"
242 #pod # method, which your Test::Routine uses to run each test. Here's a simple
243 #pod # example.
244 #pod
245 #pod # We have the same boring state that we saw before. It's just an integer that
246 #pod # is carried over between tests.
247 #pod has counter => (
248 #pod is => 'rw',
249 #pod isa => 'Int',
250 #pod lazy => 1,
251 #pod default => 0,
252 #pod clearer => 'clear_counter',
253 #pod );
254 #pod
255 #pod # The first test changes the counter's value and leaves it changed.
256 #pod test test_0 => sub {
257 #pod my ($self) = @_;
258 #pod
259 #pod is($self->counter, 0, 'start with counter = 0');
260 #pod $self->counter( $self->counter + 1);
261 #pod is($self->counter, 1, 'end with counter = 1');
262 #pod };
263 #pod
264 #pod # The second test assumes that the value is the default, again. We want to
265 #pod # make sure that before each test, the counter is reset, but we don't want to
266 #pod # tear down and recreate the whole object, because it may have other, more
267 #pod # expensive resources built.
268 #pod test test_1 => sub {
269 #pod my ($self) = @_;
270 #pod
271 #pod is($self->counter, 0, 'counter is reset between tests');
272 #pod };
273 #pod
274 #pod # ...so we apply a "before" modifier to each test run, calling the clearer on
275 #pod # the counter. When next accessed, it will re-initialize to zero. We could
276 #pod # call any other code we want here, and we can compose numerous modifiers
277 #pod # together onto run_test.
278 #pod #
279 #pod # If you want to clear *all* the object state between each test... you probably
280 #pod # want to refactor.
281 #pod before run_test => sub { $_[0]->clear_counter };
282 #pod
283 #pod run_me;
284 #pod done_testing;
285 #pod
286 #pod
287 #pod =head2 t/demo/04-misc.t
288 #pod
289 #pod use Test::Routine;
290 #pod use Test::Routine::Util;
291 #pod use Test::More;
292 #pod
293 #pod use namespace::autoclean;
294 #pod
295 #pod # One thing that the previous examples didn't show was how to mark tests as
296 #pod # "skipped" or "todo." Test::Routine makes -no- provisions for these
297 #pod # directives. Instead, it assumes you will use the entirely usable mechanisms
298 #pod # provided by Test::More.
299 #pod
300 #pod # This is a normal test. It is neither skipped nor todo.
301 #pod test boring_ordinary_tests => sub {
302 #pod pass("This is a plain old boring test that always passes.");
303 #pod pass("It's here just to remind you what they look like.");
304 #pod };
305 #pod
306 #pod # To skip a test, we just add a "skip_all" plan. Because test methods get run
307 #pod # in subtests, this skips the whole subtest, but nothing else.
308 #pod test sample_skip_test => sub {
309 #pod plan skip_all => "these tests don't pass, for some reason";
310 #pod
311 #pod is(6, 9, "I don't mind.");
312 #pod };
313 #pod
314 #pod # To mark a test todo, we just set our local $TODO variable. Because the test
315 #pod # is its own block, this works just like it would in any other Test::More test.
316 #pod test sample_todo_test => sub {
317 #pod local $TODO = 'demo of todo';
318 #pod
319 #pod is(2 + 2, 5, "we can bend the fabric of reality");
320 #pod };
321 #pod
322 #pod run_me;
323 #pod done_testing;
324 #pod
325 #pod
326 #pod =head2 t/demo/05-multiple.t
327 #pod
328 #pod #!/bin/env perl
329 #pod use strict;
330 #pod use warnings;
331 #pod
332 #pod use Test::Routine::Util;
333 #pod use Test::More;
334 #pod
335 #pod # One of the benefits of building our sets of tests into roles instead of
336 #pod # classes is that we can re-use them in whatever combination we want. We can
337 #pod # break down sets of tests into bits that can be re-used in different cases.
338 #pod # With classes, this would lead to multiple inheritance or other monstrosities.
339 #pod
340 #pod # Here's a first Test::Routine. We use it to make sure that one of our
341 #pod # fixture's attributes is a numeric id.
342 #pod {
343 #pod package Test::ThingHasID;
344 #pod use Test::Routine;
345 #pod use Test::More;
346 #pod
347 #pod requires 'id';
348 #pod
349 #pod test thing_has_numeric_id => sub {
350 #pod my ($self) = @_;
351 #pod
352 #pod my $id = $self->id;
353 #pod like($id, qr/\A[0-9]+\z/, "the thing's id is a string of ascii digits");
354 #pod };
355 #pod }
356 #pod
357 #pod # A second one ensures that the thing has an associated directory that
358 #pod # looks like a unix path.
359 #pod {
360 #pod package Test::HasDirectory;
361 #pod use Test::Routine;
362 #pod use Test::More;
363 #pod
364 #pod requires 'dir';
365 #pod
366 #pod test thing_has_unix_dir => sub {
367 #pod my ($self) = @_;
368 #pod
369 #pod my $dir = $self->dir;
370 #pod like($dir, qr{\A(?:/\w+)+/?\z}, "thing has a unix-like directory");
371 #pod };
372 #pod }
373 #pod
374 #pod # We might have one class that is only expected to pass one test:
375 #pod {
376 #pod package JustHasID;
377 #pod use Moose;
378 #pod
379 #pod has id => (
380 #pod is => 'ro',
381 #pod default => sub {
382 #pod my ($self) = @_;
383 #pod return Scalar::Util::refaddr($self);
384 #pod },
385 #pod );
386 #pod }
387 #pod
388 #pod # ...and another class that should pass both:
389 #pod {
390 #pod package UnixUser;
391 #pod use Moose;
392 #pod
393 #pod has id => (is => 'ro', default => 501);
394 #pod has dir => (is => 'ro', default => '/home/users/rjbs');
395 #pod }
396 #pod
397 #pod # So far, none of this is new, it's just a slightly different way of factoring
398 #pod # things we've seen before. In t/01-demo.t, we wrote distinct test roles and
399 #pod # classes, and we made our class compose the role explicitly. This can be
400 #pod # a useful way to put these pieces together, but we also might want to write
401 #pod # all these classes and roles as unconnected components and compose them only
402 #pod # when we're ready to run our tests. When we do that, we can tell run_tests
403 #pod # what to put together.
404 #pod #
405 #pod # Here, we tell it that we can test JustHasID with Test::ThingHasID:
406 #pod run_tests(
407 #pod "our JustHasID objects have ids",
408 #pod [ 'JustHasID', 'Test::ThingHasID' ],
409 #pod );
410 #pod
411 #pod # ...but we can run two test routines against our UnixUser class
412 #pod run_tests(
413 #pod "unix users have dirs and ids",
414 #pod [ 'UnixUser', 'Test::ThingHasID', 'Test::HasDirectory' ],
415 #pod );
416 #pod
417 #pod
418 #pod # We can still use the "attributes to initialize an object," and when doing
419 #pod # that it may be that we don't care to run all the otherwise applicable tests,
420 #pod # because they're not interesting in the scenario we're creating. For
421 #pod # example...
422 #pod run_tests(
423 #pod "a trailing slash is okay in a directory",
424 #pod [ 'UnixUser', 'Test::HasDirectory' ],
425 #pod { dir => '/home/meebo/' },
426 #pod );
427 #pod
428 #pod # ...and we're done!
429 #pod done_testing;
430 #pod
431 #pod
432 #pod
433 #pod
434 #pod =cut
435  
436 1;
437  
438 __END__