Assert-Conditional
view release on metacpan or search on metacpan
lib/Assert/Conditional.pm view on Meta::CPAN
sub assert_blank ( $ ) ;
sub assert_bmp ( $ ) ;
sub assert_box_number ( $ ) ;
sub assert_bytes ( $ ) ;
sub assert_can ( $@ ) ;
sub assert_cant ( $@ ) ;
sub assert_class_ainta ( $@ ) ;
sub assert_class_can ( $@ ) ;
sub assert_class_cant ( $@ ) ;
sub assert_class_isa ( $@ ) ;
sub assert_class_method ( ) ;
sub assert_coderef ( $ ) ;
sub assert_defined ( $ ) ;
sub assert_defined_value ( $ ) ;
sub assert_defined_variable ( \$ ) ;
sub assert_digits ( $ ) ;
sub assert_directory ( $ ) ;
sub assert_does ( $@ ) ;
sub assert_doesnt ( $@ ) ;
sub assert_dumped_core ( ;$ ) ;
sub assert_empty ( $ ) ;
sub assert_eq ( $$ ) ;
sub assert_eq_letters ( $$ ) ;
sub assert_even_number ( $ ) ;
sub assert_exited ( ;$ ) ;
sub assert_false ( $ ) ;
sub assert_fractional ( $ ) ;
sub assert_full_perl_ident ( $ ) ;
sub assert_globref ( $ ) ;
sub assert_happy_code ( & ) ;
sub assert_happy_exit ( ;$ ) ;
sub assert_hash_keys ( \% @ ) ;
sub assert_hash_keys_allowed ( \% @ ) ;
sub assert_hash_keys_allowed_and_required ( \% $ $ ) ;
sub assert_hash_keys_required ( \% @ ) ;
sub assert_hash_keys_required_and_allowed ( \% $ $ ) ;
sub assert_hash_nonempty ( \% ) ;
sub assert_hashref ( $ ) ;
sub assert_hashref_keys ( $@ ) ;
sub assert_hashref_keys_allowed ( $@ ) ;
sub assert_hashref_keys_allowed_and_required ( $$$ ) ;
sub assert_hashref_keys_required ( $@ ) ;
sub assert_hashref_keys_required_and_allowed ( $$$ ) ;
sub assert_hashref_nonempty ( $ ) ;
sub assert_hex_number ( $ ) ;
sub assert_in_list ( $@ ) ;
sub assert_in_numeric_range ( $$$ ) ;
sub assert_integer ( $ ) ;
sub assert_ioref ( $ ) ;
sub assert_is ( $$ ) ;
sub assert_isa ( $@ ) ;
sub assert_isnt ( $$ ) ;
sub assert_keys ( \[%$] @ ) ;
sub assert_known_package ( $ ) ;
sub assert_latin1 ( $ ) ;
sub assert_latinish ( $ ) ;
sub assert_legal_exit_status ( ;$ ) ;
sub assert_like ( $$ ) ;
sub assert_list_context ( ) ;
sub assert_list_nonempty ( @ ) ;
sub assert_locked ( \[%$] @ ) ;
sub assert_lowercased ( $ ) ;
sub assert_max_keys ( \[%$] @ ) ;
sub assert_method ( ) ;
sub assert_min_keys ( \[%$] @ ) ;
sub assert_minmax_keys ( \[%$] \[@$] \[@$] ) ;
sub assert_multi_line ( $ ) ;
sub assert_natural_number ( $ ) ;
sub assert_negative ( $ ) ;
sub assert_negative_integer ( $ ) ;
sub assert_nfc ( $ ) ;
sub assert_nfd ( $ ) ;
sub assert_nfkc ( $ ) ;
sub assert_nfkd ( $ ) ;
sub assert_no_coredump ( ;$ ) ;
sub assert_nonalphabetic ( $ ) ;
sub assert_nonascii ( $ ) ;
sub assert_nonastral ( $ ) ;
sub assert_nonblank ( $ ) ;
sub assert_nonbytes ( $ ) ;
sub assert_nonempty ( $ ) ;
sub assert_nonlist_context ( ) ;
sub assert_nonnegative ( $ ) ;
sub assert_nonnegative_integer ( $ ) ;
sub assert_nonnumeric ( $ ) ;
sub assert_nonobject ( $ ) ;
sub assert_nonpositive ( $ ) ;
sub assert_nonpositive_integer ( $ ) ;
sub assert_nonref ( $ ) ;
sub assert_nonvoid_context ( ) ;
sub assert_nonzero ( $ ) ;
sub assert_not_in_list ( $@ ) ;
sub assert_numeric ( $ ) ;
sub assert_object ( $ ) ;
sub assert_object_ainta ( $@ ) ;
sub assert_object_boolifies ( $ ) ;
sub assert_object_can ( $@ ) ;
sub assert_object_cant ( $@ ) ;
sub assert_object_isa ( $@ ) ;
sub assert_object_method ( ) ;
sub assert_object_nummifies ( $ ) ;
sub assert_object_overloads ( $@ ) ;
sub assert_object_stringifies ( $ ) ;
sub assert_odd_number ( $ ) ;
sub assert_open_handle ( $ ) ;
sub assert_positive ( $ ) ;
sub assert_positive_integer ( $ ) ;
sub assert_private_method ( ) ;
sub assert_protected_method ( ) ;
sub assert_public_method ( ) ;
sub assert_qualified_ident ( $ ) ;
sub assert_refref ( $ ) ;
sub assert_reftype ( $$ ) ;
sub assert_regex ( $ ) ;
sub assert_regular_file ( $ ) ;
sub assert_sad_exit ( ;$ ) ;
sub assert_scalar_context ( ) ;
sub assert_scalarref ( $ ) ;
sub assert_signalled ( ;$ ) ;
sub assert_signed_number ( $ ) ;
sub assert_simple_perl_ident ( $ ) ;
sub assert_single_line ( $ ) ;
sub assert_single_paragraph ( $ ) ;
sub assert_text_file ( $ ) ;
sub assert_tied ( \[$@%*] ) ;
sub assert_tied_array ( \@ ) ;
sub assert_tied_arrayref ( $ ) ;
sub assert_tied_glob ( \* ) ;
sub assert_tied_globref ( $ ) ;
sub assert_tied_hash ( \% ) ;
sub assert_tied_hashref ( $ ) ;
sub assert_tied_referent ( $ ) ;
sub assert_tied_scalar ( \$ ) ;
sub assert_tied_scalarref ( $ ) ;
sub assert_true ( $ ) ;
sub assert_unblessed_ref ( $ ) ;
sub assert_undefined ( $ ) ;
sub assert_unhappy_code ( & ) ;
sub assert_unicode_ident ( $ ) ;
sub assert_unlike ( $$ ) ;
sub assert_unlocked ( \[%$] @ ) ;
sub assert_unsignalled ( ;$ ) ;
sub assert_untied ( \[$@%*] ) ;
sub assert_untied_array ( \@ ) ;
sub assert_untied_arrayref ( $ ) ;
sub assert_untied_glob ( \* ) ;
sub assert_untied_globref ( $ ) ;
sub assert_untied_hash ( \% ) ;
sub assert_untied_hashref ( $ ) ;
sub assert_untied_referent ( $ ) ;
sub assert_untied_scalar ( \$ ) ;
sub assert_untied_scalarref ( $ ) ;
sub assert_uppercased ( $ ) ;
sub assert_void_context ( ) ;
sub assert_whole_number ( $ ) ;
sub assert_wide_characters ( $ ) ;
sub assert_zero ( $ ) ;
############################################################
sub import {
my ($package, @conditional_imports) = @_;
my @normal_imports = $package->_strip_import_conditions(@conditional_imports);
if ($Assert_Never) { $package->SUPER::import(@normal_imports, -if => 0) }
elsif ($Assert_Always) { $package->SUPER::import(@normal_imports, -if => 1) }
else { $package->SUPER::import(@conditional_imports ) }
$package->_reimport_nulled_code_protos();
}
# This is just pretty extreme, but it's also about the only way to
# make the Exporter shut up about things we sometimes need to do in
# this module.
#
# Well, not quite the only way: there's always local *SIG. :)
#
# Otherwise it dribbles all over your screen when you try more than one
# import, like importing a set and then reneging on a few of them.
#
# Newer versions of Carp appear not to need these heroics.
sub export_to_level {
my($package, $level, @export_args) = @_;
state $old_carp = \&Carp::carp;
state $filters = [
qr/^Constant subroutine \S+ redefined/,
qr/^Subroutine \S+ redefined/,
qr/^Prototype mismatch:/,
];
no warnings "redefine";
local *Carp::carp = sub {
my($text) = @_;
$text =~ $_ && return for @$filters;
local $Carp::CarpInternal{"Exporter::Heavy"} = 1;
$old_carp->($text);
};
$package->SUPER::export_to_level($level+2, @export_args);
}
# You have to do this if you have asserts that take a code
# ref as their first argument and people want to use those
lib/Assert/Conditional.pm view on Meta::CPAN
:Assert( qw[hash] )
{
my($hashref, $minkeys, $maxkeys) = @_;
_promote_to_hashref($hashref);
_promote_to_arrayref($minkeys);
@$minkeys || botch "no min keys given";
_promote_to_arrayref($maxkeys);
@$maxkeys || botch "no max keys given";
my @forbidden;
my %required = map { $_ => 1 } @$minkeys;
my %allowed = map { $_ => 1 } @$maxkeys;
for my $key (keys %$hashref) {
delete $required{$key};
delete $allowed{$key} || push @forbidden, $key;
}
my @missing = keys %required;
return unless @missing || @forbidden;
my $missing_msg = !@missing ? "" :
"key" . (@missing > 1 && "s") . " "
. quotify_and(uca_sort @missing)
. " missing from hash";
my $forbidden_msg = !@forbidden ? "" :
"key" . (@forbidden > 1 && "s") . " "
. quotify_and(uca_sort @forbidden)
. " forbidden in hash";
my $message = commify_and grep { length } $missing_msg, $forbidden_msg;
botch $message;
}
sub assert_keys( \[%$] @ )
:Assert( qw[hash] )
{
my($hashref, @keylist) = @_;
_promote_to_hashref($hashref);
assert_minmax_keys($hashref, @keylist, @keylist);
}
sub assert_hashref_keys_required_and_allowed($$$)
:Assert( qw[hash] )
{
my($hashref, $required, $allowed) = @_;
assert_minmax_keys($hashref, $required, $allowed);
}
sub assert_hashref_keys_allowed_and_required($$$)
:Assert( qw[hash] )
{
my($hashref, $allowed, $required) = @_;
assert_minmax_keys($hashref, $required, $allowed);
}
# From perl5180delta, you couldn't actually get any use of
# the predicates to check whether a hash or hashref was
# locked because even though they were exported those
# function did not exist before.!
##
## * Hash::Util has been upgraded to 0.15.
##
## "hash_unlocked" and "hashref_unlocked" now returns true if the hash
## is unlocked, instead of always returning false [perl #112126].
##
## "hash_unlocked", "hashref_unlocked", "lock_hash_recurse" and
## "unlock_hash_recurse" are now exportable [perl #112126].
##
## Two new functions, "hash_locked" and "hashref_locked", have been
## added. Oddly enough, these two functions were already exported,
## even though they did not exist [perl #112126].
BEGIN {
use Hash::Util qw{hash_locked};
my $want_version = 0.15;
my $have_version = Hash::Util->VERSION;
my $huv = "v$have_version of Hash::Util and we need";
my $compiling = "compiling assert_lock and assert_unlocked because your perl $^V has";
my $debugging = $Exporter::Verbose || $Assert_Debug;
if ($have_version < $want_version) {
carp "Not $compiling only $huv v$want_version at ", __FILE__, " line ", __LINE__ if $debugging;
} else {
carp "\u$compiling $huv only v$want_version at ", __FILE__, " line ", __LINE__ if $debugging;
confess "compilation eval blew up: $@" unless eval <<'END_OF_LOCK_STUFF';
sub assert_locked( \[%$] @ )
:Assert( qw[hash] )
{
my($hashref) = @_;
_promote_to_hashref($hashref);
hash_locked(%$hashref) || botch "hash is locked";
}
sub assert_unlocked( \[%$] @ )
:Assert( qw[hash] )
{
my($hashref) = @_;
_promote_to_hashref($hashref);
!hash_locked(%$hashref) || botch "hash is not locked";
}
1;
END_OF_LOCK_STUFF
}
}
sub assert_anyref($)
:Assert( qw[ref] )
{
my($arg) = @_;
ref($arg) || botch "expected reference argument";
}
sub assert_nonref($)
:Assert( qw[ref] )
{
my($arg) = @_;
!ref($arg) || botch "expected nonreference argument";
}
sub assert_reftype($$)
:Assert( qw[object ref] )
{
my($want_type, $arg) = @_;
my $have_type = reftype($arg) // "non-reference";
$have_type eq $want_type || botch "expected reftype of $want_type not $have_type";
}
sub assert_globref($)
:Assert( qw[glob ref] )
{
my($arg) = @_;
assert_reftype(GLOB => $arg);
}
sub assert_ioref($)
:Assert( qw[io ref] )
{
my($arg) = @_;
assert_reftype(IO => $arg);
}
sub assert_coderef($)
:Assert( qw[code ref] )
{
my($arg) = @_;
assert_reftype(CODE => $arg);
}
sub assert_hashref($)
:Assert( qw[hash ref] )
{
my($arg) = @_;
assert_reftype(HASH => $arg);
}
sub assert_arrayref($)
:Assert( qw[array ref] )
lib/Assert/Conditional.pm view on Meta::CPAN
assert_blank ( $ ) ;
assert_bmp ( $ ) ;
assert_box_number ( $ ) ;
assert_bytes ( $ ) ;
assert_can ( $@ ) ;
assert_cant ( $@ ) ;
assert_class_ainta ( $@ ) ;
assert_class_can ( $@ ) ;
assert_class_cant ( $@ ) ;
assert_class_isa ( $@ ) ;
assert_class_method ( ) ;
assert_coderef ( $ ) ;
assert_defined ( $ ) ;
assert_defined_value ( $ ) ;
assert_defined_variable ( \$ ) ;
assert_digits ( $ ) ;
assert_directory ( $ ) ;
assert_does ( $@ ) ;
assert_doesnt ( $@ ) ;
assert_dumped_core ( ;$ ) ;
assert_empty ( $ ) ;
assert_eq ( $$ ) ;
assert_eq_letters ( $$ ) ;
assert_even_number ( $ ) ;
assert_exited ( ;$ ) ;
assert_false ( $ ) ;
assert_fractional ( $ ) ;
assert_full_perl_ident ( $ ) ;
assert_globref ( $ ) ;
assert_happy_code ( & ) ;
assert_happy_exit ( ;$ ) ;
assert_hash_keys ( \% @ ) ;
assert_hash_keys_allowed ( \% @ ) ;
assert_hash_keys_allowed_and_required ( \% $ $ ) ;
assert_hash_keys_required ( \% @ ) ;
assert_hash_keys_required_and_allowed ( \% $ $ ) ;
assert_hash_nonempty ( \% ) ;
assert_hashref ( $ ) ;
assert_hashref_keys ( $@ ) ;
assert_hashref_keys_allowed ( $@ ) ;
assert_hashref_keys_allowed_and_required ( $$$ ) ;
assert_hashref_keys_required ( $@ ) ;
assert_hashref_keys_required_and_allowed ( $$$ ) ;
assert_hashref_nonempty ( $ ) ;
assert_hex_number ( $ ) ;
assert_in_list ( $@ ) ;
assert_in_numeric_range ( $$$ ) ;
assert_integer ( $ ) ;
assert_ioref ( $ ) ;
assert_is ( $$ ) ;
assert_isa ( $@ ) ;
assert_isnt ( $$ ) ;
assert_keys ( \[%$] @ ) ;
assert_known_package ( $ ) ;
assert_latin1 ( $ ) ;
assert_latinish ( $ ) ;
assert_legal_exit_status ( ;$ ) ;
assert_like ( $$ ) ;
assert_list_context ( ) ;
assert_list_nonempty ( @ ) ;
assert_locked ( \[%$] @ ) ;
assert_lowercased ( $ ) ;
assert_max_keys ( \[%$] @ ) ;
assert_method ( ) ;
assert_min_keys ( \[%$] @ ) ;
assert_minmax_keys ( \[%$] \[@$] \[@$] ) ;
assert_multi_line ( $ ) ;
assert_natural_number ( $ ) ;
assert_negative ( $ ) ;
assert_negative_integer ( $ ) ;
assert_nfc ( $ ) ;
assert_nfd ( $ ) ;
assert_nfkc ( $ ) ;
assert_nfkd ( $ ) ;
assert_no_coredump ( ;$ ) ;
assert_nonalphabetic ( $ ) ;
assert_nonascii ( $ ) ;
assert_nonastral ( $ ) ;
assert_nonblank ( $ ) ;
assert_nonbytes ( $ ) ;
assert_nonempty ( $ ) ;
assert_nonlist_context ( ) ;
assert_nonnegative ( $ ) ;
assert_nonnegative_integer ( $ ) ;
assert_nonnumeric ( $ ) ;
assert_nonobject ( $ ) ;
assert_nonpositive ( $ ) ;
assert_nonpositive_integer ( $ ) ;
assert_nonref ( $ ) ;
assert_nonvoid_context ( ) ;
assert_nonzero ( $ ) ;
assert_not_in_list ( $@ ) ;
assert_numeric ( $ ) ;
assert_object ( $ ) ;
assert_object_ainta ( $@ ) ;
assert_object_boolifies ( $ ) ;
assert_object_can ( $@ ) ;
assert_object_cant ( $@ ) ;
assert_object_isa ( $@ ) ;
assert_object_method ( ) ;
assert_object_nummifies ( $ ) ;
assert_object_overloads ( $@ ) ;
assert_object_stringifies ( $ ) ;
assert_odd_number ( $ ) ;
assert_open_handle ( $ ) ;
assert_positive ( $ ) ;
assert_positive_integer ( $ ) ;
assert_private_method ( ) ;
assert_protected_method ( ) ;
assert_public_method ( ) ;
assert_qualified_ident ( $ ) ;
assert_refref ( $ ) ;
assert_reftype ( $$ ) ;
assert_regex ( $ ) ;
assert_regular_file ( $ ) ;
assert_sad_exit ( ;$ ) ;
assert_scalar_context ( ) ;
assert_scalarref ( $ ) ;
assert_signalled ( ;$ ) ;
assert_signed_number ( $ ) ;
assert_simple_perl_ident ( $ ) ;
assert_single_line ( $ ) ;
assert_single_paragraph ( $ ) ;
assert_text_file ( $ ) ;
assert_tied ( \[$@*] ) ;
assert_tied_array ( \@ ) ;
assert_tied_arrayref ( $ ) ;
assert_tied_glob ( \* ) ;
assert_tied_globref ( $ ) ;
assert_tied_hash ( \% ) ;
assert_tied_hashref ( $ ) ;
assert_tied_referent ( $ ) ;
assert_tied_scalar ( \$ ) ;
assert_tied_scalarref ( $ ) ;
assert_true ( $ ) ;
assert_unblessed_ref ( $ ) ;
assert_undefined ( $ ) ;
assert_unhappy_code ( & ) ;
assert_unicode_ident ( $ ) ;
assert_unlike ( $$ ) ;
assert_unlocked ( \[%$] @ ) ;
assert_unsignalled ( ;$ ) ;
assert_untied ( \[$@%*] ) ;
assert_untied_array ( \@ ) ;
assert_untied_arrayref ( $ ) ;
assert_untied_glob ( \* ) ;
assert_untied_globref ( $ ) ;
assert_untied_hash ( \% ) ;
assert_untied_hashref ( $ ) ;
assert_untied_referent ( $ ) ;
assert_untied_scalar ( \$ ) ;
assert_untied_scalarref ( $ ) ;
assert_uppercased ( $ ) ;
assert_void_context ( ) ;
assert_whole_number ( $ ) ;
assert_wide_characters ( $ ) ;
assert_zero ( $ ) ;
All assertions have function prototypes; this helps you use them correctly,
and in some cases casts the argument into scalar context, adds backslashes
to pass things by reference, so you don't have to.
=head2 Export Tags
You may import all assertions or just some of them. When importing only
some of them, you may wish to use an export tag to import a set of related
assertions. Here is what each tag imports:
=over
=item C<:all>
L</assert_ainta>, L</assert_alnum>, L</assert_alphabetic>,
L</assert_anyref>, L</assert_argc>, L</assert_argc_max>,
L</assert_argc_min>, L</assert_argc_minmax>, L</assert_array_length>,
L</assert_array_length_max>, L</assert_array_length_min>,
L</assert_array_length_minmax>, L</assert_array_nonempty>,
L</assert_arrayref>, L</assert_arrayref_nonempty>, L</assert_ascii>,
L</assert_ascii_ident>, L</assert_astral>, L</assert_blank>,
L</assert_bmp>, L</assert_box_number>, L</assert_bytes>, L</assert_can>,
L</assert_cant>, L</assert_class_ainta>, L</assert_class_can>,
L</assert_class_cant>, L</assert_class_isa>, L</assert_class_method>,
L</assert_coderef>, L</assert_defined>, L</assert_defined_value>,
L</assert_defined_variable>, L</assert_digits>, L</assert_directory>,
L</assert_does>, L</assert_doesnt>, L</assert_dumped_core>,
L</assert_empty>, L</assert_eq>, L</assert_eq_letters>,
L</assert_even_number>, L</assert_exited>, L</assert_false>,
L</assert_fractional>, L</assert_full_perl_ident>, L</assert_globref>,
L</assert_happy_code>, L</assert_happy_exit>, L</assert_hash_keys>,
L</assert_hash_keys_allowed>, L</assert_hash_keys_allowed_and_required>,
L</assert_hash_keys_required>, L</assert_hash_keys_required_and_allowed>,
L</assert_hash_nonempty>, L</assert_hashref>, L</assert_hashref_keys>,
L</assert_hashref_keys_allowed>,
L</assert_hashref_keys_allowed_and_required>,
L</assert_hashref_keys_required>,
L</assert_hashref_keys_required_and_allowed>, L</assert_hashref_nonempty>,
L</assert_hex_number>, L</assert_in_list>, L</assert_in_numeric_range>,
L</assert_integer>, L</assert_ioref>, L</assert_is>, L</assert_isa>,
L</assert_isnt>, L</assert_keys>, L</assert_known_package>,
L</assert_latin1>, L</assert_latinish>, L</assert_legal_exit_status>,
L</assert_like>, L</assert_list_context>, L</assert_list_nonempty>,
L</assert_locked>, L</assert_lowercased>, L</assert_max_keys>,
L</assert_method>, L</assert_min_keys>, L</assert_minmax_keys>,
L</assert_multi_line>, L</assert_natural_number>, L</assert_negative>,
L</assert_negative_integer>, L</assert_nfc>, L</assert_nfd>,
L</assert_nfkc>, L</assert_nfkd>, L</assert_no_coredump>,
L</assert_nonalphabetic>, L</assert_nonascii>, L</assert_nonastral>,
L</assert_nonblank>, L</assert_nonbytes>, L</assert_nonempty>,
L</assert_nonlist_context>, L</assert_nonnegative>,
L</assert_nonnegative_integer>, L</assert_nonnumeric>,
L</assert_nonobject>, L</assert_nonpositive>,
L</assert_nonpositive_integer>, L</assert_nonref>,
L</assert_nonvoid_context>, L</assert_nonzero>, L</assert_not_in_list>,
L</assert_numeric>, L</assert_object>, L</assert_object_ainta>,
L</assert_object_boolifies>, L</assert_object_can>, L</assert_object_cant>,
L</assert_object_isa>, L</assert_object_method>,
L</assert_object_nummifies>, L</assert_object_overloads>,
L</assert_object_stringifies>, L</assert_odd_number>,
L</assert_open_handle>, L</assert_positive>, L</assert_positive_integer>,
L</assert_private_method>, L</assert_protected_method>,
L</assert_public_method>, L</assert_qualified_ident>, L</assert_refref>,
L</assert_reftype>, L</assert_regex>, L</assert_regular_file>,
L</assert_sad_exit>, L</assert_scalar_context>, L</assert_scalarref>,
L</assert_signalled>, L</assert_signed_number>,
L</assert_simple_perl_ident>, L</assert_single_line>,
L</assert_single_paragraph>, L</assert_text_file>, L</assert_tied>,
L</assert_tied_array>, L</assert_tied_arrayref>, L</assert_tied_glob>,
L</assert_tied_globref>, L</assert_tied_hash>, L</assert_tied_hashref>,
L</assert_tied_referent>, L</assert_tied_scalar>,
L</assert_tied_scalarref>, L</assert_true>, L</assert_unblessed_ref>,
L</assert_undefined>, L</assert_unhappy_code>, L</assert_unicode_ident>,
L</assert_unlike>, L</assert_unlocked>, L</assert_unsignalled>,
L</assert_untied>, L</assert_untied_array>, L</assert_untied_arrayref>,
L</assert_untied_glob>, L</assert_untied_globref>, L</assert_untied_hash>,
L</assert_untied_hashref>, L</assert_untied_referent>,
L</assert_untied_scalar>, L</assert_untied_scalarref>,
L</assert_uppercased>, L</assert_void_context>, L</assert_whole_number>,
L</assert_wide_characters>, and L</assert_zero>.
=item C<:argc>
L</assert_argc>, L</assert_argc_max>, L</assert_argc_min>, and
L</assert_argc_minmax>.
=item C<:array>
L</assert_array_length>, L</assert_array_length_max>,
L</assert_array_length_min>, L</assert_array_length_minmax>,
L</assert_array_nonempty>, L</assert_arrayref>,
L</assert_arrayref_nonempty>, L</assert_list_nonempty>,
L</assert_tied_array>, L</assert_tied_arrayref>, L</assert_untied_array>,
and L</assert_untied_arrayref>.
=item C<:boolean>
L</assert_false>, L</assert_happy_code>, L</assert_true>, and
L</assert_unhappy_code>.
=item C<:case>
L</assert_lowercased> and L</assert_uppercased>.
=item C<:code>
L</assert_coderef>, L</assert_happy_code>, and L</assert_unhappy_code>.
=item C<:context>
L</assert_list_context>, L</assert_nonlist_context>,
L</assert_nonvoid_context>, L</assert_scalar_context>, and
L</assert_void_context>.
=item C<:file>
L</assert_directory>, L</assert_open_handle>, L</assert_regular_file>,
and L</assert_text_file>.
=item C<:glob>
L</assert_globref>, L</assert_tied_glob>, L</assert_tied_globref>,
L</assert_untied_glob>, and L</assert_untied_globref>.
=item C<:hash>
L</assert_hash_keys>, L</assert_hash_keys_allowed>,
L</assert_hash_keys_allowed_and_required>, L</assert_hash_keys_required>,
L</assert_hash_keys_required_and_allowed>, L</assert_hash_nonempty>,
L</assert_hashref>, L</assert_hashref_keys>,
L</assert_hashref_keys_allowed>,
L</assert_hashref_keys_allowed_and_required>,
L</assert_hashref_keys_required>,
L</assert_hashref_keys_required_and_allowed>, L</assert_hashref_nonempty>,
L</assert_keys>, L</assert_locked>, L</assert_max_keys>,
L</assert_min_keys>, L</assert_minmax_keys>, L</assert_tied_hash>,
L</assert_tied_hashref>, L</assert_unlocked>, L</assert_untied_hash>,
and L</assert_untied_hashref>.
=item C<:ident>
L</assert_ascii_ident>, L</assert_full_perl_ident>,
L</assert_known_package>, L</assert_qualified_ident>, and
L</assert_simple_perl_ident>.
=item C<:io>
L</assert_ioref> and L</assert_open_handle>.
=item C<:list>
L</assert_in_list>, L</assert_list_nonempty>, and L</assert_not_in_list>.
=item C<:number>
L</assert_box_number>, L</assert_digits>, L</assert_even_number>,
L</assert_fractional>, L</assert_hex_number>, L</assert_in_numeric_range>,
L</assert_integer>, L</assert_natural_number>, L</assert_negative>,
L</assert_negative_integer>, L</assert_nonnegative>,
L</assert_nonnegative_integer>, L</assert_nonnumeric>,
L</assert_nonpositive>, L</assert_nonpositive_integer>, L</assert_nonzero>,
L</assert_numeric>, L</assert_odd_number>, L</assert_positive>,
L</assert_positive_integer>, L</assert_signed_number>,
L</assert_whole_number>, and L</assert_zero>.
=item C<:object>
L</assert_ainta>, L</assert_can>, L</assert_cant>, L</assert_class_ainta>,
L</assert_class_can>, L</assert_class_cant>, L</assert_class_isa>,
L</assert_class_method>, L</assert_does>, L</assert_doesnt>,
L</assert_isa>, L</assert_known_package>, L</assert_method>,
L</assert_nonobject>, L</assert_object>, L</assert_object_ainta>,
L</assert_object_boolifies>, L</assert_object_can>, L</assert_object_cant>,
L</assert_object_isa>, L</assert_object_method>,
L</assert_object_nummifies>, L</assert_object_overloads>,
L</assert_object_stringifies>, L</assert_private_method>,
L</assert_protected_method>, L</assert_public_method>, L</assert_reftype>,
and L</assert_unblessed_ref>.
=item C<:overload>
L</assert_object_boolifies>, L</assert_object_nummifies>,
L</assert_object_overloads>, and L</assert_object_stringifies>.
=item C<:process>
L</assert_dumped_core>, L</assert_exited>, L</assert_happy_exit>,
L</assert_legal_exit_status>, L</assert_no_coredump>, L</assert_sad_exit>,
L</assert_signalled>, and L</assert_unsignalled>.
=item C<:ref>
L</assert_anyref>, L</assert_arrayref>, L</assert_coderef>,
L</assert_globref>, L</assert_hashref>, L</assert_ioref>,
L</assert_nonref>, L</assert_refref>, L</assert_reftype>,
L</assert_scalarref>, L</assert_tied_arrayref>, L</assert_tied_globref>,
L</assert_tied_hashref>, L</assert_tied_referent>,
lib/Assert/Conditional.pm view on Meta::CPAN
=item assert_hash_nonempty(I<HASH>)
The hash must have at least one key.
=item assert_hashref_nonempty(I<HASHREF>)
The hashref's referent must have at least one key.
=item assert_keys(I<HASH> | I<HASHREF>, I<KEY_LIST>)
=for comment
This is a workaround to create a "blank" line so that the code sample is distinct.
Z<>
my @exact_keys = qw[larry moe curly];
assert_keys(%some_hash, @exact_keys);
The I<HASH> must have all keys in the non-empty I<KEY_LIST> but no others.
This is especially useful when you've got a hash that you're treating as a
"fixed record" data-type, as though it were a C C<struct>: all fields are
guaranteed to be present and nothing else.
This assertion also accepts a I<HASHREF> argument instead, but it still
must be an actual variable.
That is, if instead of a I<HASH> variable is passed as the first argument,
a scalar variable holding a hashref is passed, then the hash referenced is
subject to this constraint. In other words, you get a single level of
auto-dereference to get to the hash, but the price of that is that this
must be an lvalue not an rvalue: it must be an actual variable. For
example:
my @exact_keys = qw[larry moe curly];
assert_keys($some_hashref, @exact_keys);
assert_keys($hash_of_hashes{SOME_FIELD}, @exact_keys);
assert_keys($array_of_hashes[42], @exact_keys);
Perl enforces this at compile-time by making you use either
a C<%> or C<$> sigil on the first argument to this assertion.
For many uses of exact hashes like this, you would be well
advised to lock the hash keys once you've validated them.
use Hash::Util qw(lock_keys);
my @exact_keys = qw[larry moe curly];
assert_keys(%some_hash, @exact_keys);
lock_keys(%some_hash);
or
use Hash::Util qw(lock_ref_keys);
my @exact_keys = qw[larry moe curly];
assert_keys($some_hashref, @exact_keys);
lock_ref_keys($some_hashref);
Now the I<keys> are locked down to keep your honest, although
the I<values> can be still be changed. See L<Hash::Util>.
=item assert_min_keys(I<HASH> | I<HASHREF>, I<KEY_LIST>)
=for comment
This is a workaround to create a "blank" line so that the code sample is distinct.
Z<>
assert_min_keys(%hash, qw[blue green red]);
assert_min_keys($hashref, qw[blue green red]);
Asserts that the hash or hashref argument contains at I<least> the keys
mentioned in the non-empty key list.
=item assert_max_keys(I<HASH> | I<HASHREF>, I<KEY_LIST>)
=for comment
This is a workaround to create a "blank" line so that the code sample is distinct.
Z<>
assert_max_keys(%hash, qw[violet indigo blue cyan green yellow orange red]);
assert_max_keys($hashref, qw[violet indigo blue cyan green yellow orange red]);
Asserts that the hash or hashref argument contains at I<most> the keys
mentioned in the non-empty key list. Consider locking your hash instead of just
checking for unwanted keys. The locking will make sure that no other keys
than these can be added to the hash:
lock_keys(%hash, qw[violet indigo blue cyan green yellow orange red]);
lock_keys_ref($hashref, qw[violet indigo blue cyan green yellow orange red]);
Now you don't have to call L</assert_max_keys> at all.
=item assert_minmax_keys(I<HASH> | I<HASHREF>, I<MIN_ARRAY> | I<MIN_ARRAYREF>, I<MAX_ARRAY> | I<MAX_ARRAYREF>)
=for comment
This is a workaround to create a "blank" line so that the code sample is distinct.
Z<>
@minkeys = qw[red blue green];
@maxkeys = (@minkeys, qw[orange yellow cyan indigo]);
assert_minmax_keys(%hash, @minkeys, @maxkeys);
assert_minmax_keys($hashref, @minkeys, @maxkeys);
Asserts that the hash or hashref argument contains no other keys than the
maximum allowed ones specified, and that all of those from the minimum
required set exist. The arguments must be actual variables (lvalues),
not merely anonymous values.
You can also pass the two pairs of minimum and maximum keys as scalar
variables holding arrayrefs instead:
$minkeyref = \@minkeys;
$maxkeyref = \@maxkeys;
assert_minmax_keys(%hash, $minkeyref, $maxkeyref);
assert_minmax_keys($hashref, $minkeyref, $maxkeyref);
@minmax = ($minkeyref, $maxkeyref);
assert_minmax_keys(%hash, $minmax[0], $minmax[1]);
assert_minmax_keys($hashref, $minmax[0], $minmax[1]);
If you're careful to pass three refs of the right sorts in, you can
actually use this if you circumvent prototype checking:
&assert_minmax_keys(\%hash, @minmax);
&assert_minmax_keys( $hashref, @minmax);
=item assert_locked(I<HASH> | I<HASHREF>)
=for comment
This is a workaround to create a "blank" line.
Z<>
B<WARNING>: Only available under version 0.15 and greater of L<Hash::Util,> first found in perl v5.17.
assert_locked(%hash);
assert_locked($hashref);
assert_locked($array_of_hashes[0]);
assert_locked($arrayref_of_hashes->[0]);
assert_locked($hash_of_hashes{FIELD});
assert_locked($hashref_of_hashes->{FIELD});
The argument, which must be either a hash variable or else a scalar
variable holding a hashref, must have locked keys.
=item assert_unlocked(I<HASH> | I<HASHREF>)
=for comment
This is a workaround to create a "blank" line.
Z<>
B<WARNING>: Only available under version 0.15 and greater of L<Hash::Util>, first found in perl v5.17.
assert_unlocked(%hash);
assert_unlocked($hashref);
assert_unlocked($array_of_hashes[0]);
assert_unlocked($arrayref_of_hashes->[0]);
assert_unlocked($hash_of_hashes{FIELD});
assert_unlocked($hashref_of_hashes->{FIELD});
The argument, which must be either a hash variable or else a scalar
variable holding a hashref, must not have locked keys.
=back
=head2 Legacy Assertions about Hashes
You should usually prefer L</assert_keys>, L</assert_min_keys>,
L</assert_max_keys>, and L</assert_minmax_keys> over the assertions in this
section, since those have better names and aren't so finicky about their
first argument. The following assertions are retained for backwards
compatibility, but internally they all turn into one of those four.
The thing to remember with these is that "required" keys really means I<at
B<least> these keys>, while "allowed" keys really means I<at B<most> these
keys>. If you need those to be the same set, then just use L</assert_keys>
directly.
=over
=item assert_hash_keys(I<HASH>, I<KEY_LIST>)
B<WARNING>: This does not mean what you think it means. Don't use it.
This function is misnamed; it is the deprecated, confusing, legacy version
of L</assert_min_keys>. It really means L</assert_hash_keys_required>,
which in turn means "has at B<most> these keys". It does not mean has these
exact keys and nothing else.
For that, you want L</assert_keys>.
=item assert_hash_keys_required(I<HASH>, I<KEY_LIST>)
=for comment
This is a workaround to create a "blank" line so that the code sample is distinct.
Z<>
assert_hash_keys_required(%hash, qw[name rank serno]);
This is the legacy version of L</assert_min_keys>.
Means "has at B<most> these keys".
Each key specified in the key list must exist in the hash,
but it's ok if there are other non-required keys.
If immediately after you validate the required keys from the I<KEY_LIST>,
you intend to validate the allowed keys using that same I<KEY_LIST> because
you're required to have all your allowed keys:
my @keys = qw[name rank serno];
assert_hash_keys_required(%hash, @keys);
assert_hash_keys_allowed (%hash, @keys);
Then it would be faster to just call L</assert_keys> in the first place.
my @keys = qw[name rank serno];
assert_keys(%hash, @keys);
However, if you plan to lock the hash when you're done validating it, then
you can let the key-locker do the "allowed" step implicitly:
lib/Assert/Conditional.pm view on Meta::CPAN
Compatible to Perl version 5.10 whenever possible. (This didn't pan out; it needs 5.12.)
=back
The initial alpha release was considered completely experimental, but even
so all these goals were met. The only module required that is not part of
the standard Perl release is the underlying L<Exporter::ConditionalSubs>
which this module inherits its import method from. That module is where
(most of) the magic happens to make assertions get compiled out of your
program. You should look at that module for how the "conditional
importing" works.
=head1 SEE ALSO
=over
=item *
The L<Exporter::ConditionalSubs> module which this module is based on.
=item *
The L<Assert::Conditional::Utils> module provides some semi-standalone utility
functions.
=back
=head1 CAVEATS AND PROVISOS
This is a beta release.
=head1 BUGS AND LIMITATIONS
Under versions of Perl previous to v5.12.1, Attribute::Handlers
blows up with an internal error about a symbol going missing.
=head1 HISTORY
0.001 6 June 2015 23:28 MDT
- Initial alpha release
0.002 J June 2015 22:35 MDT
- MONGOLIAN VOWEL SEPARATOR is no longer whitespace in Unicode, so removed from test.
0.003 Tue Jun 30 05:47:16 MDT 2015
- Added assert_hash_keys_required and assert_hash_keys_allowed.
- Fixed some tests.
- Added bug report about Attribute::Handlers bug prior to 5.12.
0.004 11 Feb 2018 11:18 MST
- Suppress overloading in botch messages for object-related assertions (but not others).
- Don't carp if we're throwing an exception and exceptions are trapped.
- Support more than one word in ASSERT_CONDITIONAL (eg: "carp,always").
- If ASSERT_CONDITIONAL contains "handlers", don't block @SIG{__{WARN,DIE}__}.
- Don't let assert_isa die prematurely on an unblessed ref.
0.005 Sun May 20 20:40:25 CDT 2018
- Initial beta release.
- Reworked the hash key checkers into a simpler set: assert_keys, assert_min_keys, assert_max_keys, assert_minmax_keys.
- Added invocant-specific assertions: assert_{object,class}_{isa,ainta,can,cant}.
- Added assertions for ties, overloads, and locked hashes.
- Made assert_private_method work despite Moose wrappers.
- Added assert_protected_method that works despite Moose wrappers and roles.
- Improved the looks of the uncompiled code for assert_happy_code.
- Fixed botch() to identify the most distant stack frame not the nearest for the name of the failed assertion.
- Improved the reporting of some assertion failures.
0.006 Mon May 21 07:45:43 CDT 2018
- Use hash_{,un}locked not hashref_{,un}locked to support pre-5.16 perls.
- Unhid assert_unblessed_ref swallowed up by stray pod.
0.007 Mon May 21 19:13:58 CDT 2018
- Add missing Hash::Util version requirement for old perls to get hashref_unlock imported.
0.008 Tue May 22 11:51:37 CDT 2018
- Rewrite hash_unlocked missing till 5.16 as !hash_locked
- Add omitted etc/generate-exporter-pod to MANIFEST
0.009 Tue Aug 21 06:29:56 MDT 2018
- Delay slow calls to uca_sort till you really need them, credit Larry Leszczynski.
0.010 Sun Jul 19 13:52:00 MDT 2020
- Fix coredump in perl 5.12 by replacing UNITCHECK in Assert::Conditional::Util with normal execution at botton.
- Make perls below 5.18 work again by setting Hash::Util prereq in Makefile.PL to 0 because it's in the core only, never cpan.
- Only provide assert_locked and assert_unlocked if core Hash::Util v0.15 is there (starting perl v5.17).
- Bump version req of parent class Exporter::ConditionalSubs to v1.11.1 so we don't break Devel::Cover.
- Normalize Export sub attribute tracing so either $Exporter::Verbose=1 or env ASSERT_CONDITIONAL_DEBUG=1 work for both Assert::Conditional{,::Utils}.
- Mentioned $Exporter::Verbose support.
=head1 AUTHOR
Tom Christiansen C<< <tchrist53147@gmail.com> >>
Thanks to Larry Leszczynski at Grant Street Group for making this module
possible. Without it, my programs would be much slower, since before I
added his module to my old and pre-existing assertion system, the
assertions alone were taking up far too much CPU time.
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2015-2018, Tom Christiansen C<< <tchrist@perl.com> >>.
All Rights Reserved.
This module is free software; you can redistribute it and/or
( run in 1.015 second using v1.01-cache-2.11-cpan-e1769b4cff6 )