reached 90% bad coverage

This commit is contained in:
Maximilien Rigaut 2022-05-28 04:42:43 +02:00
parent 03c16e5bc7
commit 3f39c344c6
2 changed files with 119 additions and 4 deletions

View File

@ -67,6 +67,8 @@ class BaseContainer:
return self.map[other]()
def __eq__(self, other):
if not hasattr(other, "__len__"):
return False
if len(self) != len(other):
return False
for e, f in zip(self, other):
@ -171,7 +173,8 @@ class EtOlistContainer(BaseContainer):
class HyperLinkContainer(BaseContainer):
def __init__(self):
self.map += {'url': ""}
super().__init__()
self.map['url']: ""
pass
@ -284,9 +287,12 @@ class ContextManager:
"""
try:
pile_start = self.pile[start]
except KeyError:
_ = self.pile[end]
if start > end:
raise IndexError("Start index must be lower than end index")
except IndexError:
log_exception(
KeyError(f"Index {start} not in pile range."),
KeyError(f"Indexes ({start}:{end}) must be in the pile range ({len(self.pile)})."),
level="CRITICAL"
)
try:
@ -354,7 +360,7 @@ class ContextManager:
# Builds all containers as the parser is parsing the file
if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
from bootstraparse.modules import parser
from io import StringIO
io_string = StringIO(

View File

@ -1,4 +1,93 @@
import pytest
import rich
from bootstraparse.modules import context_mngr
from bootstraparse.modules.syntax import SemanticType, TextToken
_list_classes = [
context_mngr.TextContainer,
context_mngr.EtEmContainer,
context_mngr.EtStrongContainer,
context_mngr.EtUnderlineContainer,
context_mngr.EtStrikethroughContainer,
context_mngr.EtCustomSpanContainer,
context_mngr.EtUlistContainer,
context_mngr.EtOlistContainer,
context_mngr.HyperLinkContainer,
context_mngr.SeContainer,
context_mngr.HeaderContainer,
context_mngr.DisplayContainer,
context_mngr.TableMainContainer,
context_mngr.TableHeadContainer,
context_mngr.TableRowContainer,
context_mngr.TableCellContainer,
context_mngr.LinebreakContainer,
]
_base_list = [TextToken([1]), TextToken([2]), TextToken([3]), TextToken([4])]
@pytest.fixture
def base_cm():
lst = _base_list[:]
base = context_mngr.ContextManager(_base_list)
base.pile = lst
return base
@pytest.mark.parametrize("cls", _list_classes)
def test_container_init(cls):
cls()
def test_context_manager(base_cm):
lst = [SemanticType([1]), SemanticType([2]), SemanticType([3])]
base = context_mngr.ContextManager(lst)
base.pile = lst
for e, v in zip(_base_list, base_cm):
assert e == v
def test_encapsulate(base_cm): # TODO : be more thourough
base_cm.encapsulate(1, 2)
ctn = context_mngr.TextContainer()
ctn.add(_base_list[1])
ctn.add(_base_list[2])
res = [_base_list[0], ctn, None] + _base_list[3:]
assert base_cm.pile == res
def test_encapsulate_bad_index(base_cm):
with pytest.raises(SystemExit):
base_cm.encapsulate(1, 44)
with pytest.raises(SystemExit):
base_cm.encapsulate(33, 1)
with pytest.raises(SystemExit):
base_cm.encapsulate(2, 1)
def test_encapsulate_bad_cm(base_cm):
base_cm.pile[1] = SemanticType([1])
with pytest.raises(SystemExit):
base_cm.encapsulate(1, 2)
def test_matched_functions(base_cm):
base_cm._add_matched("test", 2)
assert base_cm.matched_elements == {'test': [2]}
base_cm._add_matched("test", 3)
assert base_cm.matched_elements == {'test': [2, 3]}
base_cm._add_matched("test2", 4)
assert base_cm.matched_elements == {'test': [2, 3], 'test2': [4]}
assert base_cm._get_matched("test") == 3
assert base_cm._get_matched("test2") == 4
assert base_cm._get_matched("test") == 2
with pytest.raises(IndexError):
base_cm._get_matched("test2")
with pytest.raises(KeyError):
assert base_cm._get_matched("test3")
def test_context_mngr():
@ -52,3 +141,23 @@ def test_base_container_with_optionals():
base.optionals = [i, c]
assert base.fetch_html_insert() == [i]
assert base.fetch_class_insert() == [c]
def test_print():
base = context_mngr.BaseContainerWithOptionals()
assert base is not None
base.print_all()
base.content = [context_mngr.BaseContainer(), context_mngr.BaseContainer(), "test", 3, context_mngr.BaseContainer()]
base.print_all()
def test_print_cm(base_cm):
base_cm.print_all()
base_cm.pile = [context_mngr.BaseContainer(), context_mngr.BaseContainer(), "test", 3, context_mngr.BaseContainer()]
base_cm.print_all()
def test_container():
base = context_mngr.BaseContainer()
assert base is not None
assert base.to_container() == base