File Coverage

blib/lib/Repl/Spec/Args/VarArg.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 4 50.0
condition 6 14 42.8
subroutine 6 6 100.0
pod 0 3 0.0
total 43 56 76.7


line stmt bran cond sub pod time code
1             package Repl::Spec::Args::VarArg;
2            
3 1     1   1774 use strict;
  1         2  
  1         49  
4 1     1   5 use warnings;
  1         2  
  1         33  
5 1     1   5 use Carp;
  1         2  
  1         718  
6            
7             # Parameters:
8             # - A type spec.
9             sub new
10             {
11 1     1 0 9 my $invocant = shift;
12 1   33     8 my $class = ref($invocant) || $invocant;
13 1   50     3 my $typespec = shift || die "TypeSpec expected.";
14            
15 1         2 my $self= {};
16 1         3 $self->{TYPESPEC} = $typespec;
17 1         3 $self->{SPECNAME} = "var: " . $typespec->name();
18 1         4 return bless $self, $class;
19             }
20            
21             sub specname()
22             {
23 2     2 0 4 my $self = shift;
24 2         342 return $self->{SPECNAME};
25             }
26            
27             # Parameters:
28             # - An argument list (ref to array).
29             # - a position.
30             # - A context!
31             sub guard
32             {
33 6     6 0 8 my $self = shift;
34 6   50     14 my $args = shift || die "Argument list expected.";
35 6   50     21 my $pos = shift || die "Position expected.";
36 6   50     11 my $ctx = shift || die "Context expected";
37            
38 6 50 33     27 croak sprintf("Var argument at position %d: argument not present or type Pair found.", $pos) if($pos < 0 || $pos >= scalar(@$args));
39 6         9 my $typespec = $self->{TYPESPEC};
40 6         6 my $result;
41 6         41 eval {$result = $typespec->guard($args->[$pos], $ctx)};
  6         22  
42 6 50       25 croak sprintf("Var argument at position %d: %s", $pos, $@) if $@;
43 6         20 return $result;
44             }
45            
46             1;