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   4 use strict;
  1         1  
  1         25  
2 1     1   3 use warnings;
  1         1  
  1         30  
3             package Data::Rx::CoreType::seq;
4             # ABSTRACT: the Rx //seq type
5             $Data::Rx::CoreType::seq::VERSION = '0.200007';
6 1     1   4 use parent 'Data::Rx::CoreType';
  1         0  
  1         4  
7              
8 1     1   36 use Scalar::Util ();
  1         1  
  1         303  
9              
10 54     54 0 131 sub subname { 'seq' }
11              
12             sub guts_from_arg {
13 8     8 0 13 my ($class, $arg, $rx, $type) = @_;
14              
15 8 100       44 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     256 Carp::croak("no contents array given")
19             unless $arg->{contents} and (ref $arg->{contents} eq 'ARRAY');
20              
21 5         7 my $guts = {};
22              
23 12         25 my @content_schemata = map { $rx->make_schema($_) }
  5         12  
24 5         10 @{ $arg->{contents} };
25              
26 5         14 $guts->{content_schemata} = \@content_schemata;
27 3         8 $guts->{tail_check} = $arg->{tail}
28 3         14 ? $rx->make_schema({ %{$arg->{tail}},
29 5 100       17 skip => 0+@{$arg->{contents}}})
30             : undef;
31              
32 5         14 return $guts;
33             }
34              
35             sub assert_valid {
36 141     141 0 5068 my ($self, $value) = @_;
37              
38 141 100 100     646 unless (! Scalar::Util::blessed($value) and ref $value eq 'ARRAY') {
39 117         452 $self->fail({
40             error => [ qw(type) ],
41             message => "found value is not an arrayref",
42             value => $value,
43             });
44             }
45              
46 24         21 my @subchecks;
47              
48 24         31 my $content_schemata = $self->{content_schemata};
49 24 100       56 if (@$value < @$content_schemata) {
50 2         20 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         43 for my $i (0 .. $#$content_schemata) {
64 60 100       79 last if $i > $#$value;
65 58         217 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       50 if (@$value > @$content_schemata) {
78 19 100       48 if ($self->{tail_check}) {
79 11         30 push @subchecks, [
80             $value,
81             $self->{tail_check},
82             { check_path => [ ['tail', 'key' ] ] },
83             ];
84             } else {
85 8         61 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         66 $self->perform_subchecks(\@subchecks);
100              
101 7         36 return 1;
102             }
103              
104             1;
105              
106             __END__