Simple/Route.pm

filter: Statementstmt 100% 77 / 77 Branchbran 93.4% 43 / 46 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 94.0% 143 / 152 CC 30 SCAR 26.3
CC
30
Cov
96%
new
16.1
match
15.0
BEGIN
1.0
CRAP 13.9
↑ Summary Simple.pm »
1 package Router::Simple::Route;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Class::Accessor::Lite 0.05 (
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 rw => [qw(name dest on_match method host pattern)],
8 13 );
9  
10 44 ▶sub new {
Subroutine new: called CC=16 SCAR=27.8
Pod undocumented
11 44 my $class = shift;
12  
13 # connect([$name, ]$pattern[, \%dest[, \%opt]])
14 44 ▶ if (@_ == 1 || ref $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 or ref $_[1]) 35 9 44
Condition100%3/3
@_ == 1 or ref $_[1]
l!l&&r!l&&!r
2339
MC/DC100%2/2
@_ == 1 or ref $_[1]
@_ == 1 ref $_[1]
Truth table100%3/3
A or B
A= @_ == 1B= ref $_[1]
ABresult
1X1
011
000
15 35 unshift(@_, undef);
16 }
17  
18 44 my ($name, $pattern, $dest, $opt) = @_;
19 44 ▶ Carp::croak("missing pattern") unless $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern 0 44 44
20 my $row = +{
21 name => $name,
22 dest => $dest,
23 on_match => $opt->{on_match},
24 44 };
25 44 ▶ if (my $method = $opt->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $opt->{"method"}) 23 21 44
26 23 ▶ $method = [$method] unless ref $method;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $method 23 0 23
27 23 $row->{method} = $method;
28  
29 23 my $method_re = join '|', @{$method};
30 23 $row->{method_re} = qr{^(?:$method_re)\z};
31 }
32 44 ▶ if (my $host = $opt->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $opt->{"host"}) 6 38 44
33 6 $row->{host} = $host;
34 6 ▶ $row->{host_re} = ref $host ? $host : qr(^\Q$host\E\z);
Branch: 1 branch
BranchTrueFalseTotal
ref $host ? : 0 6 6
35 }
36  
37 44 $row->{pattern} = $pattern;
38  
39 # compile pattern
40 44 my @capture;
41 44 $row->{pattern_re} = do {
42 44 ▶ if (ref $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern) { } 2 42 44
43 2 $row->{_regexp_capture} = 1;
44 2 $pattern;
45 } else {
46 42 $pattern =~ s!
47 \{((?:\{[0-9,]+\}|[^{}]+)+)\} | # /blog/{year:\d{4}}
48 :([A-Za-z0-9_]+) | # /blog/:year
49 (\*) | # /blog/*/*
50 ([^{:*]+) # normal string
51 !
52 108 ▶ if ($1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($1) { } 30 78 108
elsif ($2) { } 7 71 78
elsif ($3) { } 4 67 71
53 30 my ($name, $pattern) = split /:/, $1, 2;
54 30 push @capture, $name;
55 30 ▶ $pattern ? "($pattern)" : "([^/]+)";
Branch: 1 branch
BranchTrueFalseTotal
$pattern ? : 21 9 30
56 } elsif ($2) {
57 7 push @capture, $2;
58 7 "([^/]+)";
59 } elsif ($3) {
60 4 push @capture, '__splat__';
61 4 "(.+)";
62 } else {
63 67 quotemeta($4);
64 }
65 !gex;
66 # for example, pattern '/comment/' will both match '/comment/' and '/comment'
67 42 ▶ $pattern .= '?' if $opt->{directory_slash} and $pattern =~ m{\/$};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"directory_slash"} and $pattern =~ m[\/$] 4 38 42
Condition66%2/3
$opt->{"directory_slash"} and $pattern =~ m[\/$]
!ll&&!rl&&r
3804
MC/DC50%1/2
$opt->{"directory_slash"} and $pattern =~ m[\/$]
$opt->{"directory_slash"} $pattern =~ m[\/$]
Truth table66%2/3
A and B
A= $opt->{"directory_slash"}B= $pattern =~ m[\/$]
ABresult
0X0
100
111
68 42 qr{^$pattern\z};
69 }
70 };
71 44 $row->{capture} = \@capture;
72 44 ▶ $row->{dest} ||= +{};
Condition100%2/2
$row->{"dest"} ||= {}
l!l
413
MC/DC100%1/1
$row->{"dest"} ||= {}
$row->{"dest"}
Truth table100%2/2
A
A= $row->{"dest"}
Aresult
11
00
73  
74 44 return bless $row, $class;
75 }
76  
77 141 ▶sub match {
Subroutine match: called CC=15 SCAR=27.1
Pod undocumented
78 141 my ($self, $env) = @_;
79  
80 141 ▶ if ($self->{host_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"host_re"}) 20 121 141
81 20 ▶ unless ($env->{HTTP_HOST} =~ $self->{host_re}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"HTTP_HOST"} =~ /$self->{"host_re"}/) 5 15 20
82 5 return undef;
83 }
84 }
85 136 ▶ if (my @captured = ($env->{PATH_INFO} =~ $self->{pattern_re})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@captured) = $env->{"PATH_INFO"} =~ /$self->{"pattern_re"}/) 48 88 136
86 48 my %args;
87 my @splat;
88 48 ▶ if ($self->{_regexp_capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_regexp_capture'}) { } 1 47 48
89 1 push @splat, @captured;
90 } else {
91 47 ▶ if (@{$self->{capture}} > 0 && scalar(@{$self->{capture}}) != scalar(@captured)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"capture"};} > 0 and scalar @{$self->{"capture"};} != scalar @captured) 1 46 47
Condition100%3/3
@{$self->{"capture"};} > 0 and scalar @{$self->{"capture"};} != scalar @captured
!ll&&!rl&&r
21251
MC/DC100%2/2
@{$self->{"capture"};} > 0 and scalar @{$self->{"capture"};} != scalar @captured
@{$self->{"capture"};} > 0 scalar @{$self->{"capture"};} != scalar @captured
Truth table100%3/3
A and B
A= @{$self->{"capture"};} > 0B= scalar @{$self->{"capture"};} != scalar @captured
ABresult
0X0
100
111
92 # Should not contain parenthesis in regexp pattern
93 #
94 # Good: "/{date:(?:\d+)}"
95 # Bad: "/{date:(\d+)}"
96 1 Carp::carp("Path pattern should not contain paren. This code may not works in future version of Router::Simple. : " . $self->{pattern});
97 }
98  
99 47 for my $i (0..@{$self->{capture}}-1) {
100 50 ▶ if ($self->{capture}->[$i] eq '__splat__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'capture'}[$i] eq '__splat__') { } 4 46 50
101 4 push @splat, $captured[$i];
102 } else {
103 46 $args{$self->{capture}->[$i]} = $captured[$i];
104 }
105 }
106 }
107 48 ▶ if ($self->{method_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"method_re"}) 30 18 48
108 30 ▶ unless (($env->{REQUEST_METHOD} || '') =~ $self->{method_re}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($env->{"REQUEST_METHOD"} || "") =~ /$self->{"method_re"}/) 9 21 30
Condition50%1/2
$env->{'REQUEST_METHOD'} || ''
l!l
300
MC/DC0%0/1
$env->{'REQUEST_METHOD'} || ''
$env->{'REQUEST_METHOD'}
Truth table50%1/2
A
A= $env->{'REQUEST_METHOD'}
Aresult
11
00
109 9 $Router::Simple::_METHOD_NOT_ALLOWED = 1;
110 9 return undef;
111 }
112 }
113 my $match = +{
114 39 ▶ %{$self->{dest}},
Branch: 1 branch
BranchTrueFalseTotal
@splat ? : 3 36 39
115 %args,
116 ( @splat ? ( splat => \@splat ) : () ),
117 };
118 39 ▶ if ($self->{on_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_match"}) 3 36 39
119 3 my $ret = $self->{on_match}->($env, $match);
120 3 ▶ return undef unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 1 2 3
121 }
122 38 return $match;
123 }
124 88 return undef;
125 }
126  
127 13 1;
128 __END__
↑ Summary Simple.pm »