Blame t/leaktest.t

Packit 16975c
#!/usr/bin/perl -T
Packit 16975c
Packit 16975c
use warnings;
Packit 16975c
use strict;
Packit 16975c
Packit 16975c
#Test that we don't leak memory
Packit 16975c
Packit 16975c
use Test::More;
Packit 16975c
Packit 16975c
my $leak_trace_loaded;
Packit 16975c
Packit 16975c
# RECOMMEND PREREQ: Test::LeakTrace
Packit 16975c
BEGIN { $leak_trace_loaded = eval "use Test::LeakTrace; 1" }
Packit 16975c
Packit 16975c
plan skip_all => "Test::LeakTrace required for testing memory leaks"
Packit 16975c
    unless $leak_trace_loaded;
Packit 16975c
Packit 16975c
plan tests => 20;
Packit 16975c
Packit 16975c
use HTML::TreeBuilder;
Packit 16975c
Packit 16975c
my $lacks_weak;
Packit 16975c
Packit 16975c
sub first_block {
Packit 16975c
    my $lol = [
Packit 16975c
        'html',
Packit 16975c
        [ 'head', [ 'title', 'I like stuff!' ], ],
Packit 16975c
        [   'body', { 'lang', 'en-JP' },
Packit 16975c
            'stuff',
Packit 16975c
            [ 'p', 'um, p < 4!', { 'class' => 'par123' } ],
Packit 16975c
            [ 'div', { foo => 'bar' }, ' 1  2  3 ' ],        # at 0.1.2
Packit 16975c
            [ 'div', { fu  => 'baa' }, " 1   2 \xA0 3 " ],    # RT #26436 test
Packit 16975c
            ['hr'],
Packit 16975c
        ]
Packit 16975c
    ];
Packit 16975c
    my $t1 = HTML::Element->new_from_lol($lol);
Packit 16975c
Packit 16975c
    ### added to test ->is_empty() and ->look_up()
Packit 16975c
    my $hr = $t1->find('hr');
Packit 16975c
    my $lookuptag = $hr->look_up( "_tag", "body" );
Packit 16975c
    my %attrs  = $lookuptag->all_attr();
Packit 16975c
    my @attrs1 = sort keys %attrs;
Packit 16975c
    my @attrs2 = sort $lookuptag->all_attr_names();
Packit 16975c
Packit 16975c
    # Test scalar context
Packit 16975c
    my $count = $t1->content_list;
Packit 16975c
Packit 16975c
    # Test list context
Packit 16975c
    my @list = $t1->content_list;
Packit 16975c
Packit 16975c
    my $div = $t1->find_by_attribute( 'foo', 'bar' );
Packit 16975c
Packit 16975c
    my $div2 = $t1->find_by_attribute( 'fu', 'baa' );
Packit 16975c
Packit 16975c
    my $t2 = HTML::Element->new_from_lol($lol);
Packit 16975c
    $t2->address('0.1.2')->attr( 'snap', 123 );
Packit 16975c
Packit 16975c
    my $body = $t1->find_by_tag_name('body');
Packit 16975c
Packit 16975c
    my $cl = join '~', $body->content_list;
Packit 16975c
    my @detached = $body->detach_content;
Packit 16975c
    $body->push_content(@detached);
Packit 16975c
Packit 16975c
    $t2->delete if $lacks_weak;
Packit 16975c
    $t1->delete if $lacks_weak;
Packit 16975c
} # end first_block
Packit 16975c
Packit 16975c
sub second_block {
Packit 16975c
    # for normalization
Packit 16975c
    my $t1 = HTML::Element->new_from_lol( [ 'p', 'stuff', ['hr'], 'thing' ] );
Packit 16975c
    my @start = $t1->content_list;
Packit 16975c
    my $lr = $t1->content;
Packit 16975c
Packit 16975c
    # insert some undefs
Packit 16975c
    splice @$lr, 1, 0, undef;    # insert an undef between [0] and [1]
Packit 16975c
    push @$lr, undef;            # append an undef to the end
Packit 16975c
    unshift @$lr, undef;         # prepend an undef to the front
Packit 16975c
         # $lr is [undef, 'stuff', undef, H::E('hr'), 'thing', undef]
Packit 16975c
Packit 16975c
    {
Packit 16975c
        my $cl_count = $t1->content_list;
Packit 16975c
        my @cl       = $t1->content_list;
Packit 16975c
    }
Packit 16975c
Packit 16975c
    {
Packit 16975c
        $t1->normalize_content;
Packit 16975c
        my @cl = $t1->content_list;
Packit 16975c
    }
Packit 16975c
Packit 16975c
    $t1->attr( 'foo', 'bar' );
Packit 16975c
    $t1->attr( 'foo', '' );
Packit 16975c
    $t1->attr( 'foo', undef );    # should delete it
Packit 16975c
    $t1->delete if $lacks_weak;
Packit 16975c
} # end second_block
Packit 16975c
Packit 16975c
sub empty_tree {
Packit 16975c
    my $root = HTML::TreeBuilder->new();
Packit 16975c
    $root->implicit_body_p_tag(1);
Packit 16975c
    $root->xml_mode(1);
Packit 16975c
    $root->parse('');
Packit 16975c
    $root->eof();
Packit 16975c
    $root->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
sub br_only {
Packit 16975c
    my $root = HTML::TreeBuilder->new();
Packit 16975c
    $root->implicit_body_p_tag(1);
Packit 16975c
    $root->xml_mode(1);
Packit 16975c
    $root->parse('
');
Packit 16975c
    $root->eof();
Packit 16975c
    $root->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
sub text_only {
Packit 16975c
    my $root = HTML::TreeBuilder->new();
Packit 16975c
    $root->implicit_body_p_tag(1);
Packit 16975c
    $root->xml_mode(1);
Packit 16975c
    $root->parse('text');
Packit 16975c
    $root->eof();
Packit 16975c
    $root->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
sub empty_table {
Packit 16975c
    my $root = HTML::TreeBuilder->new();
Packit 16975c
    $root->implicit_body_p_tag(1);
Packit 16975c
    $root->xml_mode(1);
Packit 16975c
    $root->parse('
');
Packit 16975c
    $root->eof();
Packit 16975c
    $root->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
sub escapes {
Packit 16975c
    my $root   = HTML::TreeBuilder->new();
Packit 16975c
    my $escape = 'This ſoftware has ſome bugs';
Packit 16975c
    my $html   = $root->parse($escape)->eof->elementify();
Packit 16975c
    $html->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
sub other_languages {
Packit 16975c
    my $root   = HTML::TreeBuilder->new();
Packit 16975c
    my $escape = 'Gebühr vor Ort von € 30,- pro Woche';   # RT 14212
Packit 16975c
    my $html   = $root->parse($escape)->eof;
Packit 16975c
    $html->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
sub rt_18570 {
Packit 16975c
    my $root   = HTML::TreeBuilder->new();
Packit 16975c
    my $escape = 'This ∼ is a twiddle';
Packit 16975c
    my $html   = $root->parse($escape)->eof->elementify();
Packit 16975c
    $html->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
sub rt_18571 {
Packit 16975c
    my $root = HTML::TreeBuilder->new();
Packit 16975c
    my $html = $root->parse('$self->escape')->eof->elementify();
Packit 16975c
    $html->delete if $lacks_weak;
Packit 16975c
}
Packit 16975c
Packit 16975c
# Try with weak refs, if available:
Packit 16975c
SKIP: {
Packit 16975c
    skip('Scalar::Util lacks support for weak references', 10)
Packit 16975c
        unless HTML::Element->Use_Weak_Refs;
Packit 16975c
Packit 16975c
    no_leaks_ok(\&first_block, 'first block has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&second_block, 'second block has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&empty_tree, 'empty_tree has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&br_only, 'br_only has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&text_only, 'text_only has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&empty_table, 'empty_table has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&escapes, 'escapes has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&other_languages, 'other_languages has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&rt_18570, 'rt_18570 has no leaks with weak refs');
Packit 16975c
    no_leaks_ok(\&rt_18571, 'rt_18571 has no leaks with weak refs');
Packit 16975c
}
Packit 16975c
Packit 16975c
# Try again without weak refs:
Packit 16975c
$lacks_weak = 1;
Packit 16975c
HTML::Element->Use_Weak_Refs(0);
Packit 16975c
Packit 16975c
no_leaks_ok(\&first_block, 'first block has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&second_block, 'second block has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&empty_tree, 'empty_tree has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&br_only, 'br_only has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&text_only, 'text_only has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&empty_table, 'empty_table has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&escapes, 'escapes has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&other_languages, 'other_languages has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&rt_18570, 'rt_18570 has no leaks without weak refs');
Packit 16975c
no_leaks_ok(\&rt_18571, 'rt_18571 has no leaks without weak refs');