Move Test::Harness from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / cpan / Test-Harness / t / multiplexer.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 't/lib';
5
6 use Test::More qw( no_plan );
7
8 use File::Spec;
9 use TAP::Parser;
10 use TAP::Parser::Multiplexer;
11 use TAP::Parser::Iterator::Process;
12
13 my $fork_desc
14   = TAP::Parser::Iterator::Process->_use_open3
15   ? 'fork'
16   : 'nofork';
17
18 my @schedule = (
19     {   name => 'Single non-selectable source',
20
21         # Returns a list of parser, stash pairs. The stash contains the
22         # TAP that we expect from this parser.
23         sources => sub {
24             my @tap = (
25                 '1..1',
26                 'ok 1 Just fine'
27             );
28
29             return [
30                 TAP::Parser->new( { tap => join( "\n", @tap ) . "\n" } ),
31                 \@tap,
32             ];
33         },
34     },
35     {   name    => 'Two non-selectable sources',
36         sources => sub {
37             my @tap = (
38                 [   '1..1',
39                     'ok 1 Just fine'
40                 ],
41                 [   '1..2',
42                     'not ok 1 Oh dear',
43                     'ok 2 Better'
44                 ]
45             );
46
47             return map {
48                 [   TAP::Parser->new( { tap => join( "\n", @$_ ) . "\n" } ),
49                     $_
50                 ]
51             } @tap;
52         },
53     },
54     {   name    => 'Single selectable source',
55         sources => sub {
56             return [
57                 TAP::Parser->new(
58                     {   source => File::Spec->catfile(
59                             't',
60                             'sample-tests',
61                             'simple'
62                         ),
63                     }
64                 ),
65                 [   '1..5',
66                     'ok 1',
67                     'ok 2',
68                     'ok 3',
69                     'ok 4',
70                     'ok 5',
71                 ]
72             ];
73         },
74     },
75     {   name    => 'Three selectable sources',
76         sources => sub {
77             return map {
78                 [   TAP::Parser->new(
79                         {   source => File::Spec->catfile(
80                                 't',
81                                 'sample-tests',
82                                 'simple'
83                             ),
84                         }
85                     ),
86                     [   '1..5',
87                         'ok 1',
88                         'ok 2',
89                         'ok 3',
90                         'ok 4',
91                         'ok 5',
92                     ]
93                 ]
94             } 1 .. 3;
95         },
96     },
97     {   name    => 'Three selectable sources, two non-selectable sources',
98         sources => sub {
99             my @tap = (
100                 [   '1..1',
101                     'ok 1 Just fine'
102                 ],
103                 [   '1..2',
104                     'not ok 1 Oh dear',
105                     'ok 2 Better'
106                 ]
107             );
108
109             return (
110                 map {
111                     [   TAP::Parser->new(
112                             { tap => join( "\n", @$_ ) . "\n" }
113                         ),
114                         $_
115                     ]
116                   } @tap
117               ),
118               ( map {
119                     [   TAP::Parser->new(
120                             {   source => File::Spec->catfile(
121                                     't',
122                                     'sample-tests',
123                                     'simple'
124                                 ),
125                             }
126                         ),
127                         [   '1..5',
128                             'ok 1',
129                             'ok 2',
130                             'ok 3',
131                             'ok 4',
132                             'ok 5',
133                         ]
134                     ]
135                   } 1 .. 3
136               );
137         },
138     }
139 );
140
141 for my $test (@schedule) {
142     my $name    = "$test->{name} ($fork_desc)";
143     my @sources = $test->{sources}->();
144     my $mux     = TAP::Parser::Multiplexer->new;
145
146     my $count = @sources;
147     $mux->add(@$_) for @sources;
148
149     is $mux->parsers, $count, "$name: count OK";
150
151     while ( my ( $parser, $stash, $result ) = $mux->next ) {
152
153         # use Data::Dumper;
154         # diag Dumper( { stash => $stash, result => $result } );
155         if ( defined $result ) {
156             my $expect = ( shift @$stash ) || ' OOPS ';
157             my $got = $result->raw;
158             is $got, $expect, "$name: '$expect' OK";
159         }
160         else {
161             ok @$stash == 0, "$name: EOF OK";
162
163             # Make sure we only get one EOF per stream
164             push @$stash, ' expect no more ';
165         }
166     }
167     is $mux->parsers, 0, "$name: All used up";
168 }
169
170 1;