From 37eeac62b1b8974ab4d1b3c16ffe746f4161a2ed Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Fri, 15 Aug 2025 17:46:49 +0200 Subject: [PATCH] tests: parameterize Signed-off-by: Jan Kowalleck --- tests/test_syntax.py | 54 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/test_syntax.py b/tests/test_syntax.py index d71923c..ee81b2f 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -4,40 +4,40 @@ import pytest -@pytest.fixture def valid_syntax_data(): with open(Path("tests", "valid_syntax.json"), "r", encoding="utf-8") as f: return json.load(f) -@pytest.fixture def invalid_syntax_data(): with open(Path("tests", "invalid_syntax.json"), "r", encoding="utf-8") as f: return json.load(f) -def test_is_valid_syntax(valid_syntax_data): - for term, valid_examples in valid_syntax_data.items(): - for valid_example in valid_examples: - actual = h.is_valid_syntax(term=term, value=valid_example["value"]) - print("") - print( - f"Testing {term} with {valid_example['value']} : {valid_example['reason']}" - ) - assert ( - actual is True - ), f"Failed term: {term} for '{valid_example['value']}' : {valid_example['reason']}" - assert True - - -def test_not_is_valid_syntax(invalid_syntax_data): - for term, invalid_examples in invalid_syntax_data.items(): - for invalid_example in invalid_examples: - actual = h.is_valid_syntax(term=term, value=invalid_example["value"]) - print("") - print( - f"Testing {term} with {invalid_example['value']} : {invalid_example['reason']}" - ) - assert ( - actual is False - ), f"Failed term: {term} for '{invalid_example['value']}' : {invalid_example['reason']}" +def syntax_data_as_params(src_cb): + for term, examples in src_cb().items(): + for example in examples: + yield pytest.param(term, example, id=f"{term}-{example['value']}") + + +@pytest.mark.parametrize("term,valid_example", syntax_data_as_params(valid_syntax_data)) +def test_is_valid_syntax(term, valid_example): + actual = h.is_valid_syntax(term=term, value=valid_example["value"]) + print("") + print( + f"Testing {term} with {valid_example['value']} : {valid_example['reason']}" + ) + assert ( + actual is True + ), f"Failed term: {term} for '{valid_example['value']}' : {valid_example['reason']}" + +@pytest.mark.parametrize("term,invalid_example", syntax_data_as_params(invalid_syntax_data)) +def test_not_is_valid_syntax(term, invalid_example): + actual = h.is_valid_syntax(term=term, value=invalid_example["value"]) + print("") + print( + f"Testing {term} with {invalid_example['value']} : {invalid_example['reason']}" + ) + assert ( + actual is False + ), f"Failed term: {term} for '{invalid_example['value']}' : {invalid_example['reason']}"