import os import pytest from markdown import markdown 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 out_str == exp_str @pytest.mark.parametrize('in_str, exp_str', [ ('foo bar', '
foo 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 **$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 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_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 *${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](${PYMDVAR_TEST_1}/a.jpg) bar', 'foo bar
'), ('foo [link](${PYMDVAR_TEST_2}/a.html) bar', 'foo link bar
') ]) 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 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 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