Quiq/Assert.pm

filter: Statementstmt 87.3% 76 / 87 Branchbran 56.2% 27 / 48 Conditioncond 80.0% 24 / 30 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 93.3% 14 / 15 Podpod 100% 7 / 7 totaltotal 79.3% 161 / 203 CC 35 SCAR 23.3
CC
35
Cov
77%
pathExists
42.0
isDate
7.3
isNumber
7.3
CRAP 10.2
« Quiq/MediaWiki/Markup.pm ↑ Summary Quiq/Formatter.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Assert - Zusicherungen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::Assert;
16
17 my $a = Quiq::Assert->new;
18
19 # Universeller Test (hier: Prüfe, ob Wert einen Punkt (.) enthält)
20 $a->check('a.b',sub {index(shift,'.') >= 0});
21
22 # Prüfe, ob Wert in Aufzählung vorkommt
23 $a->isEnumValue('Birne',['Apfel','Birne','Pflaume']);
24
25 # Prüfe, ob Wert nicht leer ist
26 $a->isNotNull('xyz');
27
28 # Prüfe, ob Wert eine Zahl ist
29 $a->isNumber(3.14159);
30  
31 =head1 DESCRIPTION
32  
33 Die Klasse stellt Methoden zur Verfügung, mit denen eine Eingenschaft
34 eines Werts geprüft werden kann. Im Erfolgsfall kehrt die jeweilige
35 Methode zurück, im Fehlerfall wirft sie eine Exception.
36  
37 =head1 EXAMPLE
38  
39 Test von der Kommandozeile aus:
40  
41 $ perl -MQuiq::Assert -E 'Quiq::Assert->new->isNotNull("",-name=>"x")'
42 Exception:
43 ASSERT-00002: Value is null
44 Name:
45 x
46 Stacktrace:
47 Quiq::Assert::isNotNull() [+1 -e]
48 Quiq::Object::throw() [+210 .../Quiq/Assert.pm]
49 Quiq::Stacktrace::asString() [+425 .../Quiq/Object.pm]
50  
51 =cut
52  
53 # -----------------------------------------------------------------------------
54  
55 package Quiq::Assert;
56 270 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 270 our $VERSION = '1.240';
63  
64 270 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 270 ▶use Quiq::Time;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 270 ▶use Quiq::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 # -----------------------------------------------------------------------------
69  
70 =head1 METHODS
71  
72 =head2 Konstruktor
73  
74 =head3 new() - Instantiiere Objekt
75  
76 =head4 Synopsis
77  
78 $a = $class->new(@att);
79  
80 =head4 Arguments
81  
82 =over 4
83  
84 =item @att
85  
86 Attribut/Wert-Paare.
87  
88 =over 4
89  
90 =item nameSection => $label
91  
92 Bezeichnung des Namensabschnitts.
93  
94 =item stacktrace => $bool (Default: 1)
95  
96 Gib bei Exception einen Stacktrace aus.
97  
98 =back
99  
100 =back
101  
102 =head4 Returns
103  
104 Objekt
105  
106 =head4 Description
107  
108 Instantiiere ein Objekt der Klasse und liefere eine Referenz auf
109 dieses Objekt zurück. Da die Klasse ausschließlich Klassenmethoden
110 enthält, hat das Objekt lediglich die Funktion, eine abkürzende
111 Aufrufschreibweise zu ermöglichen.
112  
113 =cut
114  
115 # -----------------------------------------------------------------------------
116  
117 21 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
118 21 my $class = shift;
119 # @_: @att
120  
121 21 my $self = $class->SUPER::new(
122 nameSection => 'Name',
123 stacktrace => 1,
124 );
125 21 $self->set(@_);
126  
127 21 return $self;
128 }
129  
130 # -----------------------------------------------------------------------------
131  
132 =head2 Zusicherungen
133  
134 Die folgenden Testmethoden können sowohl als Klassen- als auch als
135 Objektmethoden gerufen werden. Im Void-Kontext gerufen, werfen sie
136 eine Exception, wenn die Bedingung verletzt ist, andernfalls 0 oder 1.
137  
138 =head3 check() - Prüfe beliebige Bedingung
139  
140 =head4 Synopsis
141  
142 $this->check($val,@opt,$sub); # Exception
143 $bool = $this->check($val,@opt,$sub); # Rückgabewert
144  
145 =head4 Arguments
146  
147 =over 4
148  
149 =item $val
150  
151 Wert, der geprüft wird.
152  
153 =item $sub
154  
155 Prüf-Subroutine. Ist die Bedingung erfüllt, liefert sie "wahr",
156 andernfalls "falsch". Beispiel:
157  
158 sub {
159 my $val = shift;
160 return $val =~ /\.export$/? 1: 0;
161 }
162  
163 =back
164  
165 =head4 Options
166  
167 =over 4
168  
169 =item -name => $str
170  
171 Name, der bei Verletzung der Bedingung als Teil der Fehlermeldung
172 ausgegeben wird. Dies kann der Name der geprüften Variable,
173 des geprüften Parameters o.ä. sein.
174  
175 =back
176  
177 =head4 Returns
178  
179 Boolean
180  
181 =head4 Description
182  
183 Prüfe den Wert $val daraufhin, ob er die Prüfung $sub besteht.
184 Ist dies nicht der Fall, wirf eine Exception, wenn die Methode im
185 Void-Kontext gerufen wurde, andernfalls 0. Ein leerer Wert verletzt
186 die Bedingung nicht, d.h. die Subroutine wird für einen leeren Wert nicht
187 gerufen und muss diesen Fall daher nicht behandeln.
188  
189 =cut
190  
191 # -----------------------------------------------------------------------------
192  
193 4 ▶sub check {
Subroutine check: called CC=7 SCAR=19.8
Pod documented
194 4 ▶ my $self = ref $_[0]? shift: shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 4 4
195 4 my $val = shift;
196 # @_: @opt,$sub
197  
198 # Optionen und Argumente
199  
200 4 my $name = undef;
201  
202 4 my $argA = $self->parameters(1,1,\@_,
203 -name => \$name,
204 );
205 4 my $sub = shift @$argA;
206  
207 # Prüfung
208  
209 4 ▶ if (!defined($val) || $val eq '' || $sub->($val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val or $val eq '' or &$sub($val)) { } 3 1 4
elsif (defined wantarray) { } 0 1 1
Condition100%6/6
not defined $val or $val eq "" or &$sub($val)
l!l&&r!l&&!r
211
not defined $val or $val eq ""
l!l&&r!l&&!r
112
MC/DC100%3/3
not defined $val or $val eq "" or &$sub($val)
not defined $val $val eq "" &$sub($val)
Truth table100%4/4
not(A) or B or C
A= not defined $valB= $val eq ""C= &$sub($val)
ABCresult
1XX1
01X1
0011
0000
210 3 return 1;
211 }
212 elsif (defined wantarray) {
213 0 return 0;
214 }
215  
216 $self->throw(
217 'ASSERT-00001: Check failed',
218 defined $name? ($self->{'nameSection'} => $name): (),
219 Value => $val,
220 1 ▶ -stacktrace => $self->{'stacktrace'},
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 1 1
221 );
222 }
223  
224 # -----------------------------------------------------------------------------
225  
226 =head3 pathExists() - Prüfe Existenz von Pfad
227  
228 =head4 Synopsis
229  
230 $this->pathExists($path); # Exception
231 $bool = $this->pathExists($path); # Rückgabewert
232  
233 =head4 Aliases
234  
235 =over 2
236  
237 =item *
238  
239 fileExists()
240  
241 =item *
242  
243 dirExists()
244  
245 =back
246  
247 =head4 Arguments
248  
249 =over 4
250  
251 =item $path (String)
252  
253 Pfad
254  
255 =back
256  
257 =head4 Returns
258  
259 Boolean
260  
261 =head4 Description
262  
263 Prüfe, ob Pfad $path existiert. Ist dies nicht der Fall, wirf eine
264 Exception, wenn die Methode im Void-Kontext gerufen wurde, andernfalls 0.
265 Ein leerer Wert verletzt die Bedingung nicht.
266  
267 =cut
268  
269 # -----------------------------------------------------------------------------
270  
271 0 ▶sub pathExists {
Subroutine pathExists: not called CC=6 SCAR=37.4
Pod documented
272 0 ▶ my $self = ref $_[0]? shift: shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
273 0 my $path = shift;
274  
275 # Prüfung
276  
277 0 ▶ if (!defined($path) || $path eq '' || Quiq::Path->exists($path)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $path or $path eq '' or 'Quiq::Path'->exists($path)) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
Condition0%0/6
not defined $path or $path eq "" or "Quiq::Path"->exists($path)
l!l&&r!l&&!r
000
not defined $path or $path eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $path or $path eq "" or "Quiq::Path"->exists($path)
not defined $path $path eq "" "Quiq::Path"->exists($path)
Truth table0%0/4
not(A) or B or C
A= not defined $pathB= $path eq ""C= "Quiq::Path"->exists($path)
ABCresult
1XX1
01X1
0011
0000
278 0 return 1;
279 }
280 elsif (defined wantarray) {
281 0 return 0;
282 }
283  
284 $self->throw(
285 0 'ASSERT-00001: Path does not exist',
286 Path => $path,
287 );
288 }
289  
290 {
291 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
292 270 *fileExists = \&pathExists;
293 270 *dirExists = \&pathExists;
294 }
295  
296 # -----------------------------------------------------------------------------
297  
298 =head3 isDate() - Prüfe auf Datum
299  
300 =head4 Synopsis
301  
302 $this->isDate($val,@opt); # Exception
303 $bool = $this->isDate($val,@opt); # Rückgabewert
304  
305 =head4 Arguments
306  
307 =over 4
308  
309 =item $val
310  
311 Wert, der geprüft wird.
312  
313 =back
314  
315 =head4 Options
316  
317 =over 4
318  
319 =item -name => $str
320  
321 Name, der bei Verletzung der Bedingung als Teil der Fehlermeldung
322 ausgegeben wird. Dies kann der Name der geprüften Variable,
323 des geprüften Parameters o.ä. sein.
324  
325 =back
326  
327 =head4 Returns
328  
329 Boolean
330  
331 =head4 Description
332  
333 Prüfe den Wert $val daraufhin, dass er eingültiges Datum ist. Falls nein,
334 wirf eine Exception, wenn die Methode im Void-Kontext gerufen wurde,
335 andernfalls 0.
336  
337 =cut
338  
339 # -----------------------------------------------------------------------------
340  
341 3 ▶sub isDate {
Subroutine isDate: called CC=7 SCAR=19.9
Pod documented
342 3 ▶ my $self = ref $_[0]? shift: shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 3 3
343 3 my $val = shift;
344 # @_: @opt
345  
346 # Optionen
347  
348 3 my $name = undef;
349  
350 3 $self->parameters(\@_,
351 -name => \$name,
352 );
353  
354 # Prüfung
355  
356 3 ▶ if (defined($val) && $val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val ne "") 1 2 3
Condition100%3/3
defined $val and $val ne ""
!ll&&!rl&&r
111
MC/DC100%2/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table100%3/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
357 1 local $@;
358 1 eval {Quiq::Time->new(split /-/,$val)};
359 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
360 1 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 1 1
361 0 return 0;
362 }
363  
364 $self->throw(
365 'ASSERT-00002: Invalid date',
366 defined $name? ($self->{'nameSection'} => $name): (),
367 Date => $val,
368 1 ▶ -stacktrace => $self->{'stacktrace'},
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 1 1
369 );
370 }
371 }
372
373 2 return 1;
374 }
375  
376 # -----------------------------------------------------------------------------
377  
378 =head3 isEnumValue() - Prüfe auf Enthaltensein in Enum
379  
380 =head4 Synopsis
381  
382 $this->isEnumValue($val,\@values,@opt); # Exception
383 $bool = $this->isEnumValue($val,\@values,@opt); # Rückgabewert
384  
385 =head4 Arguments
386  
387 =over 4
388  
389 =item $val
390  
391 Wert, der geprüft wird.
392  
393 =item @values
394  
395 Liste der Enum-Werte.
396  
397 =back
398  
399 =head4 Options
400  
401 =over 4
402  
403 =item -name => $str
404  
405 Name, der bei Verletzung der Bedingung als Teil der Fehlermeldung
406 ausgegeben wird. Dies kann der Name der geprüften Variable,
407 des geprüften Parameters o.ä. sein.
408  
409 =back
410  
411 =head4 Returns
412  
413 Boolean
414  
415 =head4 Description
416  
417 Prüfe den Wert $val daraufhin, dass er in Liste @values enthalten ist.
418 Ist dies nicht der Fall, wirf eine Exception, wenn die Methode
419 im Void-Kontext gerufen wurde, andernfalls 0. Ein leerer Wert verletzt
420 die Bedingung nicht.
421  
422 =cut
423  
424 # -----------------------------------------------------------------------------
425  
426 4 ▶sub isEnumValue {
Subroutine isEnumValue: called CC=7 SCAR=19.7
Pod documented
427 4 ▶ my $self = ref $_[0]? shift: shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 4 4
428 4 my $val = shift;
429 4 my $valueA = shift;
430 # @_: @opt
431  
432 # Optionen
433  
434 4 my $name = undef;
435  
436 4 $self->parameters(\@_,
437 -name => \$name,
438 );
439  
440 # Prüfung
441  
442 4 ▶ if (!defined($val) || $val eq '' || grep {$val eq $_} @$valueA) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val or $val eq '' or grep {$val eq $_;} @$valueA) { } 3 1 4
elsif (defined wantarray) { } 0 1 1
Condition100%6/6
not defined $val or $val eq "" or grep {$val eq $_;} @$valueA
l!l&&r!l&&!r
211
not defined $val or $val eq ""
l!l&&r!l&&!r
112
MC/DC100%3/3
not defined $val or $val eq "" or grep {$val eq $_;} @$valueA
not defined $val $val eq "" grep {$val eq $_;} @$valueA
Truth table100%4/4
not(A) or B or C
A= not defined $valB= $val eq ""C= grep {$val eq $_;} @$valueA
ABCresult
1XX1
01X1
0011
0000
443 3 return 1;
444 }
445 elsif (defined wantarray) {
446 0 return 0;
447 }
448  
449 $self->throw(
450 'ASSERT-00001: Unexpected value',
451 defined $name? ($self->{'nameSection'} => $name): (),
452 Value => $val,
453 3 Allowed => join(', ',map {"'$_'"} @$valueA),
454 1 ▶ -stacktrace => $self->{'stacktrace'},
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 1 1
455 );
456 }
457  
458 # -----------------------------------------------------------------------------
459  
460 =head3 isNotNull() - Prüfe auf nichtleeren Wert
461  
462 =head4 Synopsis
463  
464 $this->isNotNull($val,@opt); # Exception
465 $bool = $this->isNotNull($val,@opt); # Rückgabewert
466  
467 =head4 Arguments
468  
469 =over 4
470  
471 =item $val
472  
473 Wert, der geprüft wird.
474  
475 =back
476  
477 =head4 Options
478  
479 =over 4
480  
481 =item -name => $str
482  
483 Name, der bei Verletzung der Bedingung als Teil der Fehlermeldung
484 ausgegeben wird. Dies kann der Name der geprüften Variable,
485 des geprüften Parameters o.ä. sein.
486  
487 =back
488  
489 =head4 Returns
490  
491 Boolean
492  
493 =head4 Description
494  
495 Prüfe den Wert $val daraufhin, dass er nichtleer, also weder
496 undefiniert noch ein Leerstring ist. Ist er leer, wirf eine Exception,
497 wenn die Methode im Void-Kontext gerufen wurde, andernfalls 0.
498  
499 =cut
500  
501 # -----------------------------------------------------------------------------
502  
503 6 ▶sub isNotNull {
Subroutine isNotNull: called CC=6 SCAR=18.2
Pod documented
504 6 ▶ my $self = ref $_[0]? shift: shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 3 6
505 6 my $val = shift;
506 # @_: @opt
507  
508 # Optionen
509  
510 6 my $name = undef;
511  
512 6 $self->parameters(\@_,
513 -name => \$name,
514 );
515  
516 # Prüfung
517  
518 6 ▶ if (defined($val) && $val ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val and $val ne '') { } 4 2 6
elsif (defined wantarray) { } 0 2 2
Condition100%3/3
defined $val and $val ne ""
!ll&&!rl&&r
114
MC/DC100%2/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table100%3/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
519 4 return 1;
520 }
521 elsif (defined wantarray) {
522 0 return 0;
523 }
524  
525 $self->throw(
526 'ASSERT-00002: Value is null',
527 defined $name? ($self->{'nameSection'} => $name): (),
528 2 ▶ -stacktrace => $self->{'stacktrace'},
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 2 2
529 );
530 }
531  
532 # -----------------------------------------------------------------------------
533  
534 =head3 isNumber() - Prüfe auf dezimale Zahldarstellung
535  
536 =head4 Synopsis
537  
538 $this->isNumber($val,@opt); # Exception
539 $bool = $this->isNumber($val,@opt); # Rückgabewert
540  
541 =head4 Arguments
542  
543 =over 4
544  
545 =item $val
546  
547 Wert, der geprüft wird.
548  
549 =back
550  
551 =head4 Options
552  
553 =over 4
554  
555 =item -name => $str
556  
557 Name, der bei Verletzung der Bedingung als Teil der Fehlermeldung
558 ausgegeben wird. Dies kann der Name der geprüften Variable,
559 des geprüften Parameters o.ä. sein.
560  
561 =back
562  
563 =head4 Returns
564  
565 Boolean
566  
567 =head4 Description
568  
569 Prüfe den Wert $val daraufhin, dass er eine dezimale Zahl darstellt.
570 Ist dies nicht der Fall, wirf eine Exception, wenn die Methode im
571 Void-Kontext gerufen wurde, andernfalls 0. Ein leerer Wert verletzt
572 die Bedingung nicht.
573  
574 =cut
575  
576 # -----------------------------------------------------------------------------
577  
578 5 ▶sub isNumber {
Subroutine isNumber: called CC=7 SCAR=19.9
Pod documented
579 5 ▶ my $self = ref $_[0]? shift: shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 5 5
580 5 my $val = shift;
581 # @_: @opt
582  
583 # Optionen
584  
585 5 my $name = undef;
586  
587 5 $self->parameters(\@_,
588 -name => \$name,
589 );
590  
591 # Prüfung
592  
593 5 ▶ if (!defined($val) || $val eq '' || Quiq::Math->isNumber($val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val or $val eq '' or 'Quiq::Math'->isNumber($val)) { } 4 1 5
elsif (defined wantarray) { } 0 1 1
Condition100%6/6
not defined $val or $val eq "" or "Quiq::Math"->isNumber($val)
l!l&&r!l&&!r
221
not defined $val or $val eq ""
l!l&&r!l&&!r
113
MC/DC100%3/3
not defined $val or $val eq "" or "Quiq::Math"->isNumber($val)
not defined $val $val eq "" "Quiq::Math"->isNumber($val)
Truth table100%4/4
not(A) or B or C
A= not defined $valB= $val eq ""C= "Quiq::Math"->isNumber($val)
ABCresult
1XX1
01X1
0011
0000
594 4 return 1;
595 }
596 elsif (defined wantarray) {
597 0 return 0;
598 }
599  
600 $self->throw(
601 'ASSERT-00001: Not a number',
602 defined $name? ($self->{'nameSection'} => $name): (),
603 Value => $val,
604 1 ▶ -stacktrace => $self->{'stacktrace'},
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 1 1
605 );
606 }
607  
608 # -----------------------------------------------------------------------------
609  
610 =head1 VERSION
611  
612 1.240
613  
614 =head1 AUTHOR
615  
616 Frank Seitz, L<http://fseitz.de/>
617  
618 =head1 COPYRIGHT
619  
620 Copyright (C) 2026 Frank Seitz
621  
622 =head1 LICENSE
623  
624 This code is free software; you can redistribute it and/or modify
625 it under the same terms as Perl itself.
626  
627 =cut
628  
629 # -----------------------------------------------------------------------------
630  
631 270 1;
632  
633 # eof
« Quiq/MediaWiki/Markup.pm ↑ Summary Quiq/Formatter.pm »