1#!/usr/bin/perl -wT
2
3use strict;
4use warnings;
5use lib 't/lib';
6
7use Test::More tests => 14;
8
9use TAP::Parser;
10use TAP::Parser::Iterator::Array;
11
12sub tap_to_lines {
13    my $string = shift;
14    my @lines = ( $string =~ /.*\n/g );
15    return \@lines;
16}
17
18my $tap = <<'END_TAP';
191..4
20ok 1 - input file opened
21... this is junk
22not ok first line of the input valid # todo some data
23# this is a comment
24ok 3 - read the rest of the file
25not ok 4 - this is a real failure
26Bail out!  We ran out of foobar.
27not ok 5
28END_TAP
29
30my $parser = TAP::Parser->new(
31    {   iterator => TAP::Parser::Iterator::Array->new( tap_to_lines($tap) ),
32    }
33);
34
35# results() is sane?
36
37# check the test plan
38my $result = $parser->next();
39
40# TEST
41ok $result->is_plan, 'We should have a plan';
42
43# a normal, passing test
44
45my $test = $parser->next();
46
47# TEST
48ok $test->is_test, '... and a test';
49
50# junk lines should be preserved
51
52my $unknown = $parser->next();
53
54# TEST
55ok $unknown->is_unknown, '... and an unknown line';
56
57# a failing test, which also happens to have a directive
58
59my $failed = $parser->next();
60
61# TEST
62ok $failed->is_test, '... and another test';
63
64# comments
65
66my $comment = $parser->next();
67
68# TEST
69ok $comment->is_comment, '... and a comment';
70
71# another normal, passing test
72
73$test = $parser->next();
74
75# TEST
76ok $test->is_test, '... and another test';
77
78# a failing test
79
80$failed = $parser->next();
81
82# TEST
83ok $failed->is_test, '... and yet another test';
84
85# ok 5 # skip we have no description
86# skipped test
87my $bailout = $parser->next();
88
89# TEST
90ok $bailout->is_bailout, 'And finally we should have a bailout';
91
92# TEST
93is $bailout->as_string, 'We ran out of foobar.',
94  '... and as_string() should return the explanation';
95
96# TEST
97is( $bailout->raw, 'Bail out!  We ran out of foobar.',
98    '... and raw() should return the explanation'
99);
100
101# TEST
102is( $bailout->explanation, 'We ran out of foobar.',
103    '... and it should have the correct explanation'
104);
105
106my $more_tap = "1..1\nok 1 - input file opened\n";
107
108my $second_parser = TAP::Parser->new(
109    {   iterator =>
110          TAP::Parser::Iterator::Array->new( [ split( /\n/, $more_tap ) ] ),
111    }
112);
113
114$result = $second_parser->next();
115
116# TEST
117ok $result->is_plan(), "Result is not the leftover line";
118
119$result = $second_parser->next();
120
121# TEST
122ok $result->is_test(), "Result is a test";
123
124# TEST
125ok $result->is_ok(), "The event has passed";
126
127