~ubuntu-branches/ubuntu/trusty/libtest-roo-perl/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/cookbook/lib/IteratorTest.pm

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2014-01-14 19:25:11 UTC
  • Revision ID: package-import@ubuntu.com-20140114192511-9ycl4dc1zgamfnek
Tags: upstream-1.002
ImportĀ upstreamĀ versionĀ 1.002

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package IteratorTest;
 
2
use Test::Roo::Role;
 
3
 
 
4
use MooX::Types::MooseLike::Base qw/:all/;
 
5
use Class::Load qw/load_class/;
 
6
use Path::Tiny;
 
7
 
 
8
has [qw/iterator_class result_type/] => (
 
9
    is       => 'ro',
 
10
    isa      => Str,
 
11
    required => 1,
 
12
);
 
13
 
 
14
has test_files => (
 
15
    is      => 'ro',
 
16
    isa     => ArrayRef,
 
17
    default => sub {
 
18
        return [
 
19
            qw(
 
20
              aaaa
 
21
              bbbb
 
22
              cccc/dddd
 
23
              eeee/ffff/gggg
 
24
              )
 
25
        ];
 
26
    },
 
27
);
 
28
 
 
29
has tempdir => (
 
30
    is  => 'lazy',
 
31
    isa => InstanceOf ['Path::Tiny']
 
32
);
 
33
 
 
34
has rule_object => (
 
35
    is      => 'lazy',
 
36
    isa     => Object,
 
37
    clearer => 1,
 
38
);
 
39
 
 
40
sub _build_description { return shift->iterator_class }
 
41
 
 
42
sub _build_tempdir {
 
43
    my ($self) = @_;
 
44
    my $dir = Path::Tiny->tempdir;
 
45
    $dir->child($_)->touchpath for @{ $self->test_files };
 
46
    return $dir;
 
47
}
 
48
 
 
49
sub _build_rule_object {
 
50
    my ($self) = @_;
 
51
    load_class( $self->iterator_class );
 
52
    return $self->iterator_class->new;
 
53
}
 
54
 
 
55
sub test_result_type {
 
56
    my ( $self, $file ) = @_;
 
57
    if ( my $type = $self->result_type ) {
 
58
        isa_ok( $file, $type, $file );
 
59
    }
 
60
    else {
 
61
        is( ref($file), '', "$file is string" );
 
62
    }
 
63
}
 
64
 
 
65
test 'find files' => sub {
 
66
    my $self = shift;
 
67
    $self->clear_rule_object; # make sure have a new one each time
 
68
 
 
69
    $self->tempdir;
 
70
    my $rule = $self->rule_object;
 
71
    my @files = $rule->file->all( $self->tempdir, { relative => 1 } );
 
72
 
 
73
    is_deeply( \@files, $self->test_files, "correct list of files" )
 
74
      or diag explain \@files;
 
75
 
 
76
    $self->test_result_type($_) for @files;
 
77
};
 
78
 
 
79
# ... more tests ...
 
80
 
 
81
1;