Skip to content

Commit

Permalink
more systematic tests of bad template filling
Browse files Browse the repository at this point in the history
  • Loading branch information
mccalluc committed Nov 19, 2024
1 parent e2beaa2 commit f93304f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
12 changes: 8 additions & 4 deletions dp_wizard/utils/code_generators/_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ def fill_expressions(self, **kwargs):
k_re = re.escape(k)
self._template, count = re.subn(rf"\b{k_re}\b", str(v), self._template)
if count == 0:
raise Exception(f"No slot for '{k}' in {self._path}")
raise Exception(
f"No '{k}' slot to fill with '{v}' in '{self._path}':\n\n{self._template}"
)
return self

def fill_values(self, **kwargs):
for k, v in kwargs.items():
k_re = re.escape(k)
self._template, count = re.subn(rf"\b{k_re}\b", repr(v), self._template)
if count == 0:
raise Exception(f"No slot for '{k}' in {self._path}")
raise Exception(
f"No '{k}' slot to fill with '{v}' in '{self._path}':\n\n{self._template}"
)
return self

def fill_blocks(self, **kwargs):
Expand All @@ -62,8 +66,8 @@ def match_indent(match):
def __str__(self):
unfilled_slots = self._initial_slots & self._find_slots()
if unfilled_slots:
slots_str = ", ".join(sorted(f"'{slot}'" for slot in unfilled_slots))
raise Exception(
f"Template {self._path} has unfilled slots: "
f'{", ".join(sorted(unfilled_slots))}\n\n{self._template}'
f"{slots_str} slot not filled in '{self._path}':\n\n{self._template}"
)
return self._template
57 changes: 48 additions & 9 deletions tests/utils/test_code_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ def test_fill_expressions():
assert filled == "No one expects the Spanish Inquisition!"


def test_fill_expressions_missing_slot_in_template():
template = Template(None, template="No one ... the ADJ NOUN!")
with pytest.raises(Exception, match=r"No 'VERB' slot to fill with 'expects'"):
str(
template.fill_expressions(
VERB="expects",
ADJ="Spanish",
NOUN="Inquisition",
)
)


def test_fill_expressions_extra_slot_in_template():
template = Template(None, template="No one VERB ARTICLE ADJ NOUN!")
with pytest.raises(Exception, match=r"'ARTICLE' slot not filled"):
str(
template.fill_expressions(
VERB="expects",
ADJ="Spanish",
NOUN="Inquisition",
)
)


def test_fill_values():
template = Template(None, template="assert [STRING] * NUM == LIST")
filled = str(
Expand All @@ -46,6 +70,30 @@ def test_fill_values():
assert filled == "assert ['🙂'] * 3 == ['🙂', '🙂', '🙂']"


def test_fill_values_missing_slot_in_template():
template = Template(None, template="assert [STRING] * ... == LIST")
with pytest.raises(Exception, match=r"No 'NUM' slot to fill with '3'"):
str(
template.fill_values(
STRING="🙂",
NUM=3,
LIST=["🙂", "🙂", "🙂"],
)
)


def test_fill_values_extra_slot_in_template():
template = Template(None, template="CMD [STRING] * NUM == LIST")
with pytest.raises(Exception, match=r"'CMD' slot not filled"):
str(
template.fill_values(
STRING="🙂",
NUM=3,
LIST=["🙂", "🙂", "🙂"],
)
)


def test_fill_blocks():
# "OK" is less than three characters, so it is not a slot.
template = Template(
Expand Down Expand Up @@ -85,15 +133,6 @@ def test_fill_blocks():
)


def test_fill_template_unfilled_slots():
context_template = Template("context")
with pytest.raises(
Exception,
match=re.escape("context.py has unfilled slots"),
):
str(context_template.fill_values())


def test_make_notebook():
notebook = NotebookGenerator(
AnalysisPlan(
Expand Down

0 comments on commit f93304f

Please sign in to comment.