Added all preparser tests

This commit is contained in:
dandaedre 2022-02-20 21:39:55 +01:00
parent bc41cb8541
commit 5d9cb6d093
3 changed files with 38 additions and 13 deletions

View File

@ -6,6 +6,7 @@ from io import StringIO
from bootstraparse.modules import pathresolver as pr from bootstraparse.modules import pathresolver as pr
from bootstraparse.modules import environment from bootstraparse.modules import environment
import rich import rich
from rich.tree import Tree from rich.tree import Tree
@ -150,9 +151,8 @@ class PreParser:
:return: the html to insert as a string :return: the html to insert as a string
:param shortcut: the id of the shortcut to fetch :param shortcut: the id of the shortcut to fetch
""" """
if shortcut: # return self.__env.config[""(shortcut)
return '<h1>hello world</h1>' return f'<h1>{shortcut}</h1>'
return 'error'
def get_picture_from_config(self, shortcut): def get_picture_from_config(self, shortcut):
""" """
@ -160,9 +160,7 @@ class PreParser:
:return: the html to insert as a string :return: the html to insert as a string
:param shortcut: the id of the picture to fetch :param shortcut: the id of the picture to fetch
""" """
if shortcut: return f'<img src="{shortcut}"/>'
return '<img src=""/>'
return 'error'
# todo: test import in sub-folders # todo: test import in sub-folders
# todo: test same imports on multiple lines # todo: test same imports on multiple lines
@ -189,8 +187,8 @@ class PreParser:
def __eq__(self, other): def __eq__(self, other):
""" """
Checks if the PreParser object is equal to another PreParser object. Checks if the PreParser object is equal to another PreParser object.
:param other: the other PreParser object
:return: True if the PreParser objects are equal, False otherwise :return: True if the PreParser objects are equal, False otherwise
:param other: the other PreParser object
""" """
if self.path == other.path: if self.path == other.path:
if self.name == other.name: if self.name == other.name:
@ -204,8 +202,8 @@ class PreParser:
def __ne__(self, other): def __ne__(self, other):
""" """
Checks if the PreParser object is not equal to another PreParser object. Checks if the PreParser object is not equal to another PreParser object.
:param other: the other PreParser object
:return: True if the PreParser objects are not equal, False otherwise :return: True if the PreParser objects are not equal, False otherwise
:param other: the other PreParser object
""" """
return not self.__eq__(other) return not self.__eq__(other)
@ -238,5 +236,5 @@ if __name__ == "__main__": # pragma: no cover
# rich.print(michel.rich_tree()) # rich.print(michel.rich_tree())
string_to_match = r'@[bite]{id=2}[saucisse=13] @[chaussette] @[bermuda]{tomate=tomate}[12] @[pasteque]' string_to_match = r'@[bite]{id=2}[saucisse=13] @[chaussette] @[bermuda]{tomate=tomate}[12] @[pasteque]'
# rich.inspect(_rgx_alias.match(string_to_match).captures('alias_name', 'class', 'variables')) # rich.inspect(_rgx_alias.match(string_to_match).captures('alias_name', 'class', 'variables'))
for e in _rgx_alias.match(string_to_match).captures(1): for ex in _rgx_alias.match(string_to_match).captures(1):
rich.inspect(e.captures('alias_name', 'class', 'variables')) rich.inspect(ex.captures('alias_name', 'class', 'variables'))

View File

@ -38,6 +38,7 @@ def test_config_load():
assert usr_c["aliases2"] is not None assert usr_c["aliases2"] is not None
assert usr_c.__repr__() assert usr_c.__repr__()
def test_add_to_config(): def test_add_to_config():
usr_c = config.ConfigLoader(user_conf) usr_c = config.ConfigLoader(user_conf)
usr_c.add_folder(app_conf) usr_c.add_folder(app_conf)
@ -54,4 +55,4 @@ def test_add_to_config():
from_empty.load_from_file(os.path.join(app_conf, "aliases.yaml")) from_empty.load_from_file(os.path.join(app_conf, "aliases.yaml"))
assert from_empty["aliases"] == {"aliases": {"test_alias": "test_parser"}, assert from_empty["aliases"] == {"aliases": {"test_alias": "test_parser"},
"parser": {"name": "test_parser", "type": "test_parser"}} "parser": {"name": "test_parser", "type": "test_parser"}}

View File

@ -10,7 +10,7 @@ from bootstraparse.modules import preparser
from bootstraparse.modules import environment from bootstraparse.modules import environment
############################################################################### ###############################################################################
# Environement variables # Environment variables
env = environment.Environment() env = environment.Environment()
_BASE_PATH_PREPARSER = "../../" _BASE_PATH_PREPARSER = "../../"
@ -68,6 +68,11 @@ website_tree = {
"pages/page2.bpr": content_page2 "pages/page2.bpr": content_page2
} }
get_from_config = '''
@{shortcut}{class="shortcut", type=2, name="shortcut"}[type=33, name="shortcut"]
@[picture]{class="picture", type=2, name="picture"}[type=33, name="picture"]
'''
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def base_architecture(): def base_architecture():
@ -198,7 +203,7 @@ def test_preparser_content(filename, content):
pp = preparser.PreParser(testfile, env) pp = preparser.PreParser(testfile, env)
pp.make_import_list() pp.make_import_list()
assert pp.export_with_imports().read() == content # nothing is implementes yet assert pp.export_with_imports().read() == content # nothing is implemented yet
# todo: test import in sub-folders # todo: test import in sub-folders
@ -233,3 +238,24 @@ def test_rich_tree():
pp.make_import_list() pp.make_import_list()
pp.rich_tree() pp.rich_tree()
pp.rich_tree(force=False) pp.rich_tree(force=False)
def test_get_shortcut_from_config():
from_config = temp_name("test_get_shortcut_from_config.bpr")
make_new_file(from_config, get_from_config)
pp = preparser.PreParser(from_config, env)
pp.make_import_list()
assert pp.get_shortcut_from_config("any_shortcut") == "<h1>any_shortcut</h1>"
assert pp.get_picture_from_config("any_picture") == '<img src="any_picture"/>'
@pytest.mark.xfail(reason="Not implemented")
def test_replace():
replace_file = temp_name("test_preparse.bpr")
make_new_file(replace_file, get_from_config)
pp = preparser.PreParser(replace_file, env)
pp.make_import_list()
image_f = pp.parse_shortcuts_and_images()
assert image_f.read() == """\n<h1>test_preparse</h1>\n<img src='test_preparse.bpr'/>\n"""