summaryrefslogtreecommitdiff
path: root/cpan/Test-Harness/t/iterator_factory.t
blob: c0e3e5c8b407ba8520641fdfb06c1e0de1720984 (plain)
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
#!/usr/bin/perl -w
#
# Tests for TAP::Parser::IteratorFactory & source detection
##

BEGIN {
  unshift @INC, 't/lib';
}

use strict;

use Test::More tests => 42;

use IO::File;
use File::Spec;
use TAP::Parser::Source;
use TAP::Parser::IteratorFactory;

# Test generic API...
{
    can_ok 'TAP::Parser::IteratorFactory', 'new';
    my $sf = TAP::Parser::IteratorFactory->new;
    isa_ok $sf, 'TAP::Parser::IteratorFactory';
    can_ok $sf, 'config';
    can_ok $sf, 'handlers';
    can_ok $sf, 'detect_source';
    can_ok $sf, 'make_iterator';
    can_ok $sf, 'register_handler';

    # Set config
    eval { $sf->config('bad config') };
    my $e = $@;
    like $e, qr/\QArgument to &config must be a hash reference/,
      '... and calling config with bad config should fail';

    my $config = { MySourceHandler => { foo => 'bar' } };
    is( $sf->config($config), $sf, '... and set config works' );

    # Load/Register a handler
    $sf = TAP::Parser::IteratorFactory->new(
        { MySourceHandler => { accept => 'known-source' } } );
    can_ok( 'MySourceHandler', 'can_handle' );
    is_deeply( $sf->handlers, ['MySourceHandler'], '... was registered' );

    # Known source should pass
    {
        my $source   = TAP::Parser::Source->new->raw( \'known-source' );
        my $iterator = eval { $sf->make_iterator($source) };
        my $error    = $@;
        ok( !$error, 'make_iterator with known source doesnt fail' );
        diag($error) if $error;
        isa_ok( $iterator, 'MyIterator', '... and iterator class' );
    }

    # No known source should fail
    {
        my $source   = TAP::Parser::Source->new->raw( \'unknown-source' );
        my $iterator = eval { $sf->make_iterator($source) };
        my $error    = $@;
        ok( $error, 'make_iterator with unknown source fails' );
        like $error, qr/^Cannot detect source of 'unknown-source'/,
          '... with an appropriate error message';
    }
}

# Source detection
use_ok('TAP::Parser::SourceHandler::Executable');
use_ok('TAP::Parser::SourceHandler::Perl');
use_ok('TAP::Parser::SourceHandler::File');
use_ok('TAP::Parser::SourceHandler::RawTAP');
use_ok('TAP::Parser::SourceHandler::Handle');

my $test_dir = File::Spec->catdir(
    't',
    'source_tests'
);

my @sources = (
    {   file     => 'source.tap',
        handler  => 'TAP::Parser::SourceHandler::File',
        iterator => 'TAP::Parser::Iterator::Stream',
    },
    {   file     => 'source.1',
        handler  => 'TAP::Parser::SourceHandler::File',
        config   => { File => { extensions => ['.1'] } },
        iterator => 'TAP::Parser::Iterator::Stream',
    },
    {   file     => 'source.pl',
        handler  => 'TAP::Parser::SourceHandler::Perl',
        iterator => 'TAP::Parser::Iterator::Process',
    },
    {   file     => 'source.t',
        handler  => 'TAP::Parser::SourceHandler::Perl',
        iterator => 'TAP::Parser::Iterator::Process',
    },
    {   file     => 'source',
        handler  => 'TAP::Parser::SourceHandler::Perl',
        iterator => 'TAP::Parser::Iterator::Process',
    },
    {   file     => 'source.sh',
        handler  => 'TAP::Parser::SourceHandler::Executable',
        iterator => 'TAP::Parser::Iterator::Process',
    },
    {   file     => 'source.bat',
        handler  => 'TAP::Parser::SourceHandler::Executable',
        iterator => 'TAP::Parser::Iterator::Process',
    },
    {   name     => 'raw tap string',
        source   => "0..1\nok 1 - raw tap\n",
        handler  => 'TAP::Parser::SourceHandler::RawTAP',
        iterator => 'TAP::Parser::Iterator::Array',
    },
    {   name     => 'raw tap array',
        source   => [ "0..1\n", "ok 1 - raw tap\n" ],
        handler  => 'TAP::Parser::SourceHandler::RawTAP',
        iterator => 'TAP::Parser::Iterator::Array',
    },
    {   source   => \*__DATA__,
        handler  => 'TAP::Parser::SourceHandler::Handle',
        iterator => 'TAP::Parser::Iterator::Stream',
    },
    {   source   => IO::File->new('-'),
        handler  => 'TAP::Parser::SourceHandler::Handle',
        iterator => 'TAP::Parser::Iterator::Stream',
    },
);

for my $test (@sources) {
    local $TODO = $test->{TODO};
    if ( $test->{file} ) {
        $test->{name} = $test->{file};
        $test->{source} = File::Spec->catfile( $test_dir, $test->{file} );
    }

    my $name = $test->{name} || substr( $test->{source}, 0, 10 );
    my $sf
      = TAP::Parser::IteratorFactory->new( $test->{config} )->_testing(1);

    my $raw      = $test->{source};
    my $source   = TAP::Parser::Source->new->raw( ref($raw) ? $raw : \$raw );
    my $iterator = eval { $sf->make_iterator($source) };
    my $error    = $@;
    ok( !$error, "$name: no error on make_iterator" );
    diag($error) if $error;

    #    isa_ok( $iterator, $test->{iterator}, $name );
    is( $sf->_last_handler, $test->{handler}, $name );
}

__END__
0..1
ok 1 - TAP in the __DATA__ handle