blib/lib/Test/Mojo/Role/SubmitForm.pm

filter: Statementstmt 92.5% 50 / 54 Branchbran 93.7% 30 / 32 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 92.7% 102 / 110 CC 27 SCAR 24.0
CC
27
Cov
93%
click_ok
16.6
_gather_vals
7.0
_get_controls
6.0
CRAP 11.1
↑ Summary
1 package Test::Mojo::Role::SubmitForm;
2  
3 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '1.004005'; # VERSION
8  
9 16 ▶sub click_ok {
Subroutine click_ok: called CC=16 SCAR=28.1
Pod documented
10 16 my ( $self, $selector_or_dom, $extra_params ) = @_;
11 16 ▶ $extra_params ||= {};
Condition100%2/2
$extra_params ||= {}
l!l
115
MC/DC100%1/1
$extra_params ||= {}
$extra_params
Truth table100%2/2
A
A= $extra_params
Aresult
11
00
12  
13 16 ▶ my $el = eval { $selector_or_dom->isa('Mojo::DOM') }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $selector_or_dom->isa('Mojo::DOM') } } ? : 2 14 16
Condition100%2/2
$self->tx->res->dom->at($selector_or_dom) or croak('Test::Mojo::Role::SubmitForm::click_ok()' . " Did not find element matching selector $selector_or_dom")
l!l
131
MC/DC100%1/1
$self->tx->res->dom->at($selector_or_dom) or croak('Test::Mojo::Role::SubmitForm::click_ok()' . " Did not find element matching selector $selector_or_dom")
$self->tx->res->dom->at($selector_or_dom)
Truth table100%2/2
A
A= $self->tx->res->dom->at($selector_or_dom)
Aresult
11
00
14 ? $selector_or_dom
15 : $self->tx->res->dom->at($selector_or_dom)
16 || croak 'Test::Mojo::Role::SubmitForm::click_ok()'
17 . " Did not find element matching selector $selector_or_dom";
18 15 ▶ unless ( $el->tag eq 'form' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($el->tag eq "form") 7 8 15
19 7 ▶ if (length $el->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $el->{"name"}) 7 0 7
20 7 ▶ if ( ($el->{type}//'') eq 'image' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($el->{'type'} // '') eq 'image') { } 2 5 7
Condition50%1/2
$el->{"type"} // ""
l!l
70
MC/DC0%0/1
$el->{"type"} // ""
$el->{"type"}
Truth table50%1/2
A
A= $el->{"type"}
Aresult
11
00
21 2 $extra_params->{ $el->{name} . '.x' } = 1;
22 2 $extra_params->{ $el->{name} . '.y' } = 1;
23 }
24 else {
25 5 $extra_params->{ $el->{name} } = $el->val;
26 }
27 }
28  
29 7 $el = $el->ancestors('form')->first;
30 }
31  
32 15 for ( sort keys %$extra_params ) {
33 19 ▶ next unless ref $extra_params->{$_} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $extra_params->{$_} eq "CODE" 14 5 19
34 5 ( my $name = $_ ) =~ s/"/\\"/g;
35 5 $extra_params->{$_} = $extra_params->{$_}->(
36 $self->_gather_vals( $el->find(qq{[name="$name"]})->each )
37 );
38 }
39  
40 15 my %form = (
41 $self->_get_controls($el),
42 %$extra_params,
43 );
44 15 for (keys %form) {
45 82 ▶ delete $form{$_} unless defined $form{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $form{$_} 1 81 82
46 }
47  
48 15 ▶ if ( $ENV{MOJO_SUBMITFORM_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOJO_SUBMITFORM_DEBUG"}) 0 15 15
49 0 warn "\n########## SUBMITTING FORM ##########\n";
50 0 require Mojo::Util;
51 0 warn Mojo::Util::dumper(\%form);
52 0 warn "########## END FORM ##########\n\n";
53 }
54  
55 my $tx = $self->ua->build_tx(
56 15 ▶ $el->{method}||'GET' => defined $el->{action} && $el->{action} ne '' ? $el->{action} : $self->tx->req->url
Branch: 1 branch
BranchTrueFalseTotal
defined $el->{'action'} && $el->{'action'} ne '' ? : 12 3 15
Condition100%5/5
$el->{'method'} || 'GET'
l!l
87
defined $el->{"action"} and $el->{"action"} ne ""
!ll&&!rl&&r
2112
MC/DC100%1/1
$el->{'method'} || 'GET'
$el->{'method'}
MC/DC100%2/2
defined $el->{"action"} and $el->{"action"} ne ""
defined $el->{"action"} $el->{"action"} ne ""
Truth table100%2/2
A
A= $el->{'method'}
Aresult
11
00
Truth table100%3/3
A and B
A= defined $el->{"action"}B= $el->{"action"} ne ""
ABresult
0X0
100
111
57 => form => \%form,
58 );
59  
60 15 $self->request_ok( $tx );
61 }
62  
63 123 ▶sub _gather_vals {
Subroutine _gather_vals: called CC=7 SCAR=19.5
64 123 my ( $self, @els ) = @_;
65 123 my @vals;
66 123 for ( @els ) {
67 125 ▶ next if $_
Branch: 1 branch
BranchTrueFalseTotal
if $_->matches("[type=radio]:not(:checked), [type=checkbox]:not(:checked)") 16 109 125
68 ->matches('[type=radio]:not(:checked), [type=checkbox]:not(:checked)');
69  
70 109 ▶ defined( my $val = $_->val ) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $val = $_->val) or next 104 5 109
71 104 ▶ push @vals, ref $val ? @$val : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 10 94 104
72 }
73 123 ▶ return @vals == 1 ? $vals[0] : @vals ? \@vals : undef;
Branch: 2 branches
BranchTrueFalseTotal
@vals == 1 ? : 92 31 123
@vals ? : 11 20 31
74 }
75  
76 15 ▶sub _get_controls {
Subroutine _get_controls: called CC=6 SCAR=17.9
77 15 my ( $self, $form ) = @_;
78  
79 15 my @els = $form->find(
80 'input:not([type=button]):not([type=submit]):not([type=image]):not([disabled]),' .
81 'select:not([disabled]),' .
82 'textarea:not([disabled])'
83 )->each;
84  
85 # remove controls defined in templates
86 15 @els = grep { $_->ancestors('template')->size() == 0 } @els;
87  
88 15 my %controls;
89 15 for ( @els ) {
90 118 ▶ defined(my $vals = $self->_gather_vals( $_ )) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $vals = $self->_gather_vals($_)) or next 98 20 118
91 98 ▶ push @{ $controls{$_->{name}} }, ref $vals ? @$vals : $vals;
Branch: 1 branch
BranchTrueFalseTotal
ref $vals ? : 10 88 98
92 }
93 15 ▶ $#$_ or $_= $_->[0] for values %controls; # chage 1-el arrayrefs to strings
Branch: 1 branch
BranchTrueFalseTotal
$_->$#* or $_ = $_->[0] 25 48 73
94  
95 15 return %controls;
96 }
97  
98 2 q|
99 The fantastic element that explains the appeal of games to
100 many developers is neither the fire-breathing monsters nor the
101 milky-skinned, semi-clad sirens; it is the experience of
102 carrying out a task from start to finish without any change
103 in the user requirements.
104 |;
105  
106 __END__
↑ Summary