callbacks.t revision 1.1.1.1
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::IteratorFactory;
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 $factory = TAP::Parser::IteratorFactory->new;
40my $stream  = $factory->make_iterator( [ split /\n/ => $tap ] );
41my $parser  = TAP::Parser->new(
42    {   stream    => $stream,
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$stream = $factory->make_iterator( [ split /\n/ => $tap ] );
83$parser = TAP::Parser->new(
84    {   stream    => $stream,
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$stream = $factory->make_iterator( [ split /\n/ => $tap ] );
108eval {
109    $parser = TAP::Parser->new(
110        {   stream    => $stream,
111            callbacks => \%callbacks,
112        }
113    );
114};
115
116like $@, qr/Callback/, 'Bad callback keys faulted';
117