Skip to content

Commit

Permalink
Add parameter checks for stringify_set
Browse files Browse the repository at this point in the history
  • Loading branch information
syxolk committed Jan 2, 2018
1 parent bd33377 commit d355def
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vladiate/test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ def test_all_validators_support_empty_ok(validator_class, args):
({'A', 'B', 'C'}, 2, "{'A', 'B'} (1 more suppressed)"),
({'A', 'B', 'C'}, 0, "{} (3 more suppressed)"),
({}, 5, "{}"),
({}, 0, "{}"),
])
def test_stringify_set(a_set, max_len, stringified):
assert stringify_set(a_set, max_len) == stringified


def test_stringify_set_invalid_params():
with pytest.raises(ValueError):
stringify_set({}, -1, 10)
with pytest.raises(ValueError):
stringify_set({}, 10, -1)
5 changes: 5 additions & 0 deletions vladiate/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ def stringify_set(a_set, max_len, max_sort_size=8192):
Large sets won't be sorted for performance reasons.
This may result in an arbitrary ordering in the returned string.
'''
if max_len < 0:
raise ValueError("max_len must be non-negative: {}".format(max_len))
if max_sort_size < 0:
raise ValueError(
"max_sort_size must be non-negative: {}".format(max_sort_size))
# Don't convert `a_set` to a list for performance reasons
text = "{{{}}}".format(", ".join(
"'{}'".format(value) for value in islice(
Expand Down

0 comments on commit d355def

Please sign in to comment.