1#!/usr/bin/perl -w 2# 3# Tests for TAP::Parser::IteratorFactory & source detection 4## 5 6BEGIN { 7 unshift @INC, 't/lib'; 8} 9 10use strict; 11use warnings; 12 13use Test::More tests => 44; 14 15use IO::File; 16use File::Spec; 17use TAP::Parser::Source; 18use TAP::Parser::IteratorFactory; 19 20# Test generic API... 21{ 22 can_ok 'TAP::Parser::IteratorFactory', 'new'; 23 my $sf = TAP::Parser::IteratorFactory->new; 24 isa_ok $sf, 'TAP::Parser::IteratorFactory'; 25 can_ok $sf, 'config'; 26 can_ok $sf, 'handlers'; 27 can_ok $sf, 'detect_source'; 28 can_ok $sf, 'make_iterator'; 29 can_ok $sf, 'register_handler'; 30 31 # Set config 32 eval { $sf->config('bad config') }; 33 my $e = $@; 34 like $e, qr/\QArgument to &config must be a hash reference/, 35 '... and calling config with bad config should fail'; 36 37 my $config = { MySourceHandler => { foo => 'bar' } }; 38 is( $sf->config($config), $sf, '... and set config works' ); 39 40 # Load/Register a handler 41 $sf = TAP::Parser::IteratorFactory->new( 42 { MySourceHandler => { accept => 'known-source' } } ); 43 can_ok( 'MySourceHandler', 'can_handle' ); 44 is_deeply( $sf->handlers, ['MySourceHandler'], '... was registered' ); 45 46 # Known source should pass 47 { 48 my $source = TAP::Parser::Source->new->raw( \'known-source' ); 49 my $iterator = eval { $sf->make_iterator($source) }; 50 my $error = $@; 51 ok( !$error, 'make_iterator with known source doesnt fail' ); 52 diag($error) if $error; 53 isa_ok( $iterator, 'MyIterator', '... and iterator class' ); 54 } 55 56 # No known source should fail 57 { 58 my $source = TAP::Parser::Source->new->raw( \'unknown-source' ); 59 my $iterator = eval { $sf->make_iterator($source) }; 60 my $error = $@; 61 ok( $error, 'make_iterator with unknown source fails' ); 62 like $error, qr/^Cannot detect source of 'unknown-source'/, 63 '... with an appropriate error message'; 64 } 65} 66 67# Source detection 68use_ok('TAP::Parser::SourceHandler::Executable'); 69use_ok('TAP::Parser::SourceHandler::Perl'); 70use_ok('TAP::Parser::SourceHandler::File'); 71use_ok('TAP::Parser::SourceHandler::RawTAP'); 72use_ok('TAP::Parser::SourceHandler::Handle'); 73 74my $test_dir = File::Spec->catdir( 75 't', 76 'source_tests' 77); 78 79my @sources = ( 80 { file => 'source.tap', 81 handler => 'TAP::Parser::SourceHandler::File', 82 iterator => 'TAP::Parser::Iterator::Stream', 83 }, 84 { file => 'source.1', 85 handler => 'TAP::Parser::SourceHandler::File', 86 config => { File => { extensions => ['.1'] } }, 87 iterator => 'TAP::Parser::Iterator::Stream', 88 }, 89 { file => 'source.pl', 90 handler => 'TAP::Parser::SourceHandler::Perl', 91 iterator => 'TAP::Parser::Iterator::Process', 92 }, 93 { file => 'source.t', 94 handler => 'TAP::Parser::SourceHandler::Perl', 95 iterator => 'TAP::Parser::Iterator::Process', 96 }, 97 { file => 'source', 98 handler => 'TAP::Parser::SourceHandler::Perl', 99 iterator => 'TAP::Parser::Iterator::Process', 100 }, 101 { file => 'source.sh', 102 handler => 'TAP::Parser::SourceHandler::Perl', 103 iterator => 'TAP::Parser::Iterator::Process', 104 }, 105 { file => 'source.bat', 106 handler => 'TAP::Parser::SourceHandler::Executable', 107 iterator => 'TAP::Parser::Iterator::Process', 108 }, 109 { name => 'raw tap string', 110 source => "0..1\nok 1 - raw tap\n", 111 handler => 'TAP::Parser::SourceHandler::RawTAP', 112 iterator => 'TAP::Parser::Iterator::Array', 113 }, 114 { name => 'raw tap array', 115 source => [ "0..1\n", "ok 1 - raw tap\n" ], 116 handler => 'TAP::Parser::SourceHandler::RawTAP', 117 iterator => 'TAP::Parser::Iterator::Array', 118 }, 119 { source => \*__DATA__, 120 handler => 'TAP::Parser::SourceHandler::Handle', 121 iterator => 'TAP::Parser::Iterator::Stream', 122 }, 123 { source => IO::File->new('-'), 124 handler => 'TAP::Parser::SourceHandler::Handle', 125 iterator => 'TAP::Parser::Iterator::Stream', 126 }, 127 { file => 'test.tap', 128 tie => 1, 129 }, 130); 131 132for my $test (@sources) { 133 local $TODO = $test->{TODO}; 134 if ( $test->{file} ) { 135 $test->{name} = $test->{file}; 136 $test->{source} = File::Spec->catfile( $test_dir, $test->{file} ); 137 } 138 139 my $name = $test->{name} || substr( $test->{source}, 0, 10 ); 140 my $sf 141 = TAP::Parser::IteratorFactory->new( $test->{config} )->_testing(1); 142 143 my $raw = $test->{source}; 144 my $source = TAP::Parser::Source->new->raw( ref($raw) ? $raw : \$raw ); 145 my $iterator = eval { $sf->make_iterator($source) }; 146 my $error = $@; 147 148 if( $test->{tie} ) { 149 like( 150 $error, qr{^There is a tie.*Both voted .* on $test->{file}}ms, 151 "$name: votes tied" 152 ) 153 } 154 else { 155 ok( !$error, "$name: no error on make_iterator" ); 156 diag($error) if $error; 157 } 158 159 is( $sf->_last_handler, $test->{handler}, $name ); 160} 161 162__END__ 1630..1 164ok 1 - TAP in the __DATA__ handle 165