Skip to content

Commit

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

Expand All @@ -41,7 +42,8 @@ def fill_values(self, **kwargs):
self._template, count = re.subn(rf"\b{k_re}\b", repr(v), self._template)
if count == 0:
raise Exception(
f"No '{k}' slot to fill with '{v}' in '{self._path}':\n\n{self._template}"
f"No '{k}' slot to fill with '{v}' in "
f"'{self._path}':\n\n{self._template}"
)
return self

Expand All @@ -55,12 +57,23 @@ def match_indent(match):
)

k_re = re.escape(k)
self._template = re.sub(
self._template, count = re.subn(
rf"^([ \t]*){k_re}$",
match_indent,
self._template,
flags=re.MULTILINE,
)
if count == 0:
base_message = (
f"No '{k}' slot to fill with '{v}' in "
f"'{self._path}':\n\n{self._template}"
)
if k in self._template:
raise Exception(
f"Block slots must be alone on line; {base_message}"
)
else:
raise Exception(base_message)
return self

def __str__(self):
Expand Down
21 changes: 20 additions & 1 deletion tests/utils/test_code_generators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from tempfile import NamedTemporaryFile
import subprocess
from pathlib import Path
import re
import pytest
import opendp.prelude as dp
from dp_wizard.utils.code_generators import (
Expand Down Expand Up @@ -133,6 +132,26 @@ def test_fill_blocks():
)


def test_fill_blocks_missing_slot_in_template_alone():
template = Template(None, template="No block slot")
with pytest.raises(Exception, match=r"No 'SLOT' slot"):
str(template.fill_blocks(SLOT="placeholder"))


def test_fill_blocks_missing_slot_in_template_not_alone():
template = Template(None, template="No block SLOT")
with pytest.raises(
Exception, match=r"Block slots must be alone on line; No 'SLOT' slot"
):
str(template.fill_blocks(SLOT="placeholder"))


def test_fill_blocks_extra_slot_in_template():
template = Template(None, template="EXTRA\nSLOT")
with pytest.raises(Exception, match=r"'EXTRA' slot not filled"):
str(template.fill_blocks(SLOT="placeholder"))


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

0 comments on commit 251060c

Please sign in to comment.