Debian lenny version packages
[pkg-perl] / deb-src / libtest-harness-perl / libtest-harness-perl-3.12 / t / iterators.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 't/lib';
5
6 use Test::More tests => 76;
7
8 use File::Spec;
9 use TAP::Parser;
10 use TAP::Parser::IteratorFactory;
11 use Config;
12
13 sub array_ref_from {
14     my $string = shift;
15     my @lines = split /\n/ => $string;
16     return \@lines;
17 }
18
19 # we slurp __DATA__ and then reset it so we don't have to duplicate our TAP
20 my $offset = tell DATA;
21 my $tap = do { local $/; <DATA> };
22 seek DATA, $offset, 0;
23
24 my $did_setup    = 0;
25 my $did_teardown = 0;
26
27 my $setup    = sub { $did_setup++ };
28 my $teardown = sub { $did_teardown++ };
29
30 package NoForkProcess;
31 use vars qw( @ISA );
32 @ISA = qw( TAP::Parser::Iterator::Process );
33
34 sub _use_open3 {return}
35
36 package main;
37
38 my @schedule = (
39     {   name     => 'Process',
40         subclass => 'TAP::Parser::Iterator::Process',
41         source   => {
42             command => [
43                 $^X,
44                 File::Spec->catfile(
45                     ( $ENV{PERL_CORE} ? 'lib' : 't' ),
46                     'sample-tests', 'out_err_mix'
47                 )
48             ],
49             merge    => 1,
50             setup    => $setup,
51             teardown => $teardown,
52         },
53         after => sub {
54             is $did_setup,    1, "setup called";
55             is $did_teardown, 1, "teardown called";
56         },
57         need_open3 => 15,
58     },
59     {   name     => 'Array',
60         subclass => 'TAP::Parser::Iterator::Array',
61         source   => array_ref_from($tap),
62     },
63     {   name     => 'Stream',
64         subclass => 'TAP::Parser::Iterator::Stream',
65         source   => \*DATA,
66     },
67     {   name     => 'Process (Perl -e)',
68         subclass => 'TAP::Parser::Iterator::Process',
69         source =>
70           { command => [ $^X, '-e', 'print qq/one\ntwo\n\nthree\n/' ] },
71     },
72     {   name     => 'Process (NoFork)',
73         subclass => 'TAP::Parser::Iterator::Process',
74         class    => 'NoForkProcess',
75         source =>
76           { command => [ $^X, '-e', 'print qq/one\ntwo\n\nthree\n/' ] },
77     },
78 );
79
80 sub _can_open3 {
81     return $^O eq 'MSWin32' || $Config{d_fork};
82 }
83
84 my $factory = TAP::Parser::IteratorFactory->new;
85 for my $test (@schedule) {
86     SKIP: {
87         my $name       = $test->{name};
88         my $need_open3 = $test->{need_open3};
89         skip "No open3", $need_open3 if $need_open3 && !_can_open3();
90         my $subclass = $test->{subclass};
91         my $source   = $test->{source};
92         my $class    = $test->{class};
93         my $iter = $class ? $class->new($source)
94                           : $factory->make_iterator($source);
95         ok $iter, "$name: We should be able to create a new iterator";
96         isa_ok $iter, 'TAP::Parser::Iterator',
97           '... and the object it returns';
98         isa_ok $iter, $subclass, '... and the object it returns';
99
100         can_ok $iter, 'exit';
101         ok !defined $iter->exit,
102           "$name: ... and it should be undef before we are done ($subclass)";
103
104         can_ok $iter, 'next';
105         is $iter->next, 'one', "$name: next() should return the first result";
106
107         is $iter->next, 'two',
108           "$name: next() should return the second result";
109
110         is $iter->next, '', "$name: next() should return the third result";
111
112         is $iter->next, 'three',
113           "$name: next() should return the fourth result";
114
115         ok !defined $iter->next,
116           "$name: next() should return undef after it is empty";
117
118         is $iter->exit, 0,
119           "$name: ... and exit should now return 0 ($subclass)";
120
121         is $iter->wait, 0, "$name: wait should also now return 0 ($subclass)";
122
123         if ( my $after = $test->{after} ) {
124             $after->();
125         }
126     }
127 }
128
129 {
130
131     # coverage tests for the ctor
132
133     my $stream = $factory->make_iterator( IO::Handle->new );
134
135     isa_ok $stream, 'TAP::Parser::Iterator::Stream';
136
137     my @die;
138
139     eval {
140         local $SIG{__DIE__} = sub { push @die, @_ };
141
142         $factory->make_iterator( \1 );    # a ref to a scalar
143     };
144
145     is @die, 1, 'coverage of error case';
146
147     like pop @die, qr/Can't iterate with a SCALAR/,
148       '...and we died as expected';
149 }
150
151 {
152
153     # coverage test for VMS case
154
155     my $stream = $factory->make_iterator(
156         [   'not ',
157             'ok 1 - I hate VMS',
158         ]
159     );
160
161     is $stream->next, 'not ok 1 - I hate VMS',
162       'coverage of VMS line-splitting case';
163
164     # coverage test for VMS case - nothing after 'not'
165
166     $stream = $factory->make_iterator(
167         [   'not ',
168         ]
169     );
170
171     is $stream->next, 'not ', '...and we find "not" by itself';
172 }
173
174 SKIP: {
175     skip "No open3", 4 unless _can_open3();
176
177     # coverage testing for TAP::Parser::Iterator::Process ctor
178
179     my @die;
180
181     eval {
182         local $SIG{__DIE__} = sub { push @die, @_ };
183
184         $factory->make_iterator( {} );
185     };
186
187     is @die, 1, 'coverage testing for TPI::Process';
188
189     like pop @die, qr/Must supply a command to execute/,
190       '...and we died as expected';
191
192     my $parser = $factory->make_iterator(
193         {   command => [
194                 $^X,
195                 File::Spec->catfile( 't', 'sample-tests', 'out_err_mix' )
196             ],
197             merge => 1,
198         }
199     );
200
201     is $parser->{err}, '',    'confirm we set err to empty string';
202     is $parser->{sel}, undef, '...and selector to undef';
203
204     # And then we read from the parser to sidestep the Mac OS / open3
205     # bug which frequently throws an error here otherwise.
206     $parser->next;
207 }
208 __DATA__
209 one
210 two
211
212 three