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