TOON/Validator.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran 59.0% 13 / 22 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 65.0% 54 / 83 CC 17 SCAR 25.1
CC
17
Cov
72%
validate
19.4
_is_valid_line
7.6
BEGIN
1.0
CRAP 12.3
« TOON/Encoder.pm ↑ Summary TOON.pm »
1 package Data::TOON::Validator;
2 10 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
7 3 my ($class) = @_;
8 3 return bless {}, $class;
9 }
10  
11 3 ▶sub validate {
Subroutine validate: called CC=11 SCAR=29.7
Pod undocumented
12 3 my ($self, $toon_text) = @_;
13
14 3 ▶ return 1 if !$toon_text || $toon_text =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not $toon_text or $toon_text =~ /^\s*$/u 0 3 3
Condition33%1/3
not $toon_text or $toon_text =~ /^\s*$/u
l!l&&r!l&&!r
003
MC/DC0%0/2
not $toon_text or $toon_text =~ /^\s*$/u
not $toon_text $toon_text =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not $toon_textB= $toon_text =~ /^\s*$/u
ABresult
1X1
011
000
15
16 3 my @lines = split /\r?\n/, $toon_text;
17 3 ▶ pop @lines if @lines && $lines[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @lines and $lines[-1] eq "" 0 3 3
Condition33%1/3
@lines and $lines[-1] eq ""
!ll&&!rl&&r
030
MC/DC0%0/2
@lines and $lines[-1] eq ""
@lines $lines[-1] eq ""
Truth table33%1/3
A and B
A= @linesB= $lines[-1] eq ""
ABresult
0X0
100
111
18
19 # Find first non-empty line
20 3 ▶ my @non_empty = grep { $_ && $_ !~ /^\s*$/ } @lines;
Condition33%1/3
$_ && !($_ =~ /^\s*$/u)
!ll&&!rl&&r
006
MC/DC0%0/2
$_ && !($_ =~ /^\s*$/u)
$_ !($_ =~ /^\s*$/u)
Truth table33%1/3
A && not(B)
A= $_B= !($_ =~ /^\s*$/u)
ABresult
0X0
100
111
21
22 # Empty document is valid
23 3 ▶ return 1 if !@non_empty;
Branch: 1 branch
BranchTrueFalseTotal
unless @non_empty 0 3 3
24
25 # Validate each line
26 3 foreach my $line (@lines) {
27 6 ▶ next if !$line || $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not $line or $line =~ /^\s*$/u 0 6 6
Condition33%1/3
not $line or $line =~ /^\s*$/u
l!l&&r!l&&!r
006
MC/DC0%0/2
not $line or $line =~ /^\s*$/u
not $line $line =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not $lineB= $line =~ /^\s*$/u
ABresult
1X1
011
000
28
29 # Check for valid key-value or header
30 6 ▶ if (!$self->_is_valid_line($line)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_valid_line($line)) 0 6 6
31 0 return 0;
32 }
33 }
34
35 3 return 1;
36 }
37  
38 6 ▶sub _is_valid_line {
Subroutine _is_valid_line: called CC=7 SCAR=20.2
39 6 my ($self, $line) = @_;
40
41 # Remove leading whitespace to check depth
42 6 my $trimmed = $line;
43 6 $trimmed =~ s/^ +//;
44
45 # Empty line is valid
46 6 ▶ return 1 if !$trimmed;
Branch: 1 branch
BranchTrueFalseTotal
unless $trimmed 0 6 6
47
48 # Valid patterns:
49 # 1. key: value
50 # 2. key[N]: ...
51 # 3. key[N]{fields}: ...
52 # 4. - value (list item)
53 # 5. [N]: ... (root array)
54 # 6. CSV row (anything else that looks like values)
55
56 6 ▶ return 1 if $trimmed =~ /^[\w"]+\s*:/; # key: ...
Branch: 1 branch
BranchTrueFalseTotal
if $trimmed =~ /^[\w"]+\s*:/u 2 4 6
57 4 ▶ return 1 if $trimmed =~ /^[\w"]+\s*\[\d+\]/; # key[N]...
Branch: 1 branch
BranchTrueFalseTotal
if $trimmed =~ /^[\w"]+\s*\[\d+\]/u 2 2 4
58 2 ▶ return 1 if $trimmed =~ /^-\s/; # - item
Branch: 1 branch
BranchTrueFalseTotal
if $trimmed =~ /^-\s/u 0 2 2
59 2 ▶ return 1 if $trimmed =~ /^\[\d+\]/; # [N]: ... (root array)
Branch: 1 branch
BranchTrueFalseTotal
if $trimmed =~ /^\[\d+\]/u 0 2 2
60
61 # If the line has content (looks like a CSV row or data line), it's probably valid
62 # This is a permissive approach - stricter validation would require context
63 2 ▶ return 1 if $trimmed =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $trimmed =~ /\S/u 2 0 2
64
65 0 return 0;
66 }
67  
68 10 1;
« TOON/Encoder.pm ↑ Summary TOON.pm »