From a5d9664a9264f45b088cc363331b391a40779b40 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Fri, 17 Feb 2023 01:06:26 -0600 Subject: finish testing and polishing, add readme --- tests/conftest.py | 14 ++++++++++++ tests/test_pymdvar.py | 61 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 tests/conftest.py (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..df33848 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,14 @@ +import pytest + + +@pytest.fixture +def single_var_dict(): + return {'test': 'value'} + + +@pytest.fixture +def multi_var_dict(): + return {'ext': 'jpg', + 'test': 'value', + 'TEST': 'VALUE', + 'SOMETHING_ELSE': 'something_else'} diff --git a/tests/test_pymdvar.py b/tests/test_pymdvar.py index 91a97d0..a4c4439 100644 --- a/tests/test_pymdvar.py +++ b/tests/test_pymdvar.py @@ -1,3 +1,4 @@ +import os import pytest from markdown import markdown from pymdvar import VariableExtension @@ -5,31 +6,73 @@ from pymdvar import VariableExtension def test_empty_input(): in_str: str = '' + exp_str: str = markdown(in_str) out_str: str = markdown(in_str, extensions=[VariableExtension()]) - assert in_str == out_str + assert out_str == exp_str @pytest.mark.parametrize('in_str, exp_str', [ ('foo bar', '

foo bar

'), ('foo *test* bar', '

foo test bar

'), - ('foo **test** bar', '

foo test bar

'), + ('foo *PYMDVAR_TEST_1* bar', '

foo PYMDVAR_TEST_1 bar

'), ('foo $test bar', '

foo $test bar

'), - ('foo *${test* bar', '

foo ${test bar

'), ('foo **$test}** bar', '

foo $test} bar

'), + ('foo **$PYMDVAR_TEST_2}** bar', '

foo $PYMDVAR_TEST_2} bar

'), + ('foo [link](${test/a.html) bar', '

foo link bar

'), + ('foo ![image]($test}/a.jpg) bar', '

foo image bar

') +]) +def test_no_replacements_config(in_str: str, exp_str: str, single_var_dict: dict[str, str]): + out_str: str = markdown(in_str, extensions=[VariableExtension(variables=single_var_dict, enable_env=True)]) + assert out_str == exp_str + + +@pytest.mark.parametrize('in_str, exp_str', [ + ('foo *${test}* bar', '

foo bar

'), + ('foo *${PYMDVAR_TEST_1}* bar', '

foo bar

'), + ('foo [link](${test}/a.html) bar', '

foo link bar

'), ]) -def test_non_replacements(in_str, exp_str): +def test_replacements_no_config(in_str: str, exp_str: str): out_str: str = markdown(in_str, extensions=[VariableExtension()]) assert out_str == exp_str @pytest.mark.parametrize('in_str, exp_str', [ - ('foo ${test} bar', '

foo value bar

'), ('foo *${test}* bar', '

foo value bar

'), - ('foo **${test}** bar', '

foo value bar

'), + ('foo *${PYMDVAR_TEST_1}* bar', '

foo 1 bar

'), + ('foo ${value} bar', '

foo bar

'), + ('foo **${PYMDVAR_TEST_2}** bar', '

foo bar

'), ('foo [link](${test}/a.html) bar', '

foo link bar

'), - ('foo ![image](${test}/a.jpg) bar', '

foo image bar

'), + ('foo ![image](${PYMDVAR_TEST_1}/a.jpg) bar', '

foo image bar

'), + ('foo [link](${PYMDVAR_TEST_2}/a.html) bar', '

foo link bar

') ]) -def test_simple_replacements(in_str, exp_str): - out_str: str = markdown(in_str, extensions=[VariableExtension(variables={'test':'value'})]) +def test_simple_replacements(in_str: str, exp_str: str, single_var_dict: dict[str, str]): + out_str: str = markdown(in_str, extensions=[VariableExtension(variables=single_var_dict, enable_env=True)]) assert out_str == exp_str + +@pytest.mark.parametrize('in_str, exp_str', [ + ('foo *${test}* bar', '

foo value bar

'), + ('foo *${TEST}* bar', '

foo VALUE bar

'), + ('foo **${SOMETHING_ELSE}** bar', '

foo something_else bar

') +]) +def test_multivar_replacements(in_str: str, exp_str: str, multi_var_dict: dict[str, str]): + out_str: str = markdown(in_str, extensions=[VariableExtension(variables=multi_var_dict)]) + assert out_str == exp_str + + +@pytest.mark.parametrize('in_str, exp_str', [ + ('foo *${test}* and **${TEST}** bar', '

foo value and VALUE bar

'), + ('foo ![image](${PYMDVAR_TEST_1}/a.${ext}) bar', '

foo image bar

') +]) +def test_text_multivar_replacements(in_str: str, exp_str: str, multi_var_dict: dict[str, str]): + out_str: str = markdown(in_str, extensions=[VariableExtension(variables=multi_var_dict)]) + assert out_str == exp_str + + +@pytest.mark.parametrize('in_str, exp_str', [ + ('foo *${test}* and **${TEST}** bar', '

foo value and VALUE bar

'), + ('foo ![image](${PYMDVAR_TEST_1}/a.${ext}) bar', '

foo image bar

') +]) +def test_extension_text_mode(in_str: str, exp_str: str, multi_var_dict: dict[str, str]): + out_str: str = markdown(in_str, extensions=['pymdvar'], extension_configs={'pymdvar': {'variables': multi_var_dict}}) + assert out_str == exp_str -- cgit v1.2.3-70-g09d2