Bringing coverage to 100%, added a couple of future tests
This commit is contained in:
parent
0677520213
commit
66b8965b7e
|
|
@ -42,6 +42,14 @@ class PreParser:
|
||||||
self.file = open(self.relative_path_resolver(self.name), 'r')
|
self.file = open(self.relative_path_resolver(self.name), 'r')
|
||||||
return self.file
|
return self.file
|
||||||
|
|
||||||
|
def readlines(self):
|
||||||
|
"""
|
||||||
|
Reads the file and returns a list of lines.
|
||||||
|
"""
|
||||||
|
if self.file is None:
|
||||||
|
return self.open().readlines()
|
||||||
|
return self.file.readlines()
|
||||||
|
|
||||||
def make_import_list(self):
|
def make_import_list(self):
|
||||||
"""
|
"""
|
||||||
Creates a list of all files to be imported.
|
Creates a list of all files to be imported.
|
||||||
|
|
@ -55,7 +63,7 @@ class PreParser:
|
||||||
if e in self.global_dict_of_imports:
|
if e in self.global_dict_of_imports:
|
||||||
pp = self.global_dict_of_imports[e]
|
pp = self.global_dict_of_imports[e]
|
||||||
else:
|
else:
|
||||||
# todo: get the path of the file
|
# todo: get the path of the file and append it to the path given
|
||||||
pp = PreParser(e, self.__env, self.list_of_paths.copy(), self.global_dict_of_imports)
|
pp = PreParser(e, self.__env, self.list_of_paths.copy(), self.global_dict_of_imports)
|
||||||
self.global_dict_of_imports[e] = pp
|
self.global_dict_of_imports[e] = pp
|
||||||
pp.parse_import_list()
|
pp.parse_import_list()
|
||||||
|
|
@ -75,9 +83,7 @@ class PreParser:
|
||||||
Return the file object as a series of lines and append all imports to the file.
|
Return the file object as a series of lines and append all imports to the file.
|
||||||
"""
|
"""
|
||||||
# todo: export the file with all imports
|
# todo: export the file with all imports
|
||||||
self.open()
|
lines = self.readlines()
|
||||||
lines = self.file.readlines()
|
|
||||||
self.close()
|
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
@ -107,8 +113,8 @@ class PreParser:
|
||||||
return self.__repr__()
|
return self.__repr__()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
site_path = "../../../example_userfiles/index.bpr"
|
# site_path = "../../../example_userfiles/index.bpr"
|
||||||
__env = environment.Environment()
|
# __env = environment.Environment()
|
||||||
michel = PreParser(site_path, __env)
|
# michel = PreParser(site_path, __env)
|
||||||
rich.inspect(michel)
|
# rich.inspect(michel)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
# Testing the preparser module
|
# Testing the preparser module
|
||||||
import pytest
|
import pytest
|
||||||
|
import io
|
||||||
|
|
||||||
from bootstraparse.modules import preparser
|
from bootstraparse.modules import preparser
|
||||||
from bootstraparse.modules import environment
|
from bootstraparse.modules import environment
|
||||||
|
|
@ -18,6 +19,76 @@ def test_preparser_init(path):
|
||||||
pp.open()
|
pp.open()
|
||||||
pp.close()
|
pp.close()
|
||||||
pp.export_with_imports()
|
pp.export_with_imports()
|
||||||
|
pp.readlines()
|
||||||
# pp.export_without_imports()
|
# pp.export_without_imports()
|
||||||
|
|
||||||
assert type(pp.__str__()) == str
|
assert type(pp.__str__()) == str
|
||||||
assert type(pp.__repr__()) == str
|
assert type(pp.__repr__()) == str
|
||||||
|
|
||||||
|
|
||||||
|
def false_file_open(text):
|
||||||
|
def _file_open():
|
||||||
|
return io.StringIO(text)
|
||||||
|
return _file_open
|
||||||
|
|
||||||
|
|
||||||
|
def false_import_list(table):
|
||||||
|
def _import_list():
|
||||||
|
return table
|
||||||
|
return _import_list
|
||||||
|
|
||||||
|
|
||||||
|
def assert_readlines_equals(lines1, lines2):
|
||||||
|
for line1, line2 in zip(lines1, lines2):
|
||||||
|
assert line1.strip("\n") == line2.strip("\n")
|
||||||
|
|
||||||
|
|
||||||
|
def test_preparser_content():
|
||||||
|
"""
|
||||||
|
Test the preparser module on a file with content
|
||||||
|
"""
|
||||||
|
content_index = """Test content
|
||||||
|
::< exemple_userfiles/pages/page1.bpr >
|
||||||
|
Test content
|
||||||
|
::< exemple_userfiles/pages/page2.bpr >
|
||||||
|
Test content
|
||||||
|
::< exemple_userfiles/pages/page1.bpr >
|
||||||
|
Test content"""
|
||||||
|
content_page1 = """Test page1"""
|
||||||
|
content_page2 = """Test page2"""
|
||||||
|
|
||||||
|
final_content = """Test content
|
||||||
|
Test page1
|
||||||
|
Test content
|
||||||
|
Test page2
|
||||||
|
Test content
|
||||||
|
Test page1
|
||||||
|
Test content"""
|
||||||
|
|
||||||
|
p_index = preparser.PreParser("example_userfiles/index.bpr", env)
|
||||||
|
p_index.open = false_file_open(content_index)
|
||||||
|
p_index.parse_import_list = false_import_list(["exemple_userfiles/pages/page1.bpr", "exemple_userfiles/pages/page2.bpr","exemple_userfiles/pages/page1.bpr"])
|
||||||
|
p_index.make_import_list()
|
||||||
|
|
||||||
|
assert_readlines_equals(p_index.readlines(), content_index.split("\n"))
|
||||||
|
|
||||||
|
with pytest.raises(RecursionError):
|
||||||
|
p_index.list_of_paths = ["exemple_userfiles/pages/page1.bpr"]
|
||||||
|
p_index.make_import_list()
|
||||||
|
|
||||||
|
p_page1 = preparser.PreParser("example_userfiles/pages/page1.bpr", env)
|
||||||
|
p_page1.open = false_file_open(content_page1)
|
||||||
|
p_page1.parse_import_list = false_import_list([])
|
||||||
|
p_page1.make_import_list()
|
||||||
|
assert p_page1.open().readlines() == ["Test page1"]
|
||||||
|
|
||||||
|
p_page2 = preparser.PreParser("example_userfiles/pages/page2.bpr", env)
|
||||||
|
p_page2.open = false_file_open(content_page2)
|
||||||
|
p_page2.parse_import_list = false_import_list([])
|
||||||
|
p_page2.make_import_list()
|
||||||
|
assert p_page2.open().readlines() == ["Test page2"]
|
||||||
|
|
||||||
|
@pytest.xfail(reason="Not implemented")
|
||||||
|
def test_export_with_imports():
|
||||||
|
assert_readlines_equals(p_index.export_with_imports(), final_content.split("\n"))
|
||||||
|
test_export_with_imports()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue