| 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
Condition33%1/3
not $toon_text or $toon_text =~ /^\s*$/u
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
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | my @lines = split /\r?\n/, $toon_text; | |||||||||||||||||||||||||||
| 17 | 3 | ▶ | pop @lines if @lines && $lines[-1] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@lines and $lines[-1] eq ""
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 ""
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Find first non-empty line | ||||||||||||||||||||||||||||
| 20 | 3 | ▶ | my @non_empty = grep { $_ && $_ !~ /^\s*$/ } @lines; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_ && !($_ =~ /^\s*$/u)
MC/DC0%0/2
$_ && !($_ =~ /^\s*$/u)
$_
!($_ =~ /^\s*$/u)
Truth table33%1/3
A && not(B)
A= $_B= !($_ =~ /^\s*$/u)
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Empty document is valid | ||||||||||||||||||||||||||||
| 23 | 3 | ▶ | return 1 if !@non_empty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Validate each line | ||||||||||||||||||||||||||||
| 26 | 3 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 27 | 6 | ▶ | next if !$line || $line =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $line or $line =~ /^\s*$/u
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
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Check for valid key-value or header | ||||||||||||||||||||||||||||
| 30 | 6 | ▶ | if (!$self->_is_valid_line($line)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | return 1 if $trimmed =~ /^[\w"]+\s*\[\d+\]/; # key[N]... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | return 1 if $trimmed =~ /^-\s/; # - item | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | return 1 if $trimmed =~ /^\[\d+\]/; # [N]: ... (root array) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | return 0; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 10 | 1; | |||||||||||||||||||||||||||