Debian Codegraph @

t/scrape_text_by_id.t

raw symbols SCIP
1 #!/usr/bin/perl -T
2
3 use strictscip-perl cpan Test-WWW-Mechanize 1.60 strict/;
4 use warningsscip-perl cpan Test-WWW-Mechanize 1.60 warnings/;
5
6 use Test::Builder::Testerscip-perl cpan Test-WWW-Mechanize 1.60 Test/Builder/Tester/;
7
8 use Test::Morescip-perl cpan Test-WWW-Mechanize 1.60 Test/More/ tests => 3;
9
10 use URI::filescip-perl cpan Test-WWW-Mechanize 1.60 URI/file/ ();
11
12 use Test::WWW::Mechanizescip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/ ();
13
14 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). scrape_text_by_id => sub {
15 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 8;
16
17 my $mech = Test::WWW::Mechanize->newscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/new().( autolint => 0 );
18 isa_okscip-perl cpan Test-WWW-Mechanize 1.60 main/isa_ok().( $mech, 'Test::WWW::Mechanize' );
19
20 my $uri = URI::file->new_absscip-perl cpan Test-WWW-Mechanize 1.60 URI/file/new_abs().( 't/goodlinks.html' )->as_stringscip-perl cpan Test-WWW-Mechanize 1.60 main/as_string().;
21 $mech->get_okscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/get_ok().( $uri, 'Get a dummy page just to have one' );
22
23 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'nothing to find' => sub {
24 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
25 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body></body></html>' );
26
27 is_deeplyscip-perl cpan Test-WWW-Mechanize 1.60 main/is_deeply().( [$mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' )], [], 'empty list returned in list context' );
28 isscip-perl cpan Test-WWW-Mechanize 1.60 main/is().( $mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' ), undef, 'undef returned in scalar context' );
29 };
30
31 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'find one' => sub {
32 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
33 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf">contents</p></body></html>' );
34 is_deeplyscip-perl cpan Test-WWW-Mechanize 1.60 main/is_deeply().( [$mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' )], ['contents'], 'list context' );
35 isscip-perl cpan Test-WWW-Mechanize 1.60 main/is().( $mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' ), 'contents', 'scalar context' );
36 };
37
38 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'find multiple' => sub {
39 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
40
41 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf">contents</p><p id="asdf">further</p></body></html>' );
42 is_deeplyscip-perl cpan Test-WWW-Mechanize 1.60 main/is_deeply().( [$mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' )], ['contents', 'further'], 'empty list returned in list context' );
43 isscip-perl cpan Test-WWW-Mechanize 1.60 main/is().( $mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' ), 'contents', 'first string returned in scalar context' );
44 };
45
46 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'present but empty' => sub {
47 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
48
49 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf"></p></body></html>' );
50 is_deeplyscip-perl cpan Test-WWW-Mechanize 1.60 main/is_deeply().( [$mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' )], [''], 'list context' );
51 isscip-perl cpan Test-WWW-Mechanize 1.60 main/is().( $mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' ), '', 'scalar context' );
52 };
53
54 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'present but emptier' => sub {
55 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
56
57 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf" /></body></html>' );
58 is_deeplyscip-perl cpan Test-WWW-Mechanize 1.60 main/is_deeply().( [$mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' )], [''], 'list context' );
59 isscip-perl cpan Test-WWW-Mechanize 1.60 main/is().( $mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' ), '', 'scalar context' );
60 };
61
62 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'nested tag' => sub {
63 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
64
65 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf">Bob and <b>Bongo!</b></p></body></html>' );
66 is_deeplyscip-perl cpan Test-WWW-Mechanize 1.60 main/is_deeply().( [$mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' )], ['Bob and Bongo!'], 'list context' );
67 isscip-perl cpan Test-WWW-Mechanize 1.60 main/is().( $mech->scrape_text_by_idscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scrape_text_by_id().( 'asdf' ), 'Bob and Bongo!', 'scalar context' );
68 };
69 };
70
71
72 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'scraped_id_is and scraped_id_like' => sub {
73 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 5;
74
75 my $mech = Test::WWW::Mechanize->newscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/new().( autolint => 0 );
76 isa_okscip-perl cpan Test-WWW-Mechanize 1.60 main/isa_ok().( $mech, 'Test::WWW::Mechanize' );
77
78 my $uri = URI::file->new_absscip-perl cpan Test-WWW-Mechanize 1.60 URI/file/new_abs().( 't/goodlinks.html' )->as_stringscip-perl cpan Test-WWW-Mechanize 1.60 main/as_string().;
79 $mech->get_okscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/get_ok().( $uri, 'Get a dummy page just to have one' );
80
81 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'find one' => sub {
82 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
83 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf">contents</p></body></html>' );
84 $mech->scraped_id_isscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_is().( 'asdf', 'contents', 'Works in scalar context' );
85 $mech->scraped_id_likescip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_like().( 'asdf', qr/con.+s/, 'Works on regexes' );
86 };
87
88 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'nested tag' => sub {
89 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
90
91 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf">Bob and <b>Bongo!</b></p></body></html>' );
92 $mech->scraped_id_isscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_is().( 'asdf', 'Bob and Bongo!' );
93 $mech->scraped_id_likescip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_like().( 'asdf', qr/Bob.+Bongo/ );
94 };
95
96 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'failures' => sub {
97 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 6;
98
99 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( '<html><head><title></title></head><body><p id="asdf">Bob and <b>Bongo!</b></p><p id="empty"></p></body></html>' );
100
101 # Test standard successes.
102 $mech->scraped_id_isscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_is().( 'asdf', 'Bob and Bongo!' );
103 $mech->scraped_id_likescip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_like().( 'asdf', qr/Bob.+Bongo/ );
104
105 # Test failures.
106 test_outscip-perl cpan Test-WWW-Mechanize 1.60 main/test_out().( 'not ok 1 - Trying to match nonexistent ID to a string' );
107 test_failscip-perl cpan Test-WWW-Mechanize 1.60 main/test_fail().( +2 );
108 test_diagscip-perl cpan Test-WWW-Mechanize 1.60 main/test_diag().( q{Can't find ID "nonexistent" to compare to "foo"} );
109 $mech->scraped_id_isscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_is().( 'nonexistent', 'foo', 'Trying to match nonexistent ID to a string' );
110 test_testscip-perl cpan Test-WWW-Mechanize 1.60 main/test_test().( 'Fails when trying to find nonexistent ID' );
111
112 my $regex = qr/Dave/ism;
113 test_outscip-perl cpan Test-WWW-Mechanize 1.60 main/test_out().( 'not ok 1 - Trying to match nonexistent ID to a regex' );
114 test_failscip-perl cpan Test-WWW-Mechanize 1.60 main/test_fail().( +2 );
115 test_diagscip-perl cpan Test-WWW-Mechanize 1.60 main/test_diag().( qq{Can't find ID "nonexistent" to match against $regex} );
116 $mech->scraped_id_likescip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_like().( 'nonexistent', $regex, 'Trying to match nonexistent ID to a regex' );
117 test_testscip-perl cpan Test-WWW-Mechanize 1.60 main/test_test().( 'Fails when mismatched against existing ID' );
118
119 # Make sure that empty tags don't get seen as non-existent.
120 $mech->scraped_id_isscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_is().( 'empty', '' );
121 $mech->scraped_id_likescip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_like().( 'empty', qr/^$/ );
122
123 };
124 };
125
126
127 # Previous versions would miss a search for id="foo" if it was not
128 # exactly id="foo". Here we test for variants.
129 subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). 'scrape_text_by_id optimization' => sub {
130 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 6;
131
132 _find_the_chipsscip-perl cpan Test-WWW-Mechanize 1.60 main/_find_the_chips().( <<'HTML', 'Double-quoted ID' );
133 <html>
134 <head><title>Bongo</title></head>
135 <body>not chips<p id="fish">chips</p>also not chips</body></html>
136 HTML
137
138 _find_the_chipsscip-perl cpan Test-WWW-Mechanize 1.60 main/_find_the_chips().( <<'HTML', 'Single-quoted ID' );
139 <html>
140 <head><title>Bongo</title></head>
141 <body>not chips<p id='fish'>chips</p>also not chips</body></html>
142 HTML
143
144 _find_the_chipsscip-perl cpan Test-WWW-Mechanize 1.60 main/_find_the_chips().( <<'HTML', 'Unquoted ID' );
145 <html>
146 <head><title>Bongo</title></head>
147 <body>not chips<p id=fish>chips</p>also not chips</body></html>
148 HTML
149
150 _find_the_chipsscip-perl cpan Test-WWW-Mechanize 1.60 main/_find_the_chips().( <<'HTML', 'Abnormal spacing' );
151 <html>
152 <head><title>Bongo</title></head>
153 <body>not chips<p id = fish >chips</p>also not chips</body></html>
154 HTML
155
156 _find_the_chipsscip-perl cpan Test-WWW-Mechanize 1.60 main/_find_the_chips().( <<'HTML', 'Unquoted broken across lines' );
157 <html>
158 <head><title>Bongo</title></head>
159 <body>not chips<p id
160 =
161 fish >chips</p>also not chips</body></html>
162 HTML
163
164 _find_the_chipsscip-perl cpan Test-WWW-Mechanize 1.60 main/_find_the_chips().( <<'HTML', 'Quoted broken across lines' );
165 <html>
166 <head><title>Bongo</title></head>
167 <body>not chips<p
168 id
169 =
170 "fish"
171 >
172 chips
173 </p>
174 also not chips</body></html>
175 HTML
176 };
177
178 sub _find_the_chipsscip-perl cpan Test-WWW-Mechanize 1.60 main/_find_the_chips(). {
179 local $Test::Builder::Levelscip-perl cpan Test-WWW-Mechanize 1.60 Test/Builder/$Level. = $Test::Builder::Levelscip-perl cpan Test-WWW-Mechanize 1.60 Test/Builder/$Level. + 1;
180
181 my $html = shiftscip-perl cpan Test-WWW-Mechanize 1.60 main/shift().;
182 my $msg = shiftscip-perl cpan Test-WWW-Mechanize 1.60 main/shift(). or diescip-perl cpan Test-WWW-Mechanize 1.60 main/die().;
183
184 return subtestscip-perl cpan Test-WWW-Mechanize 1.60 main/subtest(). "_find_the_chips( $msg )" => sub {
185 planscip-perl cpan Test-WWW-Mechanize 1.60 main/plan(). tests => 2;
186
187 my $mech = Test::WWW::Mechanize->newscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/new().( autolint => 0 );
188 isa_okscip-perl cpan Test-WWW-Mechanize 1.60 main/isa_ok().( $mech, 'Test::WWW::Mechanize' );
189 $mech->update_htmlscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/update_html().( $html );
190 $mech->scraped_id_isscip-perl cpan Test-WWW-Mechanize 1.60 Test/WWW/Mechanize/scraped_id_is().( 'fish', 'chips' );
191 };
192 }
193
194
195 done_testingscip-perl cpan Test-WWW-Mechanize 1.60 main/done_testing().();
196
197 exitscip-perl cpan Test-WWW-Mechanize 1.60 main/exit(). 0;