6 use Test::More tests => 76;
10 use TAP::Parser::Iterator;
15 my @lines = split /\n/ => $string;
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;
27 my $setup = sub { $did_setup++ };
28 my $teardown = sub { $did_teardown++ };
30 package NoForkProcess;
32 @ISA = qw( TAP::Parser::Iterator::Process );
34 sub _use_open3 {return}
40 subclass => 'TAP::Parser::Iterator::Process',
44 File::Spec->catfile( ($ENV{PERL_CORE} ? 'lib' : 't'),
45 'sample-tests', 'out_err_mix' )
49 teardown => $teardown,
52 is $did_setup, 1, "setup called";
53 is $did_teardown, 1, "teardown called";
58 subclass => 'TAP::Parser::Iterator::Array',
59 source => array_ref_from($tap),
62 subclass => 'TAP::Parser::Iterator::Stream',
65 { name => 'Process (Perl -e)',
66 subclass => 'TAP::Parser::Iterator::Process',
68 { command => [ $^X, '-e', 'print qq/one\ntwo\n\nthree\n/' ] },
70 { name => 'Process (NoFork)',
71 subclass => 'TAP::Parser::Iterator::Process',
72 class => 'NoForkProcess',
74 { command => [ $^X, '-e', 'print qq/one\ntwo\n\nthree\n/' ] },
79 return $^O eq 'MSWin32' || $Config{d_fork};
82 for my $test (@schedule) {
84 my $name = $test->{name};
85 my $need_open3 = $test->{need_open3};
86 skip "No open3", $need_open3 if $need_open3 && !_can_open3();
87 my $subclass = $test->{subclass};
88 my $source = $test->{source};
89 my $class = $test->{class} || 'TAP::Parser::Iterator';
90 ok my $iter = $class->new($source),
91 "$name: We should be able to create a new iterator";
92 isa_ok $iter, 'TAP::Parser::Iterator',
93 '... and the object it returns';
94 isa_ok $iter, $subclass, '... and the object it returns';
97 ok !defined $iter->exit,
98 "$name: ... and it should be undef before we are done ($subclass)";
100 can_ok $iter, 'next';
101 is $iter->next, 'one', "$name: next() should return the first result";
103 is $iter->next, 'two',
104 "$name: next() should return the second result";
106 is $iter->next, '', "$name: next() should return the third result";
108 is $iter->next, 'three',
109 "$name: next() should return the fourth result";
111 ok !defined $iter->next,
112 "$name: next() should return undef after it is empty";
115 "$name: ... and exit should now return 0 ($subclass)";
117 is $iter->wait, 0, "$name: wait should also now return 0 ($subclass)";
119 if ( my $after = $test->{after} ) {
127 # coverage tests for the ctor
129 my $stream = TAP::Parser::Iterator->new( IO::Handle->new );
131 isa_ok $stream, 'TAP::Parser::Iterator::Stream';
136 local $SIG{__DIE__} = sub { push @die, @_ };
138 TAP::Parser::Iterator->new( \1 ); # a ref to a scalar
141 is @die, 1, 'coverage of error case';
143 like pop @die, qr/Can't iterate with a SCALAR/,
144 '...and we died as expected';
149 # coverage test for VMS case
151 my $stream = TAP::Parser::Iterator->new(
157 is $stream->next, 'not ok 1 - I hate VMS',
158 'coverage of VMS line-splitting case';
160 # coverage test for VMS case - nothing after 'not'
162 $stream = TAP::Parser::Iterator->new(
167 is $stream->next, 'not ', '...and we find "not" by itself';
171 skip "No open3", 4 unless _can_open3();
173 # coverage testing for TAP::Parser::Iterator::Process ctor
178 local $SIG{__DIE__} = sub { push @die, @_ };
180 TAP::Parser::Iterator->new( {} );
183 is @die, 1, 'coverage testing for TPI::Process';
185 like pop @die, qr/Must supply a command to execute/,
186 '...and we died as expected';
188 my $parser = TAP::Parser::Iterator->new(
191 File::Spec->catfile( 't', 'sample-tests', 'out_err_mix' )
197 is $parser->{err}, '', 'confirm we set err to empty string';
198 is $parser->{sel}, undef, '...and selector to undef';
200 # And then we read from the parser to sidestep the Mac OS / open3
201 # bug which frequently throws an error here otherwise.