b23762102c83dbc0fdec303c1b1cc3589c4459be
[p5sagit/p5-mst-13.2.git] / lib / Test / Harness / t / callbacks.t
1 #!/usr/bin/perl -wT
2
3 use strict;
4 use lib 't/lib';
5
6 use Test::More tests => 10;
7
8 use TAP::Parser;
9 use TAP::Parser::Iterator;
10
11 my $tap = <<'END_TAP';
12 1..5
13 ok 1 - input file opened
14 ... this is junk
15 not ok first line of the input valid # todo some data
16 # this is a comment
17 ok 3 - read the rest of the file
18 not ok 4 - this is a real failure
19 ok 5 # skip we have no description
20 END_TAP
21
22 my @tests;
23 my $plan_output;
24 my $todo      = 0;
25 my $skip      = 0;
26 my %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
39 my $stream = TAP::Parser::Iterator->new( [ split /\n/ => $tap ] );
40 my $parser = TAP::Parser->new(
41     {   stream    => $stream,
42         callbacks => \%callbacks,
43     }
44 );
45
46 can_ok $parser, 'run';
47 $parser->run;
48 is $plan_output, '1..5', 'Plan callbacks should succeed';
49 is scalar @tests, $parser->tests_run, '... as should the test callbacks';
50
51 @tests       = ();
52 $plan_output = '';
53 $todo        = 0;
54 $skip        = 0;
55 my $else = 0;
56 my $all  = 0;
57 my $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         $end = 1 if $all == 8;
71     },
72     ELSE => sub {
73         $else++;
74     },
75     ALL => sub {
76         $all++;
77     },
78 );
79
80 $stream = TAP::Parser::Iterator->new( [ split /\n/ => $tap ] );
81 $parser = TAP::Parser->new(
82     {   stream    => $stream,
83         callbacks => \%callbacks,
84     }
85 );
86
87 can_ok $parser, 'run';
88 $parser->run;
89 is $plan_output, '1..5', 'Plan callbacks should succeed';
90 is scalar @tests, $parser->tests_run, '... as should the test callbacks';
91 is $else, 2, '... and the correct number of "ELSE" lines should be seen';
92 is $all,  8, '... and the correct total number of lines should be seen';
93 is $end,  1, 'EOF callback correctly called';
94
95 # Check callback name policing
96
97 %callbacks = (
98     sometest => sub { },
99     plan     => sub { },
100     random   => sub { },
101     ALL      => sub { },
102     ELSES    => sub { },
103 );
104
105 $stream = TAP::Parser::Iterator->new( [ split /\n/ => $tap ] );
106 eval {
107     $parser = TAP::Parser->new(
108         {   stream    => $stream,
109             callbacks => \%callbacks,
110         }
111     );
112 };
113
114 like $@, qr/Callback/, 'Bad callback keys faulted';