Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grass.pygrass: Fix parameter range error reporting #2817

Merged
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions python/grass/pygrass/modules/interface/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,22 @@ def check_string(value):
if (param.min is not None and newvalue < param.min) or (
param.max is not None and newvalue > param.max
):
err_str = (
"The Parameter <%s>, must be between: "
"%g<=value<=%g, %r is outside."
)
raise ValueError(err_str % (param.name, param.min, param.max, newvalue))
if param.min is None:
err_str = (
f"The Parameter <{param.name}> must be lower than "
f"{param.max}, {newvalue} is outside."
)
elif param.max is None:
err_str = (
f"The Parameter <{param.name}> must be higher than "
f"{param.min}, {newvalue} is out of range."
)
else:
err_str = (
f"The Parameter <{param.name}> must be between: "
f"{param.min}<=value<={param.max}, {newvalue} is outside."
)
raise ValueError(err_str)
# check if value is in the list of valid values
if param.values is not None and newvalue not in param.values:
raise ValueError(must_val % (param.name, param.values))
Expand Down
78 changes: 78 additions & 0 deletions python/grass/pygrass/modules/interface/testsuite/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,84 @@ def test_range_float_double(self):
with self.assertRaises(ValueError):
_check_value(param, 2.6)

def test_positive_min_float_double(self):
"""Check range checking for a positive minimum.
Tests positive cases, type of exception, and content of the message.
"""
name = "number"
for ptype in ("float", "double"):
param = Parameter(
diz=dict(
name=name,
required="yes",
multiple="no",
type=ptype,
values=[
"2-",
],
)
)
value = 2
self.assertTupleEqual((float(value), value), _check_value(param, value))
value = 2.2
self.assertTupleEqual((value, value), _check_value(param, value))
value = "2"
self.assertTupleEqual((float(value), value), _check_value(param, value))
value = "2.5"
self.assertTupleEqual((float(value), value), _check_value(param, value))

# test errors
with self.assertRaisesRegex(ValueError, f"{name}.+elev"):
_check_value(param, "elev")
with self.assertRaisesRegex(TypeError, name):
_check_value(param, (1.0, 2.0))
# Only the main parts of the message are checked,
# but the check is order-dependent.
with self.assertRaisesRegex(ValueError, f"{name}.+1.9"):
_check_value(param, 1.9)
with self.assertRaisesRegex(ValueError, f"{name}.+-1.0"):
_check_value(param, -1.0)

def test_positive_max_float_double(self):
"""Check range checking for a positive maximum.
Tests positive cases, type of exception, and content of the message.
"""
name = "number"
for ptype in ("float", "double"):
param = Parameter(
diz=dict(
name=name,
required="yes",
multiple="no",
type=ptype,
values=[
"-100",
],
)
)
value = 1
self.assertTupleEqual((float(value), value), _check_value(param, value))
value = 1.2
self.assertTupleEqual((value, value), _check_value(param, value))
value = "0"
self.assertTupleEqual((float(value), value), _check_value(param, value))
value = "2.5"
self.assertTupleEqual((float(value), value), _check_value(param, value))

# test errors
with self.assertRaisesRegex(ValueError, f"{name}.+elev"):
_check_value(param, "elev")
with self.assertRaisesRegex(TypeError, name):
_check_value(param, (1.0, 2.0))
# Only the main parts of the message are checked,
# but the check is order-dependent.
with self.assertRaisesRegex(ValueError, f"{name}.+100.1"):
_check_value(param, 100.1)
with self.assertRaisesRegex(ValueError, f"{name}.+200"):
_check_value(param, 200.0)

def test_single_integer(self):
param = Parameter(
diz=dict(name="int_number", required="yes", multiple="no", type="integer")
Expand Down