Skip to content

Commit

Permalink
give meaningful error if undef arg given
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed May 5, 2024
1 parent 140e978 commit 713f334
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/PDL/Graphics/Simple.pm
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ sub _translate_plot {
barf "plot: requires at least one argument to plot!\n" if !@_;
barf "plot: requires at least one argument to plot, in addition to plot options\n"
if @_ == 1 and ref($_[0]) eq 'HASH';
barf "Undefined value given in plot args" if grep !defined(), @_;

##############################
# Collect plot options. These can be in a leading or trailing
Expand Down Expand Up @@ -906,16 +907,16 @@ sub _translate_plot {

##############################
# Now check options
barf sprintf("plot style %s requires %d or %d columns; you gave %d\n",$ptn,$pt->{args}->[0],$pt->{args}->[1],0+@args)
barf sprintf("plot style %s requires %d or %d columns; you gave %d\n",$ptn,$pt->{args}[0],$pt->{args}[1],0+@args)
if @args != $pt->{args}[0] and @args != $pt->{args}[1];

# Add an index variable if needed
if(defined($pt->{args}->[1])) {
if( $pt->{args}->[1] - @args == 2 ) {
if( $pt->{args}[1] - @args == 2 ) {
my @dims = ($args[0]->slice(":,:")->dims)[0,1];
unshift @args, xvals(@dims), yvals(@dims);
}
if( $pt->{args}->[1] - @args == 1 ) {
if( $pt->{args}[1] - @args == 1 ) {
unshift @args, xvals($args[0]);
}
}
Expand Down
4 changes: 4 additions & 0 deletions t/simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ sub ask_yn {
}

# error handling
eval {
PDL::Graphics::Simple::_translate_plot(undef, undef, with=>'line', undef);
};
like $@, qr/Undefined value/i;
eval {
PDL::Graphics::Simple::_translate_plot(undef, undef, with=>'NEVER_USED');
};
Expand Down

0 comments on commit 713f334

Please sign in to comment.