Debian lenny version packages
[pkg-perl] / deb-src / libhtml-parser-perl / libhtml-parser-perl-3.56 / t / handler-eof.t
1 use Test::More tests => 6;
2
3 use strict;
4 use HTML::Parser ();
5
6 my $p = HTML::Parser->new(api_version => 3);
7
8 $p->handler(start => sub { my $attr = shift; is($attr->{testno}, 1) },
9                      "attr");
10 $p->handler(end => sub { shift->eof }, "self");
11 my $text;
12 $p->handler(text => sub { $text = shift }, "text");
13
14 is($p->parse("<foo testno=1>"), $p);
15
16 $text = '';
17 ok(!$p->parse("</foo><foo testno=999>"));
18 ok(!$text);
19
20 $p->handler(end => sub { $p->parse("foo"); }, "");
21 eval {
22     $p->parse("</foo>");
23 };
24 like($@,  qr/Parse loop not allowed/);
25
26 # We used to get into an infinite loop if the eof triggered
27 # handler called ->eof
28
29 use HTML::Parser;
30 $p = HTML::Parser->new(api_version => 3);
31
32 my $i;
33 $p->handler("default" =>
34             sub {
35                 my $p=shift;
36                 #++$i; diag "$i @_";
37                 $p->eof;
38             }, "self, event");
39 $p->parse("Foo");
40 $p->eof;
41
42 # We used to sometimes trigger events after a handler signaled eof
43 my $title='';
44 $p = HTML::Parser->new(api_version => 3,);
45 $p->handler(start=> \&title_handler, 'tagname, self');
46 $p->parse("<head><title>foo</title>\n</head>");
47 is($title, "foo");
48
49 sub title_handler {
50     return if shift ne 'title';
51     my $self = shift; 
52     $self->handler(text => sub { $title .= shift}, 'dtext');
53     $self->handler(end => sub { shift->eof if shift eq 'title' }, 'tagname, self');
54 }