REST/Forms.pm

filter: Statementstmt 58.2% 81 / 139 Branchbran 41.8% 31 / 74 Conditioncond 35.9% 14 / 39 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 77.7% 7 / 9 Podpod 100% 5 / 5 totaltotal 49.1% 142 / 289 CC 57 SCAR 43.2
CC
57
Cov
50%
form_parse
99.2
form_compose
90.2
expand_list
12.0
CRAP 75.5
« REST/Exception.pm ↑ Summary REST/Object.pm »
1 #!perl
2 # PODNAME: RT::Client::REST::Forms
3 # ABSTRACT: This package provides functions from RT::Interface::REST, because we don't want to depend on rt being installed. Derived from rt 3.4.5.
4  
5 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package RT::Client::REST::Forms;
9 28 $RT::Client::REST::Forms::VERSION = '0.73';
10 28 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 28 ▶use vars qw(@EXPORT @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 28 @ISA = qw(Exporter);
15 28 @EXPORT = qw(expand_list form_parse form_compose vpush vsplit);
16  
17 28 my $CF_name = q%[#\s\w:()?/-]+%;
18 28 my $field = qr/[a-z][\w-]*|C(?:ustom)?F(?:ield)?-$CF_name|CF\.\{$CF_name}/i;
19 # always 9 https://rt-wiki.bestpractical.com/wiki/REST#Ticket_Attachments
20 28 my $spaces = ' ' x 9;
21  
22  
23 0 ▶sub expand_list {
Subroutine expand_list: not called CC=3 SCAR=24.8
Pod documented
24 0 my ($list) = @_;
25 0 my (@elts, %elts);
26  
27 0 for my $elt (split /,/, $list) {
28 0 ▶ if ($elt =~ m/^(\d+)-(\d+)$/) { push @elts, ($1..$2) }
Branch: 1 branch
BranchTrueFalseTotal
if ($elt =~ /^(\d+)-(\d+)$/) { } 0 0 0
29 0 else { push @elts, $elt }
30 }
31  
32 0 @elts{@elts}=();
33 0 my @return = sort {$a<=>$b} keys %elts;
34 return @return
35 0 }
36  
37  
38 9 ▶sub form_parse {
Subroutine form_parse: called CC=29 SCAR=46.0
Pod documented
39 9 my @lines = split /(?<=\n)/, shift;
40 9 my $state = 0;
41 9 my @forms = ();
42 9 my ($c, $o, $k, $e) = ('', [], {}, '');
43  
44 LINE:
45 9 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 121 9 130
46 121 my $line = shift @lines;
47  
48 121 ▶ next LINE if $line eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "\n" 2 119 121
49  
50 119 ▶ if ($line eq "--\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "--\n") 0 119 119
51 # We reached the end of one form. We'll ignore it if it was
52 # empty, and store it otherwise, errors and all.
53 0 ▶ if ($e || $c || @$o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e or $c or @$o) 0 0 0
Condition0%0/6
$e or $c or @$o
l!l&&r!l&&!r
000
$e or $c
l!l&&r!l&&!r
000
MC/DC0%0/3
$e or $c or @$o
$e $c @$o
Truth table0%0/4
A or B or C
A= $eB= $cC= @$o
ABCresult
1XX1
01X1
0011
0000
54 0 push @forms, [ $c, $o, $k, $e ];
55 0 $c = ''; $o = []; $k = {}; $e = '';
56 }
57 0 $state = 0;
58 next LINE
59 0 }
60  
61 119 ▶ if ($state != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state != -1) { } 119 0 119
62  
63 119 ▶ if ($state == 0 && $line =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 0 and $line =~ /^#/) 0 119 119
Condition66%2/3
$state == 0 and $line =~ /^#/
!ll&&!rl&&r
11090
MC/DC0%0/2
$state == 0 and $line =~ /^#/
$state == 0 $line =~ /^#/
Truth table66%2/3
A and B
A= $state == 0B= $line =~ /^#/
ABresult
0X0
100
111
64 # Read an optional block of comments (only) at the start
65 # of the form.
66 0 $state = 1;
67 0 $c = $line;
68 0 ▶ while (@lines && $lines[0] =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $lines[0] =~ /^#/) 0 0 0
Condition0%0/3
@lines and $lines[0] =~ /^#/
!ll&&!rl&&r
000
MC/DC0%0/2
@lines and $lines[0] =~ /^#/
@lines $lines[0] =~ /^#/
Truth table0%0/3
A and B
A= @linesB= $lines[0] =~ /^#/
ABresult
0X0
100
111
69 0 $c .= shift @lines;
70 }
71 next LINE
72 0 }
73  
74 119 ▶ if ($state <= 1 && $line =~ m/^($field:) ?$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state <= 1 and $line =~ /^($field:) ?$/s) 15 104 119
Condition66%2/3
$state <= 1 and $line =~ /^($field:) ?$/s
!ll&&!rl&&r
010415
MC/DC50%1/2
$state <= 1 and $line =~ /^($field:) ?$/s
$state <= 1 $line =~ /^($field:) ?$/s
Truth table66%2/3
A and B
A= $state <= 1B= $line =~ /^($field:) ?$/s
ABresult
0X0
100
111
75 # Empty field
76 15 my $f = $1;
77 15 $f =~ s/:?$//;
78  
79 15 ▶ push(@$o, $f) unless exists $k->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $k->{$f} 15 0 15
80 15 vpush($k, $f, undef);
81  
82 15 $state = 1;
83  
84 next LINE
85 15 }
86  
87 104 ▶ if ($state <= 1 && $line =~ m/^($field:) (.*)?$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state <= 1 and $line =~ /^($field:) (.*)?$/s) 104 0 104
Condition33%1/3
$state <= 1 and $line =~ /^($field:) (.*)?$/s
!ll&&!rl&&r
00104
MC/DC0%0/2
$state <= 1 and $line =~ /^($field:) (.*)?$/s
$state <= 1 $line =~ /^($field:) (.*)?$/s
Truth table33%1/3
A and B
A= $state <= 1B= $line =~ /^($field:) (.*)?$/s
ABresult
0X0
100
111
88 # Read a field: value specification.
89 104 my $f = $1;
90 104 my $value = $2;
91 104 $f =~ s/:?$//;
92  
93 # Read continuation lines, if any.
94 104 ▶ while (@lines && ($lines[0] eq "\n" || $lines[0] =~ m/^ +/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $lines[0] eq "\n" || $lines[0] =~ /^ +/) 216 104 320
Condition100%6/6
@lines and $lines[0] eq "\n" || $lines[0] =~ /^ +/
!ll&&!rl&&r
896216
$lines[0] eq "\n" or $lines[0] =~ /^ +/
l!l&&r!l&&!r
3318396
MC/DC100%3/3
@lines and $lines[0] eq "\n" || $lines[0] =~ /^ +/
@lines $lines[0] eq "\n" $lines[0] =~ /^ +/
Truth table100%4/4
A and B or C
A= @linesB= $lines[0] eq "\n"C= $lines[0] =~ /^ +/
ABCresult
0XX0
1000
11X1
1011
95 216 my $l = shift @lines;
96 216 $l =~ s/^$spaces//;
97 216 $value .= $l
98 }
99  
100 # `Content` is always supposed to be followed by three new lines
101 # ... but this doesnt behave as documented
102 # https://rt-wiki.bestpractical.com/wiki/REST#Ticket_Attachments
103 104 ▶ if ($f eq 'Content') {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq 'Content') { } 8 96 104
104 8 $value =~ s/\n\n\n?$//g
105 }
106 # Chomp everything else
107 else {
108 96 chomp $value
109 }
110  
111 104 ▶ push(@$o, $f) unless exists $k->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $k->{$f} 104 0 104
112 104 vpush($k, $f, $value);
113  
114 104 $state = 1;
115  
116 next LINE
117 104 }
118  
119 0 ▶ if ($line !~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^#/) 0 0 0
120 # We've found a syntax error, so we'll reconstruct the
121 # form parsed thus far, and add an error marker. (>>)
122 0 $state = -1;
123 0 $e = form_compose([[ '', $o, $k, '' ]]);
124 0 ▶ $e.= $line =~ m/^>>/ ? "$line\n" : ">> $line\n";
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^>>/ ? : 0 0 0
125 next LINE
126 0 }
127  
128 # line will be ignored
129 }
130 else {
131 # We saw a syntax error earlier, so we'll accumulate the
132 # contents of this form until the end.
133 0 $e .= "$line\n";
134 }
135 }
136 9 ▶ push(@forms, [ $c, $o, $k, $e ]) if ($e || $c || @$o);
Branch: 1 branch
BranchTrueFalseTotal
if $e or $c or @$o 9 0 9
Condition33%2/6
$e or $c or @$o
l!l&&r!l&&!r
090
$e or $c
l!l&&r!l&&!r
009
MC/DC0%0/3
$e or $c or @$o
$e $c @$o
Truth table25%1/4
A or B or C
A= $eB= $cC= @$o
ABCresult
1XX1
01X1
0011
0000
137  
138 9 for my $l (keys %$k) {
139 119 ▶ $k->{$l} = vsplit($k->{$l}) if (ref $k->{$l} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $k->{$l} eq "ARRAY" 0 119 119
140 }
141  
142 9 return \@forms;
143 }
144  
145  
146 6 ▶sub form_compose {
Subroutine form_compose: called CC=21 SCAR=45.0
Pod documented
147 6 my ($forms) = @_;
148 6 my @text;
149  
150 6 for my $form (@$forms) {
151 6 my ($c, $o, $k, $e) = @$form;
152 6 my $text = '';
153  
154 6 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 0 6 6
155 0 $c =~ s/\n*$/\n/;
156 0 $text = "$c\n";
157 }
158 6 ▶ if ($e) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e) { } 0 6 6
elsif ($o) { } 6 0 6
159 0 $text .= $e;
160 }
161 elsif ($o) {
162 6 my @lines;
163  
164 6 for my $key (@$o) {
165 10 my ($line, $sp);
166 my @values = (ref $k->{$key} eq 'ARRAY') ?
167 0 @{ $k->{$key} } :
168 10 ▶ $k->{$key};
Branch: 1 branch
BranchTrueFalseTotal
ref $k->{$key} eq 'ARRAY' ? : 0 10 10
169  
170 10 $sp = " "x(length("$key: "));
171 10 ▶ $sp = " "x4 if length($sp) > 16;
Branch: 1 branch
BranchTrueFalseTotal
if length $sp > 16 0 10 10
172  
173 10 for my $v (@values) {
174 10 ▶ if ($v =~ /\n/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v =~ /\n/) { } 0 10 10
elsif ($line and length($line) + length($v) - rindex($line, "\n") >= 70) { } 0 10 10
175 0 $v =~ s/^/$sp/gm;
176 0 $v =~ s/^$sp//;
177  
178 0 ▶ if ($line) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line) { } 0 0 0
elsif (@lines and not $lines[-1] =~ /\n\n$/) { } 0 0 0
179 0 push @lines, "$line\n\n";
180 0 $line = '';
181 }
182 ▶ elsif (@lines && $lines[-1] !~ m/\n\n$/) {
Condition0%0/3
@lines and not $lines[-1] =~ /\n\n$/
!ll&&!rl&&r
000
MC/DC0%0/2
@lines and not $lines[-1] =~ /\n\n$/
@lines not $lines[-1] =~ /\n\n$/
Truth table0%0/3
A and not(B)
A= @linesB= not $lines[-1] =~ /\n\n$/
ABresult
0X0
100
111
183 0 $lines[-1] .= "\n";
184 }
185 0 push @lines, "$key: $v\n\n";
186 }
187 ▶ elsif ($line &&
Condition33%1/3
$line and length($line) + length($v) - rindex($line, "\n") >= 70
!ll&&!rl&&r
1000
MC/DC0%0/2
$line and length($line) + length($v) - rindex($line, "\n") >= 70
$line length($line) + length($v) - rindex($line, "\n") >= 70
Truth table33%1/3
A and B
A= $lineB= length($line) + length($v) - rindex($line, "\n") >= 70
ABresult
0X0
100
111
188 length($line)+length($v)-rindex($line, "\n") >= 70)
189 {
190 0 $line .= ",\n$sp$v";
191 }
192 else {
193 10 ▶ $line = $line ? "$line, $v" : "$key: $v";
Branch: 1 branch
BranchTrueFalseTotal
$line ? : 0 10 10
194 }
195 }
196  
197 10 ▶ $line = "$key:" unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 10 10
198 10 ▶ if ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 10 0 10
199 10 ▶ if ($line =~ m/\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\n/) 0 10 10
200 0 ▶ if (@lines && $lines[-1] !~ m/\n\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and not $lines[-1] =~ /\n\n$/) 0 0 0
Condition0%0/3
@lines and not $lines[-1] =~ /\n\n$/
!ll&&!rl&&r
000
MC/DC0%0/2
@lines and not $lines[-1] =~ /\n\n$/
@lines not $lines[-1] =~ /\n\n$/
Truth table0%0/3
A and not(B)
A= @linesB= not $lines[-1] =~ /\n\n$/
ABresult
0X0
100
111
201 0 $lines[-1] .= "\n";
202 }
203 0 $line .= "\n";
204 }
205 10 push @lines, "$line\n";
206 }
207 }
208  
209 6 $text .= join '', @lines;
210 }
211 else {
212 0 chomp $text;
213 }
214 6 push @text, $text;
215 }
216  
217 6 return join "\n--\n\n", @text;
218 }
219  
220  
221 119 ▶sub vpush {
Subroutine vpush: called CC=5 SCAR=24.7
Pod documented
222 119 my ($hash, $key, $val) = @_;
223 119 ▶ my @val = ref $val eq 'ARRAY' ? @$val : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 119 119
224  
225 119 ▶ if (exists $hash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$key}) { } 0 119 119
226 0 ▶ unless (ref $hash->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash->{$key} eq "ARRAY") 0 0 0
227 0 ▶ my @v = $hash->{$key} ne '' ? $hash->{$key} : ();
Branch: 1 branch
BranchTrueFalseTotal
$hash->{$key} ne '' ? : 0 0 0
228 0 $hash->{$key} = \@v;
229 }
230 0 push @{ $hash->{$key} }, @val;
231 }
232 else {
233 119 $hash->{$key} = $val;
234 }
235 }
236  
237  
238 0 ▶sub vsplit {
Subroutine vsplit: not called CC=3 SCAR=24.8
Pod documented
239 0 my ($val) = @_;
240 0 my (@words);
241  
242 0 ▶ for my $line (map {split /\n/} (ref $val eq 'ARRAY') ? @$val : $val)
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
243 {
244 # XXX: This should become a real parser, à la Text::ParseWords.
245 0 $line =~ s/^\s+//;
246 0 $line =~ s/\s+$//;
247 0 push @words, split /\s*,\s*/, $line;
248 }
249  
250 0 return \@words;
251 }
252  
253 __END__
« REST/Exception.pm ↑ Summary REST/Object.pm »