File: 06_web_query.t

package info (click to toggle)
libhtml-treebuilder-libxml-perl 0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 533; makefile: 2; sh: 1
file content (62 lines) | stat: -rw-r--r-- 1,950 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use strict;
use warnings;
use Test::More;
use HTML::TreeBuilder::LibXML;

plan skip_all => "this test requires Web::Query" unless eval "use Web::Query; 1";
plan tests => 6;

can_ok 'HTML::TreeBuilder::LibXML', 'parse_content';
can_ok 'HTML::TreeBuilder::LibXML', 'elementify';

my $html = <<'...';
<html>
    <body>
        <div id="userbox">
            <div id="user">
                <a href="https://github.com/olegwtf"><img height="20" src="gravatar-140.png" width="20" /></a>
                <a href="https://github.com/olegwtf" class="name">olegwtf</a>
            </div>

            <ul id="user-links">
                <li>
                    <a href="/inbox/notifications" id="notifications" class="tooltipped downwards">
                        <span class="notifications"></span>
                    </a>
                </li>
                <li>
                    <a href="/settings/profile" id="settings" class="tooltipped downwards" title="Account Settings">
                        <span class="account-settings"></span>
                    </a>
                </li>
                <li>
                    <a href="/logout" data-method="post" id="logout" class="tooltipped downwards" title="Log Out">
                        <span class="logout">Log Out</span>
                    </a>
                </li>
            </ul>
        </div>
    </body>
</html>
...

sub run {
    my @classes;
    
    my $text = wq($html)->find("ul#user-links li")->each(sub {
        push @classes, $_->find("a span")->attr("class");
    })->end->find(".logout")->text;
    
    return (\@classes, $text);
}

isa_ok(HTML::TreeBuilder::XPath->new, 'HTML::TreeBuilder::XPath');
my ($their_classes, $their_text) = &run;

HTML::TreeBuilder::LibXML->replace_original();

isa_ok(HTML::TreeBuilder::XPath->new, 'HTML::TreeBuilder::LibXML');
my ($our_classes, $our_text) = &run;

is($our_text, $their_text);
is_deeply($our_classes, $their_classes);