Skip to content

Commit

Permalink
Add unit tests for TOML placeholder parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Jul 22, 2024
1 parent 10207e6 commit 4e05ad7
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test = [
"pyright==1.1.361",
"pytest~=7.4",
"pytest-dependency~=0.5",
"pytest-describe~=2.2",
"syrupy~=4.6",
"copier_template_tester",
"pyyaml-include<2", # https://github.com/copier-org/copier/issues/1568
Expand Down
1 change: 1 addition & 0 deletions src/hexdoc/utils/deserialize/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


def fill_placeholders(data: TOMLDict):
"""Replaces hexdoc-style string placeholders in-place in a parsed TOML file."""
_fill_placeholders(data, [data], set())


Expand Down
183 changes: 183 additions & 0 deletions test/utils/deserialize/test_toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# pyright: reportUnusedFunction=none

import copy

import pytest
from hexdoc.utils.deserialize.toml import TOMLDict, fill_placeholders


def _fill_placeholders_assert_valid(data: TOMLDict, want: TOMLDict):
got = copy.deepcopy(data)
fill_placeholders(got)
assert got == want


def describe_fill_placeholders():
def describe_valid_input():
@pytest.mark.parametrize(
["data", "want"],
[
[{}, {}],
[{"": ""}, {"": ""}],
[{"key": "value"}, {"key": "value"}],
],
)
def no_placeholders(data: TOMLDict, want: TOMLDict):
_fill_placeholders_assert_valid(data, want)

@pytest.mark.parametrize(
["data", "want"],
[
[
{"key": "{var}", "var": "value"},
{"key": "value", "var": "value"},
],
[
{"key": "foo {var} bar", "var": "value"},
{"key": "foo value bar", "var": "value"},
],
[
{"key": "foo {var} bar {var}", "var": "value"},
{"key": "foo value bar value", "var": "value"},
],
],
)
def simple_placeholders(data: TOMLDict, want: TOMLDict):
_fill_placeholders_assert_valid(data, want)

@pytest.mark.parametrize(
["data", "want"],
[
[
{"foo": {"key": "{^var}"}, "var": "value"},
{"foo": {"key": "value"}, "var": "value"},
],
[
{"foo": {"bar": {"key": "{^^var}"}}, "var": "value"},
{"foo": {"bar": {"key": "value"}}, "var": "value"},
],
[
{"foo": {"bar": {"baz": {"key": "{^^^var}"}}}, "var": "value"},
{"foo": {"bar": {"baz": {"key": "value"}}}, "var": "value"},
],
],
)
def parent_references(data: TOMLDict, want: TOMLDict):
_fill_placeholders_assert_valid(data, want)

@pytest.mark.parametrize(
["data", "want"],
[
[
{"key": "{$var}", "var": "value"},
{"key": "value", "var": "value"},
],
[
{"foo": {"key": "{$var}"}, "var": "value"},
{"foo": {"key": "value"}, "var": "value"},
],
[
{"foo": {"bar": {"key": "{$var}"}}, "var": "value"},
{"foo": {"bar": {"key": "value"}}, "var": "value"},
],
[
{"foo": {"bar": {"baz": {"key": "{$var}"}}}, "var": "value"},
{"foo": {"bar": {"baz": {"key": "value"}}}, "var": "value"},
],
],
)
def root_references(data: TOMLDict, want: TOMLDict):
_fill_placeholders_assert_valid(data, want)

@pytest.mark.parametrize(
["data", "want"],
[
[
{"foo": ["{var}"], "var": "value"},
{"foo": ["value"], "var": "value"},
],
[
{"foo": [{"key": "{^var}"}], "var": "value"},
{"foo": [{"key": "value"}], "var": "value"},
],
[
{"foo": ["{$var}"], "var": "value"},
{"foo": ["value"], "var": "value"},
],
[
{"foo": [{"key": "{$var}"}], "var": "value"},
{"foo": [{"key": "value"}], "var": "value"},
],
],
)
def arrays(data: TOMLDict, want: TOMLDict):
_fill_placeholders_assert_valid(data, want)

def describe_intrinsic_functions():
@pytest.mark.parametrize(
["data", "want"],
[
[
{"key": {"!Raw": ""}},
{"key": ""},
],
[
{"key": {"!Raw": "value"}},
{"key": "value"},
],
[
{"key": {"!Raw": "{var}"}, "var": "value"},
{"key": "{var}", "var": "value"},
],
[
{"key": {"!Raw": "^{.+}$"}},
{"key": "^{.+}$"},
],
[
{"key": {"!Raw": {}}},
{"key": {}},
],
[
{"key": {"!Raw": {"!Raw": "value"}}},
{"key": {"!Raw": "value"}},
],
],
)
def raw(data: TOMLDict, want: TOMLDict):
_fill_placeholders_assert_valid(data, want)

@pytest.mark.parametrize(
["data", "want"],
[
[
{"key": {"!None": ""}},
{"key": None},
],
[
{"key": {"!None": "value"}},
{"key": None},
],
[
{"key": {"!None": {}}},
{"key": None},
],
],
)
def none(data: TOMLDict, want: TOMLDict):
_fill_placeholders_assert_valid(data, want)

@pytest.mark.parametrize(
["data", "want_err"],
[
[{"key": "{var}"}, KeyError],
pytest.param(
{"key": "{^var}", "var": "value"},
KeyError,
marks=pytest.mark.xfail,
),
[{"key": "{var}", "var": 0}, TypeError],
],
)
def invalid_input(data: TOMLDict, want_err: type[Exception]):
with pytest.raises(want_err):
fill_placeholders(data)

0 comments on commit 4e05ad7

Please sign in to comment.