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