This file is indexed.

/usr/share/perl5/Test/Roo/Cookbook.pm is in libtest-roo-perl 1.002-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use 5.008001;
use strictures;

package Test::Roo::Cookbook;
# ABSTRACT: Test::Roo examples
our $VERSION = '1.002'; # VERSION

1;


# vim: ts=4 sts=4 sw=4 et:

__END__

=pod

=encoding utf-8

=head1 NAME

Test::Roo::Cookbook - Test::Roo examples

=head1 VERSION

version 1.002

=head1 DESCRIPTION

This file offers usage ideas and examples for L<Test::Roo>.

=for Pod::Coverage method_names_here

=head1 ORGANIZING TEST CLASSES AND ROLES

=head2 Self-contained test file

A single test file could be used for simple tests where you want to
use Moo attributes for fixtures that get used by test blocks.

Here is an example that requires a C<corpus> attribute, stores
lines from that file in the C<lines> attribute and makes it
available to all test blocks:

    # examples/cookbook/single_file.t

    use Test::Roo;

    use MooX::Types::MooseLike::Base qw/ArrayRef/;
    use Path::Tiny;

    has corpus => (
        is       => 'ro',
        isa      => sub { -f shift },
        required => 1,
    );

    has lines => (
        is  => 'lazy',
        isa => ArrayRef,
    );

    sub _build_lines {
        my ($self) = @_;
        return [ map { lc } path( $self->corpus )->lines ];
    }

    test 'sorted' => sub {
        my $self = shift;
        is_deeply( $self->lines, [ sort @{$self->lines} ], "alphabetized");
    };

    test 'a to z' => sub {
        my $self = shift;
        my %letters = map { substr($_,0,1) => 1 } @{ $self->lines };
        is_deeply( [sort keys %letters], ["a" .. "z"], "all letters found" );
    };


    run_me( { corpus => "/usr/share/dict/words" } );
    # ... test other corpuses ...

    done_testing;

=head2 Standalone test class

You don't have to put the test class into the F<.t> file.  It's just a class.

Here is the same corpus checking example as before, but now as a class:

    # examples/cookbook/lib/CorpusCheck.pm

    package CorpusCheck;
    use Test::Roo;

    use MooX::Types::MooseLike::Base qw/ArrayRef/;
    use Path::Tiny;

    has corpus => (
        is       => 'ro',
        isa      => sub { -f shift },
        required => 1,
    );

    has lines => (
        is  => 'lazy',
        isa => ArrayRef,
    );

    sub _build_lines {
        my ($self) = @_;
        return [ map { lc } path( $self->corpus )->lines ];
    }

    test 'sorted' => sub {
        my $self = shift;
        is_deeply( $self->lines, [ sort @{$self->lines} ], "alphabetized");
    };

    test 'a to z' => sub {
        my $self = shift;
        my %letters = map { substr($_,0,1) => 1 } @{ $self->lines };
        is_deeply( [sort keys %letters], ["a" .. "z"], "all letters found" );
    };

    1;

Running it from a F<.t> file doesn't even need L<Test::Roo>:

    # examples/cookbook/standalone.t

    use strictures;
    use Test::More;

    use lib 'lib';
    use CorpusCheck;

    CorpusCheck->run_tests({ corpus => "/usr/share/dict/words" });

    done_testing;

=head2 Standalone Test Roles

The real power of L<Test::Roo> is decomposing test behaviors into
roles that can be reused.

Imagine we want to test a file-finder module like L<Path::Iterator::Rule>.
We could put tests for it into a role, then run the tests from a file that composes
that role.  For example, here would be the test file:

    # examples/cookbook/test-pir.pl

    use Test::Roo;

    use lib 'lib';

    with 'IteratorTest';

    run_me(
        {
            iterator_class => 'Path::Iterator::Rule',
            result_type    => '',
        }
    );

    done_testing;

Then in the distribution for L<Path::Class::Rule>, the same role
could be tested with a test file like this:

    # examples/cookbook/test-pcr.pl

    use Test::Roo;

    use lib 'lib';

    with 'IteratorTest';

    run_me(
        {
            iterator_class => 'Path::Class::Rule',
            result_type    => 'Path::Class::Entity',
        },
    );

    done_testing;

What is the common role that they are consuming?  It sets up a test
directory, creates files and runs tests:

    # examples/cookbook/lib/IteratorTest.pm

    package IteratorTest;
    use Test::Roo::Role;

    use MooX::Types::MooseLike::Base qw/:all/;
    use Class::Load qw/load_class/;
    use Path::Tiny;

    has [qw/iterator_class result_type/] => (
        is       => 'ro',
        isa      => Str,
        required => 1,
    );

    has test_files => (
        is      => 'ro',
        isa     => ArrayRef,
        default => sub {
            return [
                qw(
                aaaa
                bbbb
                cccc/dddd
                eeee/ffff/gggg
                )
            ];
        },
    );

    has tempdir => (
        is  => 'lazy',
        isa => InstanceOf ['Path::Tiny']
    );

    has rule_object => (
        is      => 'lazy',
        isa     => Object,
        clearer => 1,
    );

    sub _build_description { return shift->iterator_class }

    sub _build_tempdir {
        my ($self) = @_;
        my $dir = Path::Tiny->tempdir;
        $dir->child($_)->touchpath for @{ $self->test_files };
        return $dir;
    }

    sub _build_rule_object {
        my ($self) = @_;
        load_class( $self->iterator_class );
        return $self->iterator_class->new;
    }

    sub test_result_type {
        my ( $self, $file ) = @_;
        if ( my $type = $self->result_type ) {
            isa_ok( $file, $type, $file );
        }
        else {
            is( ref($file), '', "$file is string" );
        }
    }

    test 'find files' => sub {
        my $self = shift;
        $self->clear_rule_object; # make sure have a new one each time

        $self->tempdir;
        my $rule = $self->rule_object;
        my @files = $rule->file->all( $self->tempdir, { relative => 1 } );

        is_deeply( \@files, $self->test_files, "correct list of files" )
        or diag explain \@files;

        $self->test_result_type($_) for @files;
    };

    # ... more tests ...

    1;

=head1 CREATING AND MANAGING FIXTURES

=head2 Skipping all tests

If you need to skip all tests in the F<.t> file because some prerequisite
isn't available or some fixture couldn't be built, use a C<BUILD> method and
call C<< plan skip_all => $reason >>.

    use Class::Load qw/try_load_class/;

    has fixture => (
        is => 'lazy',
    );

    sub _build_fixture {
        # ... something that might die if unavailable ...
    }

    sub BUILD {
        my ($self) = @_;

        try_load_class('Class::Name')
            or plan skip_all => "Class::Name required to run these tests";

        eval { $self->fixture }
            or plan skip_all => "Couldn't build fixture";
    }

=head2 Setting a test description

You can override C<_build_description> to create a test description based
on other attributes.  For example, the C<IteratorTest> package earlier
had these lines:

    has [qw/iterator_class result_type/] => (
        is       => 'ro',
        isa      => Str,
        required => 1,
    );

    sub _build_description { return shift->iterator_class }

The C<iterator_class> attribute is required and then the description
is set to it.  Or, there could be a more verbose description:

    sub _build_description {
        my $name = shift->iterator_class;
        return "Testing the $name class"
    }

=head2 Requiring a builder

A test role can specify a lazy attribute and then require the
consuming class to provide a builder for it.

In the test role:

    has fixture => (
        is => 'lazy',
    );

    requires '_build_fixture';

In the consuming class:

    sub _build_fixture { ... }

=head2 Clearing fixtures

If a fixture has a clearer method, it can be easily reset during testing.
This works really well with lazy attributes which get regenerated on demand.

    has fixture => (
        is => 'lazy',
        clearer => 1,
    );

    test "some test" => sub {
        my $self = shift;
        $self->clear_fixture;
        ...
    };

=head1 MODIFIERS FOR SETUP AND TEARDOWN

=head2 Setting up a fixture before testing

When you need to do some extra work to set up a fixture, you can put a
method modifier on the C<setup> method.  In some cases, this is more
intuitive than doing all the work in an attribute builder.

Here is an example that creates an SQLite table before any tests are
run and cleans up afterwards:

    # example/cookbook/sqlite.t

    use Test::Roo;
    use DBI;
    use Path::Tiny;

    has tempdir => (
        is      => 'ro',
        clearer => 1,
        default => sub { Path::Tiny->tempdir },
    );

    has dbfile => (
        is      => 'lazy',
        default => sub { shift->tempdir->child('test.sqlite3') },
    );

    has dbh => ( is => 'lazy', );

    sub _build_dbh {
        my $self = shift;
        DBI->connect(
            "dbi:SQLite:dbname=" . $self->dbfile, { RaiseError => 1 }
        );
    }

    before 'setup' => sub {
        my $self = shift;
        $self->dbh->do("CREATE TABLE f (f1, f2, f3)");
    };

    after 'teardown' => sub { shift->clear_tempdir };

    test 'first' => sub {
        my $self = shift;
        my $dbh  = $self->dbh;
        my $sth  = $dbh->prepare("INSERT INTO f(f1,f2,f3) VALUES (?,?,?)");
        ok( $sth->execute( "one", "two", "three" ), "inserted data" );

        my $got = $dbh->selectrow_arrayref("SELECT * FROM f");
        is_deeply( $got, [qw/one two three/], "read data" );
    };

    run_me;
    done_testing;

=head2 Running tests during setup and teardown

You can run any tests you like during setup or teardown.  The previous example
could have written the setup and teardown hooks like this:

    before 'setup' => sub {
        my $self = shift;
        ok( ! -f $self->dbfile, "test database file not created" );
        ok( $self->dbh->do("CREATE TABLE f (f1, f2, f3)"), "created table");
        ok( -f $self->dbfile, "test database file exists" );
    };

    after 'teardown' => sub {
        my $self = shift;
        my $dir = $self->tempdir;
        $self->clear_tempdir;
        ok( ! -f $dir, "tempdir cleaned up");
    };

=head1 MODIFIERS ON TESTS

=head2 Global modifiers with C<each_test>

Modifying C<each_test> triggers methods before or after B<every> test block
defined with the C<test> function.  Because this affects all tests, whether
from the test class or composed from roles, it needs to be used thoughtfully.

Here is an example that ensures that every test block is run in its own
separate temporary directory.

    # examples/cookbook/with_tempd.t

    use Test::Roo;
    use File::pushd qw/tempd/;
    use Cwd qw/getcwd/;

    has tempdir => (
        is => 'lazy',
        isa => sub { shift->isa('File::pushd') },
        clearer => 1,
    );

    # tempd changes directory until the object is destroyed
    # and the fixture caches the object until cleared
    sub _build_tempdir { return tempd() }

    # building attribute will change to temp directory
    before each_test => sub { shift->tempdir };

    # clearing attribute will change to original directory
    after each_test => sub { shift->clear_tempdir };

    # do stuff in a temp directory
    test 'first test' => sub {
        my $self = shift;
        is( $self->tempdir, getcwd(), "cwd is " . $self->tempdir );
        # ... more tests ...
    };

    # do stuff in a separate, fresh temp directory
    test 'second test' => sub {
        my $self = shift;
        is( $self->tempdir, getcwd(), "cwd is " . $self->tempdir );
        # ... more tests ...
    };

    run_me;
    done_testing;

=head2 Individual test modifiers

If you want to have method modifiers on an individual test, put your
L<Test::More> tests in a method, add modifiers to that method, and use C<test>
to invoke it.

    # examples/cookbook/hookable_test.t

    use Test::Roo;

    has counter => ( is => 'rw', default => sub { 0 } );

    sub is_positive {
        my $self = shift;
        ok( $self->counter > 0, "counter is positive" );
    }

    before is_positive => sub { shift->counter( 1 ) };

    test 'hookable' => sub { shift->is_positive };

    run_me;
    done_testing;

=head2 Wrapping tests

As a middle ground between global and individual modifiers, if you need to call
some code repeatedly for some, but not all all tests, you can create a custom
test function.  This might make sense for only a few tests, but could be
helpful if there are many that need similar behavior, but you can't make it
global by modifying C<each_test>.

The following example clears the fixture before tests defined with the
C<fresh_test> function.

    # examples/cookbook/wrapped.t

    use strict;
    use Test::Roo;

    has fixture => (
        is => 'rw',
        lazy => 1,
        builder => 1,
        clearer => 1,
    );

    sub _build_fixture { "Hello World" }

    sub fresh_test {
        my ($name, $code) = @_;
        test $name, sub {
            my $self = shift;
            $self->clear_fixture;
            $code->($self);
        };
    }

    fresh_test 'first' => sub {
        my $self = shift;
        is ( $self->fixture, 'Hello World', "fixture has default" );
        $self->fixture("Goodbye World");
    };

    fresh_test 'second' => sub {
        my $self = shift;
        is ( $self->fixture, 'Hello World', "fixture has default" );
    };

    run_me;
    done_testing;

=head1 AUTHOR

David Golden <dagolden@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by David Golden.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004

=cut