File Coverage

blib/lib/Chess/Plisco/EPD/Record.pm
Criterion Covered Total %
statement 44 47 93.6
branch 14 18 77.7
condition 2 4 50.0
subroutine 8 8 100.0
pod 0 4 0.0
total 68 81 83.9


line stmt bran cond sub pod time code
1             #! /bin/false
2              
3             # Copyright (C) 2021 Guido Flohr ,
4             # all rights reserved.
5              
6             # This program is free software. It comes without any warranty, to
7             # the extent permitted by applicable law. You can redistribute it
8             # and/or modify it under the terms of the Do What the Fuck You Want
9             # to Public License, Version 2, as published by Sam Hocevar. See
10             # http://www.wtfpl.net/ for more details.
11              
12             package Chess::Plisco::EPD::Record;
13             $Chess::Plisco::EPD::Record::VERSION = '0.4';
14 5     5   33 use strict;
  5         10  
  5         141  
15 5     5   23 use integer;
  5         9  
  5         28  
16              
17 5     5   112 use Locale::TextDomain qw('Chess-Plisco');
  5         12  
  5         23  
18              
19 5     5   3802 use Chess::Plisco qw(:all);
  5         23  
  5         5298  
20              
21             sub new {
22 70000     70000 0 145643 my ($class, $line) = @_;
23              
24 70000         96262 my $ws = "[ \011-\015]";
25 70000         181552 $line =~ s/^$ws+//;
26 70000         121058 $line =~ s/ws+$//;
27 70000         349589 my ($pieces, $to_move, $castling, $ep_shift, $ops) = split /$ws+/, $line, 5;
28 70000 50       160801 if (!defined $ep_shift) {
29 0         0 die __"Incomplete EPD string.\n";
30             }
31              
32 70000         97896 my %operations;
33 70000         121248 while (length $ops) {
34 280000 50       1032698 if ($ops !~ s/^$ws*([_a-zA_Z0-9]+)//) {
35 0         0 die "Invalid EPD.\n";
36             }
37              
38 280000         534698 my $operation = $1;
39             die __x("Duplicate operation '{operation}'.", operation => $operation)
40 280000 50       456465 if exists $operations{$operation};
41            
42 280000         311690 my @operands;
43 280000         409868 while (length $ops) {
44 560038 100       2833831 if ($ops =~ s/^$ws*"(.*?)"//) {
    100          
    50          
45 70000         182968 push @operands, $1;
46             } elsif ($ops =~ s/^$ws*([^ \t;]+)//) {
47 210038         554635 push @operands, $1;
48             } elsif ($ops =~ s/^$ws*;$ws*//) {
49 280000         473888 last;
50             } else {
51 0         0 die __"Invalid EPD.\n";
52             }
53             }
54              
55 280000         884393 $operations{$operation} = [@operands];
56             }
57              
58 70000         264377 my $position = Chess::Plisco->new("$pieces $to_move $castling $ep_shift");
59 70000   50     234865 my $hmc = $operations{hmvc} || 0;
60 70000   50     165255 my $fmc = $operations{fmvc} || 1;
61 70000         184461 my $fen = "$pieces $to_move $castling $ep_shift $hmc $fmc";
62 70000         171328 my $position = Chess::Plisco->new($fen);
63              
64 70000         476845 bless {
65             __position => $position,
66             __operations => \%operations,
67             }, $class;
68             }
69              
70             sub position {
71 306     306 0 3996 shift->{__position};
72             }
73              
74             sub operations {
75 1835     1835 0 8014 shift->{__operations};
76             }
77              
78             sub operation {
79 1224     1224 0 783412 my ($self, $opcode) = @_;
80              
81 1224 100       3585 if (wantarray) {
    100          
82 612 100       959 return @{$self->operations->{$opcode} || []};
  612         1494  
83             } elsif (exists $self->operations->{$opcode}) {
84 611         1403 return $self->operations->{$opcode}->[0];
85             } else {
86 1         6 return;
87             }
88             }
89              
90             1;