Conformance/Check/DataSchema.pm

filter: Statementstmt 47.0% 16 / 34 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 55.5% 5 / 9 Podpod 100% 4 / 4 totaltotal 39.0% 25 / 64 CC 8 SCAR 23.0
CC
8
Cov
33%
check
20.0
from_config
12.0
file_format
12.0
CRAP 9.9
« Conformance/Snapshot/Tree.pm ↑ Summary Conformance/Snapshot/Directory.pm »
1 package Dist::Conformance::Check::DataSchema 0.006;
2  
3 # ABSTRACT: A JSON, YAML or INI file parses and matches a schema
4  
5 1 ▶use 5.040;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::Base 'Dist::Conformance::Check', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Dist::Conformance::Schema();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 my %PARSER = (
14 json => \&Dist::Conformance::Schema::parse_json,
15 yaml => \&Dist::Conformance::Schema::parse_yaml,
16 ini => \&Dist::Conformance::Schema::parse_ini,
17 );
18  
19 1 my %FORMAT_OF_EXTENSION = ( json => 'json', yml => 'yaml', yaml => 'yaml', ini => 'ini' );
20  
21 0 ▶sub config_schema ($) {
Subroutine config_schema: not called CC=1 SCAR=6.9
Pod documented
22 return {
23 0 required => [qw{file schema}],
24 properties => {
25 file => $Dist::Conformance::Schema::PATH,
26 schema => { type => 'object' },
27 format => { type => 'string', enum => [ sort keys %PARSER ] },
28 missing => { %$Dist::Conformance::Schema::STATUS, default => 'info' },
29 },
30 };
31 }
32  
33  
34 0 ▶sub from_config ( $class, %arguments ) {
Subroutine from_config: not called CC=3 SCAR=24.8
Pod documented
35 0 ▶ my $where = $arguments{where} // "the check $arguments{name}";
Condition0%0/2
$arguments{"where"} // qq[the check $arguments{"name"}]
l!l
00
MC/DC0%0/1
$arguments{"where"} // qq[the check $arguments{"name"}]
$arguments{"where"}
Truth table0%0/2
A
A= $arguments{"where"}
Aresult
11
00
36 0 my $self = $class->SUPER::from_config(%arguments);
37 0 my $file = $self->config->{file};
38 0 ▶ die "$where is not valid:\n /format: $file does not end in .json, .yml, .yaml or .ini, so give json, yaml or ini\n" if !defined $self->file_format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->file_format 0 0 0
39 0 return $self;
40 }
41  
42  
43 0 ▶sub file_format ($self) {
Subroutine file_format: not called CC=3 SCAR=24.8
Pod documented
44 0 my $config = $self->config;
45 0 ▶ return $config->{format} if defined $config->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"format"} 0 0 0
46 0 my ($extension) = $config->{file} =~ m/[.]([^.\/]+)\z/;
47 0 ▶ return defined $extension ? $FORMAT_OF_EXTENSION{ lc $extension } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $extension ? : 0 0 0
48 }
49  
50  
51 0 ▶sub check ( $self, $snapshot ) {
Subroutine check: not called CC=4 SCAR=30.0
Pod documented
52 0 my ( $file, $schema ) = @{ $self->config }{qw{file schema}};
53 0 ▶ my $text = $snapshot->content($file) // return ( $self->config->{missing}, "there is no $file" );
Condition0%0/2
$snapshot->content($file) // (return $self->config->{"missing"}, "there is no $file")
l!l
00
MC/DC0%0/1
$snapshot->content($file) // (return $self->config->{"missing"}, "there is no $file")
$snapshot->content($file)
Truth table0%0/2
A
A= $snapshot->content($file)
Aresult
11
00
54 0 ▶ my $data = eval { $PARSER{ $self->file_format }->( $text, $file ) } // return ( 'fail', Dist::Conformance::Schema::first_line($@) );
Condition0%0/2
eval { do { $PARSER{$self->file_format}($text, $file) } } // (return "fail", Dist::Conformance::Schema::first_line($@))
l!l
00
MC/DC0%0/1
eval { do { $PARSER{$self->file_format}($text, $file) } } // (return "fail", Dist::Conformance::Schema::first_line($@))
eval { do { $PARSER{$self->file_format}($text, $file) } }
Truth table0%0/2
A
A= eval { do { $PARSER{$self->file_format}($text, $file) } }
Aresult
11
00
55 0 my @errors = Dist::Conformance::Schema::errors( $data, $schema );
56 0 ▶ return ( 'fail', "$file does not match the schema: " . join '; ', @errors ) if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
57 0 return ( 'pass', 'valid' );
58 }
59  
60 __END__
« Conformance/Snapshot/Tree.pm ↑ Summary Conformance/Snapshot/Directory.pm »