Skip to content

Commit

Permalink
[cdd/tests/test_shared/test_pure_utils.py] Test `ensure_valid_identif…
Browse files Browse the repository at this point in the history
…ier` ; [cdd/shared/pure_utils.py] Improve `ensure_valid_identifier`
  • Loading branch information
SamuelMarks committed Sep 19, 2023
1 parent b2c0811 commit e51ff3b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cdd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from logging import getLogger as get_logger

__author__ = "Samuel Marks"
__version__ = "0.0.99rc8"
__version__ = "0.0.99rc9"
__description__ = (
"Open API to/fro routes, models, and tests. "
"Convert between docstrings, classes, methods, argparse, pydantic, and SQLalchemy."
Expand Down
5 changes: 3 additions & 2 deletions cdd/shared/pure_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Pure utils for pure functions. For the same input will always produce the same input_str.
"""

import string
import typing
from ast import Name, Str
from collections import deque
Expand Down Expand Up @@ -1057,8 +1058,8 @@ def ensure_valid_identifier(s):
return "{}_".format(s)
elif s[0].isdigit():
s = "_{}".format(s)
valid = frozenset("_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
return "".join(filter(valid.__contains__, s))
valid = frozenset("_{}{}".format(string.ascii_letters, string.digits))
return "".join(filter(valid.__contains__, s)) or "_"


def set_attr(obj, key, val):
Expand Down
14 changes: 14 additions & 0 deletions cdd/tests/test_shared/test_pure_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
blockwise,
deindent,
diff,
ensure_valid_identifier,
find_module_filepath,
get_module,
identity,
Expand Down Expand Up @@ -69,6 +70,19 @@ def test_blockwise(self) -> None:
self.assertTupleEqual(tuple(blockwise("ABC")), (("A", "B"), ("C", None)))
self.assertTupleEqual(tuple(blockwise("ABCD")), (("A", "B"), ("C", "D")))

def test_ensure_valid_identifier(self) -> None:
"""Tests that `ensure_valid_identifier` works"""
self.assertEqual(ensure_valid_identifier("_5"), "_5")
for ident in "foo", "bar", "can", "haz_", "_", "_5", "_a":
self.assertEqual(ensure_valid_identifier(ident), ident)
for ident in "6", "5":
self.assertEqual(ensure_valid_identifier(ident), "_{}".format(ident))
for ident in "for", "while", "break", "continue", "def", "class":
self.assertEqual(ensure_valid_identifier(ident), "{}_".format(ident))
for ident in "$", "-", "-%":
self.assertEqual(ensure_valid_identifier(ident), "_")
self.assertEqual(ensure_valid_identifier(""), "_")

def test_find_module_filepath(self) -> None:
"""tests that it can `find_module_filepath`"""
self.assertEqual(
Expand Down

0 comments on commit e51ff3b

Please sign in to comment.