1#!/usr/bin/perl -wT
2
3use strict;
4use warnings;
5use lib 't/lib';
6
7use Test::More tests => 10;
8
9use TAP::Parser;
10use TAP::Parser::Iterator::Array;
11
12my $tap = <<'END_TAP';
131..5
14ok 1 - input file opened
15... this is junk
16not ok first line of the input valid # todo some data
17# this is a comment
18ok 3 - read the rest of the file
19not ok 4 - this is a real failure
20ok 5 # skip we have no description
21END_TAP
22
23my @tests;
24my $plan_output;
25my $todo      = 0;
26my $skip      = 0;
27my %callbacks = (
28    test => sub {
29        my $test = shift;
30        push @tests => $test;
31        $todo++ if $test->has_todo;
32        $skip++ if $test->has_skip;
33    },
34    plan => sub {
35        my $plan = shift;
36        $plan_output = $plan->as_string;
37    }
38);
39
40my $iterator = TAP::Parser::Iterator::Array->new( [ split /\n/ => $tap ] );
41my $parser = TAP::Parser->new(
42    {   iterator  => $iterator,
43        callbacks => \%callbacks,
44    }
45);
46
47can_ok $parser, 'run';
48$parser->run;
49is $plan_output, '1..5', 'Plan callbacks should succeed';
50is scalar @tests, $parser->tests_run, '... as should the test callbacks';
51
52@tests       = ();
53$plan_output = '';
54$todo        = 0;
55$skip        = 0;
56my $else = 0;
57my $all  = 0;
58my $end  = 0;
59%callbacks = (
60    test => sub {
61        my $test = shift;
62        push @tests => $test;
63        $todo++ if $test->has_todo;
64        $skip++ if $test->has_skip;
65    },
66    plan => sub {
67        my $plan = shift;
68        $plan_output = $plan->as_string;
69    },
70    EOF => sub {
71        my $p = shift;
72        $end = 1 if $all == 8 and $p->isa('TAP::Parser');
73    },
74    ELSE => sub {
75        $else++;
76    },
77    ALL => sub {
78        $all++;
79    },
80);
81
82$iterator = TAP::Parser::Iterator::Array->new( [ split /\n/ => $tap ] );
83$parser = TAP::Parser->new(
84    {   iterator  => $iterator,
85        callbacks => \%callbacks,
86    }
87);
88
89can_ok $parser, 'run';
90$parser->run;
91is $plan_output, '1..5', 'Plan callbacks should succeed';
92is scalar @tests, $parser->tests_run, '... as should the test callbacks';
93is $else, 2, '... and the correct number of "ELSE" lines should be seen';
94is $all,  8, '... and the correct total number of lines should be seen';
95is $end,  1, 'EOF callback correctly called';
96
97# Check callback name policing
98
99%callbacks = (
100    sometest => sub { },
101    plan     => sub { },
102    random   => sub { },
103    ALL      => sub { },
104    ELSES    => sub { },
105);
106
107$iterator = TAP::Parser::Iterator::Array->new( [ split /\n/ => $tap ] );
108eval {
109    $parser = TAP::Parser->new(
110        {   iterator  => $iterator,
111            callbacks => \%callbacks,
112        }
113    );
114};
115
116like $@, qr/Callback/, 'Bad callback keys faulted';
117