Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
descriptive warning when using the eq overloaded operator on non-nume…
…ric strings Instead of saying "isn't numeric in null operation", provide a more descriptive warning that indicates the value and the operator being used. See <http://sourceforge.net/p/pdl/bugs/332>, <#33> for more information.
- Loading branch information
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use Test::More tests => 5; | ||
use Test::Warn; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
## Issue information | ||
## | ||
## Name: "isn't numeric in null operation" warning could be more helpful | ||
## | ||
## <http://sourceforge.net/p/pdl/bugs/332/> | ||
## <https://github.com/PDLPorters/pdl/issues/33> | ||
|
||
use PDL::Config; | ||
use PDL::LiteF; | ||
|
||
# The following code calls the PDL::Ops::eq() function via the operator | ||
# overload for the eq operator. Because the Perl eq operator is usually used | ||
# for strings, the default warning of "isn't numeric in null operation" is | ||
# confusing. Comparing a PDL against a string should give a more useful | ||
# warning. | ||
|
||
my $numeric_warning = [qr/not numeric nor a PDL/]; | ||
my $no_warning = undef; | ||
|
||
sub check_eq_warnings { | ||
my ($string, $warning) = @_; | ||
warning_like { (pdl() eq $string); } $warning; | ||
warning_like { ($string eq pdl()); } $warning; | ||
} | ||
|
||
subtest "String 'x' is not numeric and should warn" => sub { | ||
check_eq_warnings('x', $numeric_warning); | ||
}; | ||
subtest "String 'nancy' is not numeric and should warn" => sub { | ||
check_eq_warnings('nancy', $numeric_warning); | ||
}; | ||
subtest "String 'inf' is numeric" => sub { | ||
check_eq_warnings('inf', $no_warning); | ||
}; | ||
subtest "String 'nan' is numeric" => sub { | ||
check_eq_warnings('nan', $no_warning); | ||
}; | ||
TODO: { | ||
# implementing this might require checking for strings that can be made into PDLs | ||
local $TODO = "Using the eq operator with the string 'bad' might be a good feature"; | ||
todo_skip "Bad values disabled", 1 unless $PDL::Config{WITH_BADVAL}; | ||
|
||
subtest "String 'bad' is numeric (in PDL)" => sub { | ||
check_eq_warnings('bad', $no_warning); | ||
}; | ||
} | ||
|
||
done_testing; |