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