lib/ClarID/Tools/Util.pm

filter: Statementstmt 100% 48 / 48 Branchbran 57.1% 8 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 83.7% 67 / 80 CC 9 SCAR 10.0
CC
9
Cov
88%
assert_supported_codebook_version
4.1
load_json_file
4.1
load_yaml_file
3.1
CRAP 2.7
« lib/ClarID/Tools/Validator.pm ↑ Summary lib/ClarID/Tools/Command/validate.pm »
1 package ClarID::Tools::Util;
2  
3 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use YAML::XS qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use JSON::XS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use ClarID::Tools ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 our @EXPORT_OK = qw( load_yaml_file load_json_file assert_supported_codebook_version );
11  
12 # Load a YAML file, ensure it’s a HASHREF
13 55 ▶sub load_yaml_file {
Subroutine load_yaml_file: called CC=3 SCAR=11.2
Pod undocumented
14 55 my ($file) = @_;
15 55 my $data = eval { LoadFile($file) };
16 55 ▶ croak "Error loading YAML file '$file': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 55 55
17 55 ▶ croak "Expected a HASH in '$file'" unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 55 55
18 55 assert_supported_codebook_version( $data, $file );
19 53 return $data;
20 }
21  
22 55 ▶sub assert_supported_codebook_version {
Subroutine assert_supported_codebook_version: called CC=4 SCAR=14.2
Pod undocumented
23 55 my ( $data, $file ) = @_;
24  
25 55 my $version = $data->{metadata}{version};
26 55 ▶ croak "Missing metadata.version in codebook '$file'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and $version ne "" 0 55 55
Condition33%1/3
defined $version and $version ne ""
!ll&&!rl&&r
0055
MC/DC0%0/2
defined $version and $version ne ""
defined $version $version ne ""
Truth table33%1/3
A and B
A= defined $versionB= $version ne ""
ABresult
0X0
100
111
27 unless defined $version && $version ne '';
28  
29 my %supported =
30 55 map { $_ => 1 } @ClarID::Tools::SUPPORTED_CODEBOOK_VERSIONS;
31  
32 55 ▶ return 1 if $supported{$version};
Branch: 1 branch
BranchTrueFalseTotal
if $supported{$version} 53 2 55
33  
34 2 my $supported = join ', ', @ClarID::Tools::SUPPORTED_CODEBOOK_VERSIONS;
35 2 croak
36 "Unsupported codebook version '$version' in '$file'. This ClarID-Tools release supports: $supported";
37 }
38  
39 # Load a JSON file, ensure it’s a HASHREF
40 48 ▶sub load_json_file {
Subroutine load_json_file: called CC=4 SCAR=14.1
Pod undocumented
41 48 my ($file) = @_;
42 48 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Error opening JSON file '${file}': $!") 48 0 48
43 or croak "Error opening JSON file '$file': $!";
44 48 local $/;
45 48 my $text = <$fh>;
46 48 close $fh;
47 48 my $data = eval { decode_json($text) };
48 48 ▶ croak "Error parsing JSON in '$file': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 48 48
49 48 ▶ croak "Expected a HASH in '$file'" unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 48 48
50 48 return $data;
51 }
52  
53 55 1;
« lib/ClarID/Tools/Validator.pm ↑ Summary lib/ClarID/Tools/Command/validate.pm »