RawTAP.pm revision 1.1
1package TAP::Parser::SourceHandler::RawTAP;
2
3use strict;
4use vars qw($VERSION @ISA);
5
6use TAP::Parser::SourceHandler   ();
7use TAP::Parser::IteratorFactory ();
8use TAP::Parser::Iterator::Array ();
9
10@ISA = qw(TAP::Parser::SourceHandler);
11
12TAP::Parser::IteratorFactory->register_handler(__PACKAGE__);
13
14=head1 NAME
15
16TAP::Parser::SourceHandler::RawTAP - Stream output from raw TAP in a scalar/array ref.
17
18=head1 VERSION
19
20Version 3.23
21
22=cut
23
24$VERSION = '3.23';
25
26=head1 SYNOPSIS
27
28  use TAP::Parser::Source;
29  use TAP::Parser::SourceHandler::RawTAP;
30
31  my $source = TAP::Parser::Source->new->raw( \"1..1\nok 1\n" );
32  $source->assemble_meta;
33
34  my $class = 'TAP::Parser::SourceHandler::RawTAP';
35  my $vote  = $class->can_handle( $source );
36  my $iter  = $class->make_iterator( $source );
37
38=head1 DESCRIPTION
39
40This is a I<raw TAP output> L<TAP::Parser::SourceHandler> - it has 2 jobs:
41
421. Figure out if the L<TAP::Parser::Source> it's given is raw TAP output
43(L</can_handle>).
44
452. Creates an iterator for raw TAP output (L</make_iterator>).
46
47Unless you're writing a plugin or subclassing L<TAP::Parser>, you probably
48won't need to use this module directly.
49
50=head1 METHODS
51
52=head2 Class Methods
53
54=head3 C<can_handle>
55
56  my $vote = $class->can_handle( $source );
57
58Only votes if $source is an array, or a scalar with newlines.  Casts the
59following votes:
60
61  0.9  if it's a scalar with '..' in it
62  0.7  if it's a scalar with 'ok' in it
63  0.3  if it's just a scalar with newlines
64  0.5  if it's an array
65
66=cut
67
68sub can_handle {
69    my ( $class, $src ) = @_;
70    my $meta = $src->meta;
71
72    return 0 if $meta->{file};
73    if ( $meta->{is_scalar} ) {
74        return 0 unless $meta->{has_newlines};
75        return 0.9 if ${ $src->raw } =~ /\d\.\.\d/;
76        return 0.7 if ${ $src->raw } =~ /ok/;
77        return 0.3;
78    }
79    elsif ( $meta->{is_array} ) {
80        return 0.5;
81    }
82    return 0;
83}
84
85=head3 C<make_iterator>
86
87  my $iterator = $class->make_iterator( $source );
88
89Returns a new L<TAP::Parser::Iterator::Array> for the source.
90C<$source-E<gt>raw> must be an array ref, or a scalar ref.
91
92C<croak>s on error.
93
94=cut
95
96sub make_iterator {
97    my ( $class, $src ) = @_;
98    my $meta = $src->meta;
99
100    my $tap_array;
101    if ( $meta->{is_scalar} ) {
102        $tap_array = [ split "\n" => ${ $src->raw } ];
103    }
104    elsif ( $meta->{is_array} ) {
105        $tap_array = $src->raw;
106    }
107
108    $class->_croak('No raw TAP found in $source->raw')
109      unless scalar $tap_array;
110
111    return TAP::Parser::Iterator::Array->new($tap_array);
112}
113
1141;
115
116=head1 SUBCLASSING
117
118Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
119
120=head1 SEE ALSO
121
122L<TAP::Object>,
123L<TAP::Parser>,
124L<TAP::Parser::IteratorFactory>,
125L<TAP::Parser::SourceHandler>,
126L<TAP::Parser::SourceHandler::Executable>,
127L<TAP::Parser::SourceHandler::Perl>,
128L<TAP::Parser::SourceHandler::File>,
129L<TAP::Parser::SourceHandler::Handle>
130
131=cut
132