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