blib/lib/JSON/Schema/Corvus/XS.pm

filter: Statementstmt 93.3% 98 / 105 Branchbran 75.0% 36 / 48 Conditioncond 65.0% 13 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 95.2% 20 / 21 Podpod 100% 2 / 2 totaltotal 82.7% 173 / 209 CC 31 SCAR 25.2
CC
31
Cov
85%
_options
24.3
_is_data
4.1
new
3.0
CRAP 12.4
↑ Summary XS.xs »
1 package JSON::Schema::Corvus::XS;
2  
3 4 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = 'v0.1.1';
10  
11 # The C library this binding is written against: 0.1.4 or later in the 0.1 series.
12 4 ▶use constant REQUIRED_LIBRARY_VERSION => (0 << 16) | (1 << 8) | 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # On Windows the C library is a DLL beside the share install, which must be loaded before the XS module that links it:
15 # Windows then finds it by name.
16 4 ▶if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 4 4
17 0 require Alien::CorvusJsonSchema;
18 0 ▶ if (Alien::CorvusJsonSchema->install_type eq 'share') {
Branch: 1 branch
BranchTrueFalseTotal
if ("Alien::CorvusJsonSchema"->install_type eq "share") 0 0 0
19 0 require DynaLoader;
20 0 for my $library (Alien::CorvusJsonSchema->dynamic_libs) {
21 0 ▶ DynaLoader::dl_load_file($library, 0)
Branch: 1 branch
BranchTrueFalseTotal
DynaLoader::dl_load_file($library, 0) or die "JSON::Schema::Corvus::XS: cannot load ${library}: " . DynaLoader::dl_error() . "\n" 0 0 0
22 or die "JSON::Schema::Corvus::XS: cannot load $library: " . DynaLoader::dl_error() . "\n";
23 }
24 }
25 }
26 4 require XSLoader;
27 4 XSLoader::load(__PACKAGE__, $VERSION);
28  
29 {
30 4 my $version = library_version();
31 4 ▶ if ($version < REQUIRED_LIBRARY_VERSION || ($version >> 8) != (REQUIRED_LIBRARY_VERSION >> 8)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version < 260 or $version >> 8 != 1) 0 4 4
Condition33%1/3
$version < 260 or $version >> 8 != 1
l!l&&r!l&&!r
004
MC/DC0%0/2
$version < 260 or $version >> 8 != 1
$version < 260 $version >> 8 != 1
Truth table33%1/3
A or B
A= $version < 260B= $version >> 8 != 1
ABresult
1X1
011
000
32 0 die sprintf("JSON::Schema::Corvus::XS %s needs version 0.1.4 or later in the 0.1 series of the corvus-json-schema "
33 . "C library, and has %s\n", $VERSION, library_version_string());
34 }
35 }
36  
37 4 my %DIALECTS = (
38 'draft4' => 4,
39 'draft6' => 6,
40 'draft7' => 7,
41 'draft2019-09' => 2019,
42 'draft2020-12' => 2020,
43 );
44  
45 4 my %OPTIONS = map { $_ => 1 } qw(
46 default_dialect assert_format assert_format_in_legacy_drafts assert_content formats resolve_document base_uri
47 entry_point max_depth pattern_match_limit
48 );
49  
50 5 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
51 5 my ($message) = @_;
52 5 die JSON::Schema::Corvus::XS::Error->_new($message, 1);
53 }
54  
55 # The options as the XS reads them, checked.
56 47 ▶sub _options {
Subroutine _options: called CC=24 SCAR=31.9
57 47 my (%options) = @_;
58 47 my %native;
59 47 for my $name (sort keys %options) {
60 36 ▶ _usage("unknown option $name") unless $OPTIONS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $OPTIONS{$name} 1 35 36
61 }
62 46 ▶ if (defined $options{default_dialect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"default_dialect"}) 6 40 46
63 my $dialect = $DIALECTS{ $options{default_dialect} }
64 6 ▶ // _usage("default_dialect is not one of " . join(', ', sort keys %DIALECTS));
Condition66%2/3
$DIALECTS{$options{"default_dialect"}} // _usage('default_dialect is not one of ' . join(", ", sort(keys %DIALECTS)))
l!l&&r!l&&!r
510
MC/DC0%0/2
$DIALECTS{$options{"default_dialect"}} // _usage('default_dialect is not one of ' . join(", ", sort(keys %DIALECTS)))
$DIALECTS{$options{"default_dialect"}} _usage('default_dialect is not one of ' . join(", ", sort(keys %DIALECTS)))
Truth table33%1/3
A // B
A= $DIALECTS{$options{"default_dialect"}}B= _usage('default_dialect is not one of ' . join(", ", sort(keys %DIALECTS)))
ABresult
1X1
011
000
65 5 $native{default_dialect} = $dialect;
66 }
67 45 for my $flag (qw(assert_format assert_format_in_legacy_drafts assert_content)) {
68 135 ▶ $native{$flag} = $options{$flag} ? 1 : 0 if defined $options{$flag};
Branch: 2 branches
BranchTrueFalseTotal
if defined $options{$flag} 8 127 135
$options{$flag} ? : 7 1 8
69 }
70 45 for my $text (qw(base_uri entry_point)) {
71 90 ▶ $native{$text} = "$options{$text}" if defined $options{$text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{$text} 2 88 90
72 }
73 45 ▶ if (defined $options{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"max_depth"}) 2 43 45
74 2 ▶ _usage('max_depth is not a whole number from 1') unless $options{max_depth} =~ /\A[0-9]+\z/ && $options{max_depth} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"max_depth"} =~ /\A[0-9]+\z/u and $options{"max_depth"} > 0 1 1 2
Condition66%2/3
$options{"max_depth"} =~ /\A[0-9]+\z/u and $options{"max_depth"} > 0
!ll&&!rl&&r
101
MC/DC50%1/2
$options{"max_depth"} =~ /\A[0-9]+\z/u and $options{"max_depth"} > 0
$options{"max_depth"} =~ /\A[0-9]+\z/u $options{"max_depth"} > 0
Truth table66%2/3
A and B
A= $options{"max_depth"} =~ /\A[0-9]+\z/uB= $options{"max_depth"} > 0
ABresult
0X0
100
111
75 1 $native{max_depth} = 0 + $options{max_depth};
76 }
77 44 ▶ if (exists $options{pattern_match_limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"pattern_match_limit"}) 3 41 44
78 # Seconds, as the other packages have it; undef, 0 or less turns the limit off.
79 3 my $seconds = $options{pattern_match_limit};
80 3 ▶ if (!defined $seconds || $seconds <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $seconds or $seconds <= 0) { } 2 1 3
Condition100%3/3
not defined $seconds or $seconds <= 0
l!l&&r!l&&!r
111
MC/DC100%2/2
not defined $seconds or $seconds <= 0
not defined $seconds $seconds <= 0
Truth table100%3/3
not(A) or B
A= not defined $secondsB= $seconds <= 0
ABresult
1X1
011
000
81 2 $native{pattern_match_limit_ms} = 0;
82 }
83 else {
84 1 my $ms = $seconds * 1000;
85 1 ▶ $ms = int($ms) == $ms ? int($ms) : int($ms) + 1;
Branch: 1 branch
BranchTrueFalseTotal
int $ms == $ms ? : 1 0 1
86 1 ▶ $native{pattern_match_limit_ms} = $ms < 1 ? 1 : $ms;
Branch: 1 branch
BranchTrueFalseTotal
$ms < 1 ? : 0 1 1
87 }
88 }
89 44 ▶ if (defined $options{formats}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"formats"}) 6 38 44
90 6 ▶ _usage('formats is not a hash of code references') unless ref $options{formats} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options{"formats"} eq "HASH" 0 6 6
91 6 for my $name (keys %{ $options{formats} }) {
92 6 ▶ _usage("format $name is not a code reference") unless ref $options{formats}{$name} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options{"formats"}{$name} eq "CODE" 1 5 6
93 }
94 5 $native{formats} = { %{ $options{formats} } };
95 }
96 43 ▶ if (defined $options{resolve_document}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"resolve_document"}) 7 36 43
97 7 ▶ _usage('resolve_document is not a code reference') unless ref $options{resolve_document} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options{"resolve_document"} eq "CODE" 0 7 7
98 7 $native{resolve_document} = $options{resolve_document};
99 }
100 43 return \%native;
101 }
102  
103 41 ▶sub _is_data {
Subroutine _is_data: called CC=4 SCAR=14.2
104 41 my ($value) = @_;
105 41 ▶ return 1 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 7 34 41
106 4 ▶ return 1 if defined &builtin::is_bool && do { no warnings; builtin::is_bool($value) };
Subroutine BEGIN: called CC=1 SCAR=0.3
Branch: 1 branch
BranchTrueFalseTotal
if defined &builtin::is_bool and do { no warnings; builtin::is_bool($value) } 0 34 34
Condition33%1/3
defined &builtin::is_bool and do { no warnings; builtin::is_bool($value) }
!ll&&!rl&&r
0340
MC/DC0%0/2
defined &builtin::is_bool and do { no warnings; builtin::is_bool($value) }
defined &builtin::is_bool do { no warnings; builtin::is_bool($value) }
Truth table33%1/3
A and B
A= defined &builtin::is_boolB= do { no warnings; builtin::is_bool($value) }
ABresult
0X0
100
111
107 34 return 0;
108 }
109  
110 45 ▶sub compile {
Subroutine compile: called CC=2 SCAR=6.9
Pod documented
111 45 my ($class, $schema, %options) = @_;
112 45 my $native = _options(%options);
113 41 ▶ return _is_data($schema) ? _compile_data($schema, $native) : _compile_text($schema, $native);
Branch: 1 branch
BranchTrueFalseTotal
_is_data($schema) ? : 7 34 41
114 }
115  
116 2 ▶sub compile_uri {
Subroutine compile_uri: called CC=2 SCAR=7.1
Pod documented
117 2 my ($class, $uri, %options) = @_;
118 2 ▶ _usage('the URI is undef') unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 2 2
119 2 return _compile_uri("$uri", _options(%options));
120 }
121  
122 package JSON::Schema::Corvus::XS::ResultsCollector;
123  
124 4 my %LEVELS = (basic => 0, detailed => 1, verbose => 2);
125  
126 6 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
127 6 my ($class, $level) = @_;
128 6 ▶ $level //= 'detailed';
Condition100%2/2
$level //= "detailed"
l!l
51
MC/DC100%1/1
$level //= "detailed"
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
129 6 ▶ my $code = $LEVELS{ lc $level }
Condition66%2/3
$LEVELS{lc $level} // JSON::Schema::Corvus::XS::_usage("the level is not basic, detailed or verbose")
l!l&&r!l&&!r
510
MC/DC0%0/2
$LEVELS{lc $level} // JSON::Schema::Corvus::XS::_usage("the level is not basic, detailed or verbose")
$LEVELS{lc $level} JSON::Schema::Corvus::XS::_usage("the level is not basic, detailed or verbose")
Truth table33%1/3
A // B
A= $LEVELS{lc $level}B= JSON::Schema::Corvus::XS::_usage("the level is not basic, detailed or verbose")
ABresult
1X1
011
000
130 // JSON::Schema::Corvus::XS::_usage("the level is not basic, detailed or verbose");
131 5 return $class->_new($code);
132 }
133  
134 1 ▶sub annotations {
Subroutine annotations: called CC=1 SCAR=0.0
135 1 my ($self) = @_;
136 1 require JSON::PP;
137 1 return JSON::PP->new->decode($self->annotations_json);
138 }
139  
140 package JSON::Schema::Corvus::XS::Error;
141  
142 4 ▶use overload '""' => sub { $_[0]{message} }, 'bool' => sub { 1 }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
143  
144 5 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
145 5 my ($class, $message, $status) = @_;
146 5 return bless { message => $message, status => $status }, $class;
147 }
148  
149 4 ▶sub message { $_[0]{message} }
Subroutine message: called CC=1 SCAR=0.0
150 0 ▶sub status { $_[0]{status} }
Subroutine status: not called CC=1 SCAR=6.9
151  
152 package JSON::Schema::Corvus::XS::Error::InvalidJson;
153 4 our @ISA = ('JSON::Schema::Corvus::XS::Error');
154 2 ▶sub offset { $_[0]{offset} }
Subroutine offset: called CC=1 SCAR=0.0
155  
156 package JSON::Schema::Corvus::XS::Error::Compilation;
157 4 our @ISA = ('JSON::Schema::Corvus::XS::Error');
158  
159 package JSON::Schema::Corvus::XS::Error::PatternTooLarge;
160 4 our @ISA = ('JSON::Schema::Corvus::XS::Error::Compilation');
161  
162 package JSON::Schema::Corvus::XS::Error::Depth;
163 4 our @ISA = ('JSON::Schema::Corvus::XS::Error');
164  
165 package JSON::Schema::Corvus::XS::Error::PatternMatchLimit;
166 4 our @ISA = ('JSON::Schema::Corvus::XS::Error');
167  
168 4 1;
169  
170 __END__
↑ Summary XS.xs »