mirror of https://github.com/zulip/zulip.git
validators: Make to_positive_or_allowed_int an optional argument.
This commit is contained in:
parent
d88f1103b7
commit
772500d1c6
|
@ -465,10 +465,10 @@ def to_non_negative_int(s: str, max_int_size: int = 2 ** 32 - 1) -> int:
|
|||
return x
|
||||
|
||||
|
||||
def to_positive_or_allowed_int(allowed_integer: int) -> Callable[[str], int]:
|
||||
def to_positive_or_allowed_int(allowed_integer: Optional[int] = None) -> Callable[[str], int]:
|
||||
def converter(s: str) -> int:
|
||||
x = int(s)
|
||||
if x == allowed_integer:
|
||||
if allowed_integer is not None and x == allowed_integer:
|
||||
return x
|
||||
if x == 0:
|
||||
raise ValueError("argument is 0")
|
||||
|
|
|
@ -787,12 +787,20 @@ class ValidatorTestCase(ZulipTestCase):
|
|||
self.assertEqual(to_non_negative_int(str(2 ** 32)))
|
||||
|
||||
def test_to_positive_or_allowed_int(self) -> None:
|
||||
self.assertEqual(to_positive_or_allowed_int()("5"), 5)
|
||||
self.assertEqual(to_positive_or_allowed_int(-1)("5"), 5)
|
||||
|
||||
self.assertEqual(to_positive_or_allowed_int(-1)("-1"), -1)
|
||||
with self.assertRaisesRegex(ValueError, "argument is negative"):
|
||||
to_positive_or_allowed_int(-1)("-5")
|
||||
with self.assertRaisesRegex(ValueError, "argument is negative"):
|
||||
to_positive_or_allowed_int()("-5")
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
to_positive_or_allowed_int(-1)("0")
|
||||
with self.assertRaises(ValueError):
|
||||
to_positive_or_allowed_int()("0")
|
||||
self.assertEqual(to_positive_or_allowed_int(0)("0"), 0)
|
||||
|
||||
def test_check_float(self) -> None:
|
||||
x: Any = 5.5
|
||||
|
|
Loading…
Reference in New Issue