blib/lib/JMAP/Validation/Tests/String.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 75 SCAR 34.0
CC
5
Cov
0%
is_array_of_ids
12.0
is_date
12.0
is_string
2.0
CRAP 30.0
1 package JMAP::Validation::Tests::String;
2  
3 use strict;
4 use warnings;
5  
6 use DateTime;
7 use Encode;
8 use JMAP::Validation::Tests::Array;
9  
10 # data types {{{
11  
12 sub is_string {
13 my ($value) = @_;
14  
15 return (ref($value) || '') eq 'JSON::Typist::String';
16 }
17  
18 # }}}
19  
20 # restrictions {{{
21  
22 sub has_at_least_one_character {
23 my ($value) = @_;
24  
25 return length($value) >= 1;
26 }
27  
28 sub has_at_most_256_bytes {
29 my ($value) = @_;
30  
31 return length(Encode::encode_utf8($value)) <= 256;
32 }
33  
34 sub has_no_leading_hash {
35 my ($value) = @_;
36  
37 return $value !~ /^#/;
38 }
39  
40 sub is_id {
41 my ($value) = @_;
42  
43 return
44 is_string($value)
45 && has_at_least_one_character($value)
46 && has_at_most_256_bytes($value)
47 && has_no_leading_hash($value);
48 }
49  
50 sub is_array_of_ids {
51 my ($value) = @_;
52  
53 return unless JMAP::Validation::Tests::Array::is_array($value);
54  
55 foreach my $id (@{$value}) {
56 return unless is_id($id);
57 }
58  
59 return 1;
60 }
61  
62 sub is_date {
63 my ($value) = @_;
64  
65 return is_string($value);
66 return unless $value =~ m{^(\d\d\d\d)-(\d\d)-(\d\d)$};
67  
68 return unless eval {
69 DateTime->new(
70 year => $1,
71 month => $2,
72 day => $3,
73 )
74 };
75  
76 return 1;
77 }
78  
79 # }}}
80  
81 1;