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