Test/Page/HTML.pm

filter: Statementstmt 75.9% 41 / 54 Branchbran 12.5% 1 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 10 / 12 Podpod 80.0% 4 / 5 totaltotal 66.2% 59 / 89 CC 8 SCAR 17.7
CC
8
Cov
66%
delete
20.0
DESTROY
6.0
tree
2.3
CRAP 5.9
« Test/Form/Widget/Menu.pm ↑ Summary Test/Form.pm »
1 package CGI::Test::Page::HTML;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 ####################################################################
5 # $Id: HTML.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
6 # $Name: cgi-test_0-104_t1 $
7 ####################################################################
8 #
9 # Copyright (c) 2001, Raphael Manfredi
10 #
11 # You may redistribute only under the terms of the Artistic License,
12 # as specified in the README file that comes with the distribution.
13  
14 14 require CGI::Test::Page::Real;
15 14 ▶use base qw(CGI::Test::Page::Real);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #
18 # ->new
19 #
20 # Creation routine
21 #
22 sub new
23 16 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
24 16 my $this = bless {}, shift;
25 16 $this->_init(@_);
26 16 return $this;
27 }
28  
29 #
30 # Attribute access
31 #
32  
33 sub tree
34 16 ▶{
Subroutine tree: called CC=2 SCAR=8.1
Pod documented
35 16 my $this = shift;
36 16 ▶ return $this->{tree} || $this->_build_tree();
Condition33%1/3
$this->{'tree'} || $this->_build_tree
l!l&&r!l&&!r
0160
MC/DC0%0/2
$this->{'tree'} || $this->_build_tree
$this->{'tree'} $this->_build_tree
Truth table33%1/3
A || B
A= $this->{'tree'}B= $this->_build_tree
ABresult
1X1
011
000
37 }
38  
39 sub forms
40 16 ▶{
Subroutine forms: called CC=2 SCAR=7.1
Pod documented
41 16 my $this = shift;
42 16 ▶ return $this->{forms} || $this->_xtract_forms();
Condition66%2/3
$this->{'forms'} || $this->_xtract_forms
l!l&&r!l&&!r
2140
MC/DC0%0/2
$this->{'forms'} || $this->_xtract_forms
$this->{'forms'} $this->_xtract_forms
Truth table66%2/3
A || B
A= $this->{'forms'}B= $this->_xtract_forms
ABresult
1X1
011
000
43 }
44  
45 sub form_count
46 2 ▶{
Subroutine form_count: called CC=2 SCAR=7.1
Pod documented
47 2 my $this = shift;
48 2 ▶ $this->_xtract_forms() unless exists $this->{form_count};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"form_count"} 2 0 2
49 2 return $this->{form_count};
50 }
51  
52 #
53 # ->_build_tree
54 #
55 # Parse HTML content from `raw_content' into an HTML tree.
56 # Only called the first time an access to `tree' is requested.
57 #
58 # Returns constructed tree object.
59 #
60 sub _build_tree
61 16 ▶{
Subroutine _build_tree: called CC=1 SCAR=0.0
62 16 my $this = shift;
63  
64 16 require HTML::TreeBuilder;
65  
66 16 my $tree = HTML::TreeBuilder->new();
67 16 $tree->ignore_unknown(0); # Keep everything, even unknown tags
68 16 $tree->store_comments(1); # Useful things may hide in "comments"
69 16 $tree->store_declarations(1); # Store everything that we may test
70 16 $tree->store_pis(1); # Idem
71 16 $tree->warn(1); # We want to know if there's a problem
72  
73 16 $tree->parse($this->raw_content);
74 16 $tree->eof;
75  
76 16 return $this->{tree} = $tree;
77 }
78  
79 #
80 # _xtract_forms
81 #
82 # Extract <FORMS> tags out of the tree, and for each form, build a
83 # CGI::Test::Form object that represents it.
84 # Only called the first time an access to `forms' is requested.
85 #
86 # Side effect: updates the `forms' and `form_count' attributes.
87 #
88 # Returns list ref of objects, in the order they were found.
89 #
90 sub _xtract_forms
91 16 ▶{
Subroutine _xtract_forms: called CC=1 SCAR=0.0
92 16 my $this = shift;
93 16 my $tree = $this->tree;
94  
95 16 require CGI::Test::Form;
96  
97 #
98 # The CGI::Test::Form objects we're about to create will refer back to
99 # us, because they are conceptually part of this page. Besides, their
100 # HTML tree is a direct reference into our own tree.
101 #
102  
103 16 ▶ my @forms = $tree->look_down(sub {$_[ 0 ]->tag eq "form"});
Subroutine __ANON__: called CC=1 SCAR=0.0
104 16 @forms = map {CGI::Test::Form->new($_, $this)} @forms;
105  
106 16 $this->{form_count} = scalar @forms;
107 16 return $this->{forms} = \@forms;
108 }
109  
110 #
111 # ->delete
112 #
113 # Break circular references
114 #
115 sub delete
116 0 ▶{
Subroutine delete: not called CC=4 SCAR=30.0
Pod documented
117 0 my $this = shift;
118  
119 #
120 # The following attributes are "lazy", i.e. calculated on demand.
121 # Therefore, take precautions before de-referencing them.
122 #
123  
124 0 ▶ $this->{tree} = $this->{tree}->delete if ref $this->{tree};
Branch: 1 branch
BranchTrueFalseTotal
if ref $this->{"tree"} 0 0 0
125 0 ▶ if (ref $this->{forms})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $this->{"forms"}) 0 0 0
126 {
127 0 foreach my $form (@{$this->{forms}})
128 {
129 0 $form->delete;
130 }
131 0 delete $this->{forms};
132 }
133  
134 0 $this->SUPER::delete;
135 0 return;
136 }
137  
138 #
139 # (DESTROY)
140 #
141 # Dispose of HTML tree properly
142 #
143 sub DESTROY
144 0 ▶{
Subroutine DESTROY: not called CC=2 SCAR=17.9
145 0 my $this = shift;
146 0 ▶ return unless ref $this->{tree};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{"tree"} 0 0 0
147 0 $this->{tree} = $this->{tree}->delete;
148 0 return;
149 }
150  
151 14 1;
152  
153 =head1 NAME
154  
155 CGI::Test::Page::HTML - A HTML page reply
156  
157 =head1 SYNOPSIS
158  
159 # Inherits from CGI::Test::Page::Real
160  
161 =head1 DESCRIPTION
162  
163 This class represents an HTTP reply containing C<text/html> data.
164 When testing CGI scripts, this is usually what one gets back.
165  
166 =head1 INTERFACE
167  
168 The interface is the same as the one described in L<CGI::Test::Page::Real>,
169 with the following addition:
170  
171 =over 4
172  
173 =item C<tree>
174  
175 Returns the root of the HTML tree of the page content, as an
176 HTML::Element node.
177  
178 =back
179  
180 =head1 AUTHORS
181  
182 The original author is Raphael Manfredi.
183  
184 Steven Hilton was long time maintainer of this module.
185  
186 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
187  
188 =head1 SEE ALSO
189  
190 CGI::Test::Page::Real(3), HTML::Element(3).
191  
192 =cut
193  
« Test/Form/Widget/Menu.pm ↑ Summary Test/Form.pm »