File Coverage

blib/lib/Data/Rx/CoreType/seq.pm
Criterion Covered Total %
statement 41 41 100.0
branch 16 16 100.0
condition 6 6 100.0
subroutine 7 7 100.0
pod 0 3 0.0
total 70 73 95.8


line stmt bran cond sub pod time code
1 1     1   5 use strict;
  1         2  
  1         31  
2 1     1   6 use warnings;
  1         2  
  1         47  
3             package Data::Rx::CoreType::seq;
4             # ABSTRACT: the Rx //seq type
5             $Data::Rx::CoreType::seq::VERSION = '0.200006';
6 1     1   5 use parent 'Data::Rx::CoreType';
  1         2  
  1         6  
7              
8 1     1   49 use Scalar::Util ();
  1         3  
  1         567  
9              
10 54     54 0 188 sub subname { 'seq' }
11              
12             sub guts_from_arg {
13 8     8 0 21 my ($class, $arg, $rx, $type) = @_;
14              
15 8 100       225 Carp::croak("unknown arguments to new")
16             unless Data::Rx::Util->_x_subset_keys_y($arg, {contents=>1,tail=>1});
17              
18 7 100 100     447 Carp::croak("no contents array given")
19             unless $arg->{contents} and (ref $arg->{contents} eq 'ARRAY');
20              
21 5         13 my $guts = {};
22              
23 12         36 my @content_schemata = map { $rx->make_schema($_) }
  5         15  
24 5         13 @{ $arg->{contents} };
25              
26 5         17 $guts->{content_schemata} = \@content_schemata;
27 3         15 $guts->{tail_check} = $arg->{tail}
28 3         22 ? $rx->make_schema({ %{$arg->{tail}},
29 5 100       172 skip => 0+@{$arg->{contents}}})
30             : undef;
31              
32 5         20 return $guts;
33             }
34              
35             sub assert_valid {
36 141     141 0 8481 my ($self, $value) = @_;
37              
38 141 100 100     1102 unless (! Scalar::Util::blessed($value) and ref $value eq 'ARRAY') {
39 117         1077 $self->fail({
40             error => [ qw(type) ],
41             message => "found value is not an arrayref",
42             value => $value,
43             });
44             }
45              
46 24         41 my @subchecks;
47              
48 24         40 my $content_schemata = $self->{content_schemata};
49 24 100       60 if (@$value < @$content_schemata) {
50 2         31 push @subchecks,
51             $self->new_fail({
52             error => [ qw(size) ],
53             size => 0 + @$value,
54             value => $value,
55             message => sprintf(
56             "too few entries found; found %s, need at least %s",
57             0 + @$value,
58             0 + @$content_schemata,
59             ),
60             });
61             }
62              
63 24         68 for my $i (0 .. $#$content_schemata) {
64 60 100       120 last if $i > $#$value;
65 58         363 push @subchecks, [
66             $value->[ $i ],
67             $content_schemata->[ $i ],
68             { data_path => [ [$i, 'index' ] ],
69             check_path => [
70             [ 'contents', 'key' ],
71             [ $i, 'index' ]
72             ],
73             },
74             ];
75             }
76              
77 24 100       78 if (@$value > @$content_schemata) {
78 19 100       41 if ($self->{tail_check}) {
79 11         45 push @subchecks, [
80             $value,
81             $self->{tail_check},
82             { check_path => [ ['tail', 'key' ] ] },
83             ];
84             } else {
85 8         103 push @subchecks,
86             $self->new_fail({
87             error => [ qw(size) ],
88             size => 0 + @$value,
89             value => $value,
90             message => sprintf(
91             "too many entries found; found %s, need no more than %s",
92             0 + @$value,
93             0 + @$content_schemata,
94             ),
95             });
96             }
97             }
98              
99 24         115 $self->perform_subchecks(\@subchecks);
100              
101 7         51 return 1;
102             }
103              
104             1;
105              
106             __END__