/usr/share/perl5/Carp/Assert/More.pm is in libcarp-assert-more-perl 1.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | package Carp::Assert::More;
use warnings;
use strict;
use Exporter;
use Carp::Assert;
use vars qw( $VERSION @ISA @EXPORT );
*_fail_msg = *Carp::Assert::_fail_msg;
=head1 NAME
Carp::Assert::More - convenience wrappers around Carp::Assert
=head1 VERSION
Version 1.14
=cut
BEGIN {
$VERSION = '1.14';
@ISA = qw(Exporter);
@EXPORT = qw(
assert_defined
assert_exists
assert_fail
assert_hashref
assert_in
assert_integer
assert_is
assert_isa
assert_isnt
assert_lacks
assert_like
assert_listref
assert_negative
assert_negative_integer
assert_nonblank
assert_nonempty
assert_nonnegative
assert_nonnegative_integer
assert_nonref
assert_nonzero
assert_nonzero_integer
assert_positive
assert_positive_integer
assert_undefined
assert_unlike
);
}
=head1 SYNOPSIS
A set of convenience functions for common assertions.
use Carp::Assert::More;
my $obj = My::Object;
assert_isa( $obj, 'My::Object', 'Got back a correct object' );
=head1 DESCRIPTION
Carp::Assert::More is a set of wrappers around the L<Carp::Assert> functions
to make the habit of writing assertions even easier.
Everything in here is effectively syntactic sugar. There's no technical
reason to use
assert_isa( $foo, 'HTML::Lint' );
instead of
assert( defined $foo );
assert( ref($foo) eq 'HTML::Lint' );
other than readability and simplicity of the code.
My intent here is to make common assertions easy so that we as programmers
have no excuse to not use them.
=head1 CAVEATS
I haven't specifically done anything to make Carp::Assert::More be
backwards compatible with anything besides Perl 5.6.1, much less back
to 5.004. Perhaps someone with better testing resources in that area
can help me out here.
=head1 SIMPLE ASSERTIONS
=head2 assert_is( $string, $match [,$name] )
Asserts that I<$string> matches I<$match>.
=cut
sub assert_is($$;$) {
my $string = shift;
my $match = shift;
my $name = shift;
# undef only matches undef
return if !defined($string) && !defined($match);
assert_defined( $string, $name );
assert_defined( $match, $name );
return if $string eq $match;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_isnt( $string, $unmatch [,$name] )
Asserts that I<$string> does NOT match I<$unmatch>.
=cut
sub assert_isnt($$;$) {
my $string = shift;
my $unmatch = shift;
my $name = shift;
# undef only matches undef
return if defined($string) xor defined($unmatch);
return if defined($string) && defined($unmatch) && ($string ne $unmatch);
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_like( $string, qr/regex/ [,$name] )
Asserts that I<$string> matches I<qr/regex/>.
The assertion fails either the string or the regex are undef.
=cut
sub assert_like($$;$) {
my $string = shift;
my $regex = shift;
my $name = shift;
assert_nonref( $string, $name );
assert_isa( $regex, 'Regexp', $name );
return if $string =~ $regex;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_unlike( $string, qr/regex/ [,$name] )
Asserts that I<$string> matches I<qr/regex/>.
The assertion fails if the regex is undef.
=cut
sub assert_unlike($$;$) {
my $string = shift;
my $regex = shift;
my $name = shift;
return if !defined($string);
assert_nonref( $string, $name );
assert_isa( $regex, 'Regexp', $name );
return if $string !~ $regex;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_defined( $this [, $name] )
Asserts that I<$this> is defined.
=cut
sub assert_defined($;$) {
return if defined( $_[0] );
require Carp;
&Carp::confess( _fail_msg($_[1]) );
}
=head2 assert_undefined( $this [, $name] )
Asserts that I<$this> is not defined.
=cut
sub assert_undefined($;$) {
return unless defined( $_[0] );
require Carp;
&Carp::confess( _fail_msg($_[1]) );
}
=head2 assert_nonblank( $this [, $name] )
Asserts that I<$this> is not blank and not a reference.
=cut
sub assert_nonblank($;$) {
my $this = shift;
my $name = shift;
assert_nonref( $this, $name );
return if $this ne "";
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head1 NUMERIC ASSERTIONS
=head2 assert_integer( $this [, $name ] )
Asserts that I<$this> is an integer, which may be zero or negative.
assert_integer( 0 ); # pass
assert_integer( 14 ); # pass
assert_integer( -14 ); # FAIL
assert_integer( '14.' ); # FAIL
=cut
sub assert_integer($;$) {
my $this = shift;
my $name = shift;
assert_nonref( $this, $name );
return if $this =~ /^-?\d+$/;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_nonzero( $this [, $name ] )
Asserts that the numeric value of I<$this> is not zero.
assert_nonzero( 0 ); # FAIL
assert_nonzero( -14 ); # pass
assert_nonzero( '14.' ); # pass
Asserts that the numeric value of I<$this> is not zero.
=cut
sub assert_nonzero($;$) {
my $this = shift;
my $name = shift;
no warnings;
return if $this+0 != 0;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_positive( $this [, $name ] )
Asserts that the numeric value of I<$this> is greater than zero.
assert_positive( 0 ); # FAIL
assert_positive( -14 ); # FAIL
assert_positive( '14.' ); # pass
=cut
sub assert_positive($;$) {
my $this = shift;
my $name = shift;
no warnings;
return if $this+0 > 0;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_nonnegative( $this [, $name ] )
Asserts that the numeric value of I<$this> is greater than or equal
to zero. Since non-numeric strings evaluate to zero, this means that
any non-numeric string will pass.
assert_nonnegative( 0 ); # pass
assert_nonnegative( -14 ); # FAIL
assert_nonnegative( '14.' ); # pass
assert_nonnegative( 'dog' ); # pass
=cut
sub assert_nonnegative($;$) {
my $this = shift;
my $name = shift;
no warnings;
return if $this+0 >= 0;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_negative( $this [, $name ] )
Asserts that the numeric value of I<$this> is less than zero.
assert_negative( 0 ); # FAIL
assert_negative( -14 ); # pass
assert_negative( '14.' ); # FAIL
=cut
sub assert_negative($;$) {
my $this = shift;
my $name = shift;
no warnings;
return if $this+0 < 0;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_nonzero_integer( $this [, $name ] )
Asserts that the numeric value of I<$this> is not zero, and that I<$this>
is an integer.
assert_nonzero_integer( 0 ); # FAIL
assert_nonzero_integer( -14 ); # pass
assert_nonzero_integer( '14.' ); # FAIL
=cut
sub assert_nonzero_integer($;$) {
my $this = shift;
my $name = shift;
assert_nonzero( $this, $name );
assert_integer( $this, $name );
}
=head2 assert_positive_integer( $this [, $name ] )
Asserts that the numeric value of I<$this> is greater than zero, and
that I<$this> is an integer.
assert_positive_integer( 0 ); # FAIL
assert_positive_integer( -14 ); # FAIL
assert_positive_integer( '14.' ); # FAIL
assert_positive_integer( '14' ); # pass
=cut
sub assert_positive_integer($;$) {
my $this = shift;
my $name = shift;
assert_positive( $this, $name );
assert_integer( $this, $name );
}
=head2 assert_nonnegative_integer( $this [, $name ] )
Asserts that the numeric value of I<$this> is not less than zero, and
that I<$this> is an integer.
assert_nonnegative_integer( 0 ); # pass
assert_nonnegative_integer( -14 ); # pass
assert_nonnegative_integer( '14.' ); # FAIL
=cut
sub assert_nonnegative_integer($;$) {
my $this = shift;
my $name = shift;
assert_nonnegative( $this, $name );
assert_integer( $this, $name );
}
=head2 assert_negative_integer( $this [, $name ] )
Asserts that the numeric value of I<$this> is less than zero, and that
I<$this> is an integer.
assert_negative_integer( 0 ); # FAIL
assert_negative_integer( -14 ); # pass
assert_negative_integer( '14.' ); # FAIL
=cut
sub assert_negative_integer($;$) {
my $this = shift;
my $name = shift;
assert_negative( $this, $name );
assert_integer( $this, $name );
}
=head1 REFERENCE ASSERTIONS
=head2 assert_isa( $this, $type [, $name ] )
Asserts that I<$this> is an object of type I<$type>.
=cut
sub assert_isa($$;$) {
my $this = shift;
my $type = shift;
my $name = shift;
assert_defined( $this, $name );
# The assertion is true if
# 1) For objects, $this is of class $type or of a subclass of $type
# 2) For non-objects, $this is a reference to a HASH, SCALAR, ARRAY, etc.
require Scalar::Util;
return if Scalar::Util::blessed( $this ) && $this->isa( $type );
return if ref($this) eq $type;
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_nonempty( $this [, $name ] )
I<$this> must be a ref to either a hash or an array. Asserts that that
collection contains at least 1 element. Will assert (with its own message,
not I<$name>) unless given a hash or array ref. It is OK if I<$this> has
been blessed into objecthood, but the semantics of checking an object to see
if it has keys (for a hashref) or returns >0 in scalar context (for an array
ref) may not be what you want.
assert_nonempty( 0 ); # FAIL
assert_nonempty( 'foo' ); # FAIL
assert_nonempty( undef ); # FAIL
assert_nonempty( {} ); # FAIL
assert_nonempty( [] ); # FAIL
assert_nonempty( {foo=>1} );# pass
assert_nonempty( [1,2,3] ); # pass
=cut
sub assert_nonempty($;$) {
my $ref = shift;
my $name = shift;
require Scalar::Util;
my $underlying_type;
if ( Scalar::Util::blessed( $ref ) ) {
$underlying_type = Scalar::Util::reftype( $ref );
}
else {
$underlying_type = ref( $ref );
}
if ( $underlying_type eq 'HASH' ) {
assert_positive( scalar keys %{$ref}, $name );
}
elsif ( $underlying_type eq 'ARRAY' ) {
assert_positive( scalar @{$ref}, $name );
}
else {
assert_fail( 'Not an array or hash reference' );
}
}
=head2 assert_nonref( $this [, $name ] )
Asserts that I<$this> is not undef and not a reference.
=cut
sub assert_nonref($;$) {
my $this = shift;
my $name = shift;
assert_defined( $this, $name );
return unless ref( $this );
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_hashref( $ref [,$name] )
Asserts that I<$ref> is defined, and is a reference to a (possibly empty) hash.
B<NB:> This method returns I<false> for objects, even those whose underlying
data is a hashref. This is as it should be, under the assumptions that:
=over 4
=item (a)
you shouldn't rely on the underlying data structure of a particular class, and
=item (b)
you should use C<assert_isa> instead.
=back
=cut
sub assert_hashref($;$) {
my $ref = shift;
my $name = shift;
return assert_isa( $ref, 'HASH', $name );
}
=head2 assert_listref( $ref [,$name] )
Asserts that I<$ref> is defined, and is a reference to a (possibly empty) list.
B<NB:> The same caveat about objects whose underlying structure is a
hash (see C<assert_hashref>) applies here; this method returns false
even for objects whose underlying structure is an array.
=cut
sub assert_listref($;$) {
my $ref = shift;
my $name = shift;
return assert_isa( $ref, 'ARRAY', $name );
}
=head1 SET AND HASH MEMBERSHIP
=head2 assert_in( $string, \@inlist [,$name] );
Asserts that I<$string> is defined and matches one of the elements
of I<\@inlist>.
I<\@inlist> must be an array reference of defined strings.
=cut
sub assert_in($$;$) {
my $string = shift;
my $arrayref = shift;
my $name = shift;
assert_nonref( $string, $name );
assert_isa( $arrayref, 'ARRAY', $name );
foreach my $element (@{$arrayref}) {
assert_nonref( $element, $name );
return if $string eq $element;
}
require Carp;
&Carp::confess( _fail_msg($name) );
}
=head2 assert_exists( \%hash, $key [,$name] )
=head2 assert_exists( \%hash, \@keylist [,$name] )
Asserts that I<%hash> is indeed a hash, and that I<$key> exists in
I<%hash>, or that all of the keys in I<@keylist> exist in I<%hash>.
assert_exists( \%custinfo, 'name', 'Customer has a name field' );
assert_exists( \%custinfo, [qw( name addr phone )],
'Customer has name, address and phone' );
=cut
sub assert_exists($$;$) {
my $hash = shift;
my $key = shift;
my $name = shift;
assert_isa( $hash, 'HASH', $name );
my @list = ref($key) ? @$key : ($key);
for ( @list ) {
if ( !exists( $hash->{$_} ) ) {
require Carp;
&Carp::confess( _fail_msg($name) );
}
}
}
=head2 assert_lacks( \%hash, $key [,$name] )
=head2 assert_lacks( \%hash, \@keylist [,$name] )
Asserts that I<%hash> is indeed a hash, and that I<$key> does NOT exist
in I<%hash>, or that none of the keys in I<@keylist> exist in I<%hash>.
assert_lacks( \%users, 'root', 'Root is not in the user table' );
assert_lacks( \%users, [qw( root admin nobody )], 'No bad usernames found' );
=cut
sub assert_lacks($$;$) {
my $hash = shift;
my $key = shift;
my $name = shift;
assert_isa( $hash, 'HASH', $name );
my @list = ref($key) ? @$key : ($key);
for ( @list ) {
if ( exists( $hash->{$_} ) ) {
require Carp;
&Carp::confess( _fail_msg($name) );
}
}
}
=head1 UTILITY ASSERTIONS
=head2 assert_fail( [$name] )
Assertion that always fails. C<assert_fail($msg)> is exactly the same
as calling C<assert(0,$msg)>, but it eliminates that case where you
accidentally use C<assert($msg)>, which of course never fires.
=cut
sub assert_fail(;$) {
require Carp;
&Carp::confess( _fail_msg($_[0]) );
}
=head1 COPYRIGHT & LICENSE
Copyright 2005-2012 Andy Lester.
This program is free software; you can redistribute it and/or modify
it under the terms of the Artistic License version 2.0.
=head1 ACKNOWLEDGEMENTS
Thanks to
Bob Diss,
Pete Krawczyk,
David Storrs,
Dan Friedman,
Allard Hoeve,
Thomas L. Shinnick,
and Leland Johnson
for code and fixes.
=cut
"I stood on the porch in a tie."
|