1#!/usr/bin/perl -w 2 3BEGIN { 4 unshift @INC, 't/lib'; 5} 6 7use strict; 8use vars qw(%INIT %CUSTOM); 9 10use Test::More tests => 24; 11use File::Spec::Functions qw( catfile updir ); 12 13use_ok('TAP::Parser::SubclassTest'); 14 15# TODO: foreach my $source ( ... ) 16{ # perl source 17 %INIT = %CUSTOM = (); 18 my $source = catfile( 't', 'subclass_tests', 'perl_source' ); 19 my $p = TAP::Parser::SubclassTest->new( { source => $source } ); 20 21 # The grammar is lazily constructed so we need to ask for it to 22 # trigger it's creation. 23 my $grammer = $p->_grammar; 24 25 ok( $p->{initialized}, 'new subclassed parser' ); 26 27 is( $p->source_class => 'MySource', 'source_class' ); 28 is( $p->perl_source_class => 'MyPerlSource', 'perl_source_class' ); 29 is( $p->grammar_class => 'MyGrammar', 'grammar_class' ); 30 is( $p->iterator_factory_class => 'MyIteratorFactory', 31 'iterator_factory_class' 32 ); 33 is( $p->result_factory_class => 'MyResultFactory', 34 'result_factory_class' 35 ); 36 37 is( $INIT{MyPerlSource}, 1, 'initialized MyPerlSource' ); 38 is( $CUSTOM{MyPerlSource}, 1, '... and it was customized' ); 39 is( $INIT{MyGrammar}, 1, 'initialized MyGrammar' ); 40 is( $CUSTOM{MyGrammar}, 1, '... and it was customized' ); 41 42 # make sure overrided make_* methods work... 43 %CUSTOM = (); 44 $p->make_source; 45 is( $CUSTOM{MySource}, 1, 'make custom source' ); 46 $p->make_perl_source; 47 is( $CUSTOM{MyPerlSource}, 1, 'make custom perl source' ); 48 $p->make_grammar; 49 is( $CUSTOM{MyGrammar}, 1, 'make custom grammar' ); 50 $p->make_iterator; 51 is( $CUSTOM{MyIterator}, 1, 'make custom iterator' ); 52 $p->make_result; 53 is( $CUSTOM{MyResult}, 1, 'make custom result' ); 54 55 # make sure parser helpers use overrided classes too (the parser should 56 # be the central source of configuration/overriding functionality) 57 # The source is already tested above (parser doesn't keep a copy of the 58 # source currently). So only one to check is the Grammar: 59 %INIT = %CUSTOM = (); 60 my $r = $p->_grammar->tokenize; 61 isa_ok( $r, 'MyResult', 'i has results' ); 62 is( $INIT{MyResult}, 1, 'initialized MyResult' ); 63 is( $CUSTOM{MyResult}, 1, '... and it was customized' ); 64 is( $INIT{MyResultFactory}, 1, '"initialized" MyResultFactory' ); 65} 66 67SKIP: { # non-perl source 68 %INIT = %CUSTOM = (); 69 my $cat = '/bin/cat'; 70 unless ( -e $cat ) { 71 skip "no '$cat'", 4; 72 } 73 my $file = catfile( 't', 'data', 'catme.1' ); 74 my $p = TAP::Parser::SubclassTest->new( { exec => [ $cat => $file ] } ); 75 76 is( $INIT{MySource}, 1, 'initialized MySource subclass' ); 77 is( $CUSTOM{MySource}, 1, '... and it was customized' ); 78 is( $INIT{MyIterator}, 1, 'initialized MyIterator subclass' ); 79 is( $CUSTOM{MyIterator}, 1, '... and it was customized' ); 80} 81