blib/lib/Langertha/Holmgang/Sim/Roost.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 42 / 42 Conditioncond 70.9% 22 / 31 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 90.2% 157 / 174 CC 35 SCAR 18.8
CC
35
Cov
94%
_t_survey_roost
12.0
_t_move_dragon
8.0
tool
5.3
CRAP 6.6
« blib/lib/Langertha/Holmgang/Case.pm ↑ Summary blib/lib/Langertha/Holmgang/Template.pm »
1 package Langertha::Holmgang::Sim::Roost;
2 # ABSTRACT: The answer world roost — an invented dragon roost with perches and a larder, for the example suite demo-roost
3  
4 38 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 38 with 'Langertha::Holmgang::Role::AnswerWorld';
8  
9 38 our $VERSION = '0.001';
10  
11  
12 38 our %TOOLS = (
13 survey_roost => [],
14 feed_dragon => [qw( dragon food )],
15 move_dragon => [qw( dragon perch )],
16 );
17  
18 #### Plugin contract
19  
20 80 ▶sub device_type { 'roost' }
Subroutine device_type: called CC=1 SCAR=0.0
Pod documented
21  
22  
23 17 ▶sub tools { return \%TOOLS }
Subroutine tools: called CC=1 SCAR=0.0
Pod documented
24  
25  
26 89 ▶sub tool {
Subroutine tool: called CC=5 SCAR=16.7
Pod documented
27 89 my ( $self, $roost, $name, $args, $ctx ) = @_;
28 89 ▶ $name //= '';
Condition50%1/2
$name //= ""
l!l
890
MC/DC0%0/1
$name //= ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
29 89 ▶ my $code = $TOOLS{$name} && $self->can( '_t_'.$name )
Branch: 1 branch
BranchTrueFalseTotal
my $code = $Langertha::Holmgang::Sim::Roost::TOOLS{$name} && $self->can('_t_' . $name) or return q[Error: unknown tool '] . $name . q['.], 0 88 1 89
Condition66%2/3
$Langertha::Holmgang::Sim::Roost::TOOLS{$name} && $self->can('_t_' . $name)
!ll&&!rl&&r
1088
MC/DC50%1/2
$Langertha::Holmgang::Sim::Roost::TOOLS{$name} && $self->can('_t_' . $name)
$Langertha::Holmgang::Sim::Roost::TOOLS{$name} $self->can('_t_' . $name)
Truth table66%2/3
A && B
A= $Langertha::Holmgang::Sim::Roost::TOOLS{$name}B= $self->can('_t_' . $name)
ABresult
0X0
100
111
30 or return ( "Error: unknown tool '".$name."'.", 0 );
31 88 ▶ return $self->$code( $roost, $args || {} );
Condition50%1/2
$args || {}
l!l
880
MC/DC0%0/1
$args || {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
32 }
33  
34  
35 #### State
36  
37 # the key of a hash entry, matched without case and surrounding white space
38 153 ▶sub _key {
Subroutine _key: called CC=4 SCAR=14.2
39 153 my ( $self, $h, $want ) = @_;
40 153 ▶ return undef unless ref $h eq 'HASH' && defined $want && !ref $want;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "HASH" and defined $want and not ref $want 1 152 153
Condition50%3/6
ref $h eq "HASH" and defined $want and not ref $want
!ll&&!rl&&r
01152
ref $h eq "HASH" and defined $want
!ll&&!rl&&r
00153
MC/DC33%1/3
ref $h eq "HASH" and defined $want and not ref $want
ref $h eq "HASH" defined $want not ref $want
Truth table50%2/4
A and B and not(C)
A= ref $h eq "HASH"B= defined $wantC= not ref $want
ABCresult
0XX0
10X0
1100
1111
41 152 ( my $w = lc $want ) =~ s/\A\s+|\s+\z//g;
42 152 my ($k) = grep { lc $_ eq $w } sort keys %$h;
43 152 return $k;
44 }
45  
46 5 ▶sub _given {
Subroutine _given: called CC=3 SCAR=11.1
47 5 my ( $self, $v ) = @_;
48 5 ▶ return defined $v && !ref $v ? $v : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v && !ref($v) ? : 4 1 5
Condition66%2/3
defined $v and not ref $v
!ll&&!rl&&r
014
MC/DC50%1/2
defined $v and not ref $v
defined $v not ref $v
Truth table66%2/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
49 }
50  
51 55 ▶sub _baskets {
Subroutine _baskets: called CC=2 SCAR=6.9
52 55 my ( $self, $n ) = @_;
53 55 ▶ return $n.' basket'.( $n == 1 ? '' : 's' );
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 27 28 55
54 }
55  
56 46 ▶sub _list {
Subroutine _list: called CC=3 SCAR=11.0
57 46 my ( $self, $l ) = @_;
58 46 ▶ my @l = ref $l eq 'ARRAY' ? @$l : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $l eq 'ARRAY' ? : 44 2 46
59 46 ▶ return @l ? join( ', ', @l ) : 'none';
Branch: 1 branch
BranchTrueFalseTotal
@l ? : 22 24 46
60 }
61  
62 #### Tools
63  
64 10 ▶sub _t_survey_roost {
Subroutine _t_survey_roost: called CC=12 SCAR=24.9
65 10 my ( $self, $r ) = @_;
66 10 ▶ my $d = ref $r->{dragons} eq 'HASH' ? $r->{dragons} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $$r{'dragons'} eq 'HASH' ? : 9 1 10
67 10 ▶ my $p = ref $r->{perches} eq 'HASH' ? $r->{perches} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $$r{'perches'} eq 'HASH' ? : 9 1 10
68 10 ▶ my $l = ref $r->{larder} eq 'HASH' ? $r->{larder} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $$r{'larder'} eq 'HASH' ? : 9 1 10
69 10 ▶ my %on = map { defined $d->{$_}{perch} ? ( $d->{$_}{perch} => $_ ) : () } keys %$d;
Branch: 1 branch
BranchTrueFalseTotal
defined $$d{$_}{'perch'} ? : 22 1 23
70 10 my @out = ('Dragons:');
71 push @out, map {
72 10 my $x = $d->{$_};
73 '- '.$_.': wingspan '.( $x->{wingspan} // '?' ).', perch '.( $x->{perch} // 'none' )
74 23 ▶ .', eats '.$self->_list( $x->{eats} ).', meals '.$self->_list( $x->{meals} );
Condition75%3/4
$$x{"wingspan"} // "?"
l!l
230
$$x{"perch"} // "none"
l!l
221
MC/DC0%0/1
$$x{"wingspan"} // "?"
$$x{"wingspan"}
MC/DC100%1/1
$$x{"perch"} // "none"
$$x{"perch"}
Truth table50%1/2
A
A= $$x{"wingspan"}
Aresult
11
00
Truth table100%2/2
A
A= $$x{"perch"}
Aresult
11
00
75 } sort keys %$d;
76 10 ▶ push @out, '- none' unless %$d;
Branch: 1 branch
BranchTrueFalseTotal
unless %$d 1 9 10
77 10 push @out, 'Perches:';
78 10 ▶ push @out, map { '- '.$_.': span '.( $p->{$_}{span} // '?' ).', '.( $on{$_} // 'free' ) } sort keys %$p;
Condition75%3/4
$$p{$_}{"span"} // "?"
l!l
310
$on{$_} // "free"
l!l
229
MC/DC0%0/1
$$p{$_}{"span"} // "?"
$$p{$_}{"span"}
MC/DC100%1/1
$on{$_} // "free"
$on{$_}
Truth table50%1/2
A
A= $$p{$_}{"span"}
Aresult
11
00
Truth table100%2/2
A
A= $on{$_}
Aresult
11
00
79 10 ▶ push @out, '- none' unless %$p;
Branch: 1 branch
BranchTrueFalseTotal
unless %$p 1 9 10
80 10 push @out, 'Larder:';
81 10 push @out, map { '- '.$_.': '.$self->_baskets( $l->{$_} ) } sort keys %$l;
82 10 ▶ push @out, '- empty' unless %$l;
Branch: 1 branch
BranchTrueFalseTotal
unless %$l 1 9 10
83 10 return ( join( "\n", @out ), 1 );
84 }
85  
86 42 ▶sub _t_feed_dragon {
Subroutine _t_feed_dragon: called CC=5 SCAR=16.1
87 42 my ( $self, $r, $a ) = @_;
88 my $dk = $self->_key( $r->{dragons}, $a->{dragon} )
89 42 ▶ or return ( "Error: there is no dragon called '".$self->_given( $a->{dragon} )."'.", 0 );
Branch: 1 branch
BranchTrueFalseTotal
my $dk = $self->_key($$r{"dragons"}, $$a{"dragon"}) or return q[Error: there is no dragon called '] . $self->_given($$a{"dragon"}) . q['.], 0 40 2 42
90 my $fk = $self->_key( $r->{larder}, $a->{food} )
91 40 ▶ or return ( "Error: the larder has no '".$self->_given( $a->{food} )."'.", 0 );
Branch: 1 branch
BranchTrueFalseTotal
my $fk = $self->_key($$r{"larder"}, $$a{"food"}) or return q[Error: the larder has no '] . $self->_given($$a{"food"}) . q['.], 0 39 1 40
92 39 ▶ return ( 'Error: no '.$fk.' left in the larder.', 0 ) unless $r->{larder}{$fk} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $$r{"larder"}{$fk} > 0 2 37 39
93 37 $r->{larder}{$fk}--;
94 37 my $dragon = $r->{dragons}{$dk};
95 37 ▶ $dragon->{meals} = [] unless ref $dragon->{meals} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$dragon{"meals"} eq "ARRAY" 1 36 37
96 37 push @{ $dragon->{meals} }, $fk;
97 37 return ( $dk.' ate a basket of '.$fk.'. Left in the larder: '.$self->_baskets( $r->{larder}{$fk} ).'.', 1 );
98 }
99  
100 36 ▶sub _t_move_dragon {
Subroutine _t_move_dragon: called CC=8 SCAR=20.8
101 36 my ( $self, $r, $a ) = @_;
102 my $dk = $self->_key( $r->{dragons}, $a->{dragon} )
103 36 ▶ or return ( "Error: there is no dragon called '".$self->_given( $a->{dragon} )."'.", 0 );
Branch: 1 branch
BranchTrueFalseTotal
my $dk = $self->_key($$r{"dragons"}, $$a{"dragon"}) or return q[Error: there is no dragon called '] . $self->_given($$a{"dragon"}) . q['.], 0 35 1 36
104 my $pk = $self->_key( $r->{perches}, $a->{perch} )
105 35 ▶ or return ( "Error: there is no perch called '".$self->_given( $a->{perch} )."'.", 0 );
Branch: 1 branch
BranchTrueFalseTotal
my $pk = $self->_key($$r{"perches"}, $$a{"perch"}) or return q[Error: there is no perch called '] . $self->_given($$a{"perch"}) . q['.], 0 34 1 35
106 34 my $dragon = $r->{dragons}{$dk};
107 34 ▶ return ( $dk.' is already on '.$pk.'.', 1 ) if ( $dragon->{perch} // '' ) eq $pk;
Branch: 1 branch
BranchTrueFalseTotal
if ($$dragon{"perch"} // "") eq $pk 1 33 34
Condition100%2/2
$$dragon{"perch"} // ""
l!l
331
MC/DC100%1/1
$$dragon{"perch"} // ""
$$dragon{"perch"}
Truth table100%2/2
A
A= $$dragon{"perch"}
Aresult
11
00
108 33 ▶ my ($other) = grep { $_ ne $dk && ( $r->{dragons}{$_}{perch} // '' ) eq $pk } sort keys %{ $r->{dragons} };
Condition100%5/5
$_ ne $dk && ($$r{'dragons'}{$_}{'perch'} // '') eq $pk
!ll&&!rl&&r
33651
$$r{"dragons"}{$_}{"perch"} // ""
l!l
633
MC/DC100%2/2
$_ ne $dk && ($$r{'dragons'}{$_}{'perch'} // '') eq $pk
$_ ne $dk $$r{"dragons"}{$_}{"perch"}
Truth table100%3/3
A && B
A= $_ ne $dkB= $$r{"dragons"}{$_}{"perch"}
ABresult
0X0
100
111
109 33 ▶ return ( 'Error: '.$pk.' is taken by '.$other.'.', 0 ) if defined $other;
Branch: 1 branch
BranchTrueFalseTotal
if defined $other 1 32 33
110 32 $dragon->{perch} = $pk;
111 32 return ( $dk.' flew to '.$pk.'.', 1 );
112 }
113  
114 38 __PACKAGE__->meta->make_immutable;
115  
116 __END__
« blib/lib/Langertha/Holmgang/Case.pm ↑ Summary blib/lib/Langertha/Holmgang/Template.pm »