6 use Test::More tests => 76;
10 use TAP::Parser::IteratorFactory;
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',
46 ? ( File::Spec->updir(), 'ext', 'Test', 'Harness' )
56 teardown => $teardown,
59 is $did_setup, 1, "setup called";
60 is $did_teardown, 1, "teardown called";
65 subclass => 'TAP::Parser::Iterator::Array',
66 source => array_ref_from($tap),
69 subclass => 'TAP::Parser::Iterator::Stream',
72 { name => 'Process (Perl -e)',
73 subclass => 'TAP::Parser::Iterator::Process',
75 { command => [ $^X, '-e', 'print qq/one\ntwo\n\nthree\n/' ] },
77 { name => 'Process (NoFork)',
78 subclass => 'TAP::Parser::Iterator::Process',
79 class => 'NoForkProcess',
81 { command => [ $^X, '-e', 'print qq/one\ntwo\n\nthree\n/' ] },
86 return $^O eq 'MSWin32' || $Config{d_fork};
89 my $factory = TAP::Parser::IteratorFactory->new;
90 for my $test (@schedule) {
92 my $name = $test->{name};
93 my $need_open3 = $test->{need_open3};
94 skip "No open3", $need_open3 if $need_open3 && !_can_open3();
95 my $subclass = $test->{subclass};
96 my $source = $test->{source};
97 my $class = $test->{class};
100 ? $class->new($source)
101 : $factory->make_iterator($source);
102 ok $iter, "$name: We should be able to create a new iterator";
103 isa_ok $iter, 'TAP::Parser::Iterator',
104 '... and the object it returns';
105 isa_ok $iter, $subclass, '... and the object it returns';
107 can_ok $iter, 'exit';
108 ok !defined $iter->exit,
109 "$name: ... and it should be undef before we are done ($subclass)";
111 can_ok $iter, 'next';
112 is $iter->next, 'one', "$name: next() should return the first result";
114 is $iter->next, 'two',
115 "$name: next() should return the second result";
117 is $iter->next, '', "$name: next() should return the third result";
119 is $iter->next, 'three',
120 "$name: next() should return the fourth result";
122 ok !defined $iter->next,
123 "$name: next() should return undef after it is empty";
126 "$name: ... and exit should now return 0 ($subclass)";
128 is $iter->wait, 0, "$name: wait should also now return 0 ($subclass)";
130 if ( my $after = $test->{after} ) {
138 # coverage tests for the ctor
140 my $stream = $factory->make_iterator( IO::Handle->new );
142 isa_ok $stream, 'TAP::Parser::Iterator::Stream';
147 local $SIG{__DIE__} = sub { push @die, @_ };
149 $factory->make_iterator( \1 ); # a ref to a scalar
152 is @die, 1, 'coverage of error case';
154 like pop @die, qr/Can't iterate with a SCALAR/,
155 '...and we died as expected';
160 # coverage test for VMS case
162 my $stream = $factory->make_iterator(
168 is $stream->next, 'not ok 1 - I hate VMS',
169 'coverage of VMS line-splitting case';
171 # coverage test for VMS case - nothing after 'not'
173 $stream = $factory->make_iterator(
178 is $stream->next, 'not ', '...and we find "not" by itself';
182 skip "No open3", 4 unless _can_open3();
184 # coverage testing for TAP::Parser::Iterator::Process ctor
189 local $SIG{__DIE__} = sub { push @die, @_ };
191 $factory->make_iterator( {} );
194 is @die, 1, 'coverage testing for TPI::Process';
196 like pop @die, qr/Must supply a command to execute/,
197 '...and we died as expected';
199 my $parser = $factory->make_iterator(
202 File::Spec->catfile( 't', 'sample-tests', 'out_err_mix' )
208 is $parser->{err}, '', 'confirm we set err to empty string';
209 is $parser->{sel}, undef, '...and selector to undef';
211 # And then we read from the parser to sidestep the Mac OS / open3
212 # bug which frequently throws an error here otherwise.