/
css_selector.t
98 lines (81 loc) · 2.92 KB
/
css_selector.t
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl -w
use strict;
use Test::Builder::Tester;
use Test::More;
use File::Spec;
BEGIN {
eval 'require HTML::Selector::XPath';
plan skip_all => 'Install HTML::Selector::XPath to use CSS selectors'
if $@;
plan tests => 16;
}
BEGIN { use_ok 'Test::XPath' or die; }
my $html = '<html><head><title>Hello</title><body><p class="foo"><em><b>first</b></em></p><p><em><b>post</b></em></p></body></html>';
ok my $xp = Test::XPath->new(
xml => $html,
is_html => 1,
filter => 'css_selector',
), 'Create Test::XPath object with CSS selector support';
# Try successful ok.
test_out( 'ok 1 - whatever');
$xp->ok('> html > head > title', 'whatever');
test_test('ok works');
# Try failed ok.
my $file = File::Spec->catfile(split m{/} => __FILE__);
test_out('not ok 1 - whatever');
test_err(qq{# Failed test 'whatever'\n# at $file line 34.});
$xp->ok('> html > head > foo', 'whatever');
test_test('ok fail works');
# Try a recursive call.
test_out( 'ok 1 - p');
test_out( 'ok 2 - em');
test_out( 'ok 3 - b');
test_out( 'ok 4 - em');
test_out( 'ok 5 - b');
$xp->ok( '> html > body > p', sub {
shift->ok('> em', sub {
$_->ok('> b', 'b');
}, 'em');
}, 'p');
test_test('recursive ok should work');
# Try is, like, and cmp_ok.
$xp->is( ' > html > head > title', 'Hello', 'is should work');
$xp->isnt( ' > html > head > title', 'Bye', 'isnt should work');
$xp->like( ' > html > head > title', qr{^Hel{2}o$}, 'like should work');
$xp->unlike( ' > html > head > title', qr{^Bye$}, 'unlike should work');
$xp->cmp_ok(' > html > head > title', 'eq', 'Hello', 'cmp_ok should work');
# Make them fail.
test_out('not ok 1 - is should work');
test_out('not ok 2 - isnt should work');
test_out('not ok 3 - like should work');
test_out('not ok 4 - unlike should work');
test_out('not ok 5 - cmp_ok should work');
$xp->is( ' > html > head > title', 'Bye', 'is should work');
$xp->isnt( ' > html > head > title', 'Hello', 'isnt should work');
$xp->like( ' > html > head > title', qr{^Bye$}, 'like should work');
$xp->unlike( ' > html > head > title', qr{^Hel{2}o$}, 'unlike should work');
$xp->cmp_ok(' > html > head > title', 'ne', 'Hello', 'cmp_ok should work');
test_test(
skip_err => 1,
title => 'Failures in the simple methods should work',
);
# Try multiples.
$xp->is(' > html > body > p', 'firstpost', 'Should work for multiples');
# Try an attribute.
$xp->ok(' > html > body > p[class="foo"]', 'Should find by attribute value');
# Try a non-existent node.
test_out('not ok 1');
$xp->ok(' > foo > baz');
test_test(
skip_err => 1,
title => 'Nonexistent node should be false in ok()',
);
# Try successful ok.
test_out( 'ok 1 - whatever');
$xp->not_ok(' > html > head > foo', 'whatever');
test_test('not_ok works');
# Try failed ok.
test_out('not ok 1 - whatever');
test_err(qq{# Failed test 'whatever'\n# at $file line 97.});
$xp->not_ok(' > html > head > title', 'whatever');
test_test('not_ok fail works');