Skip to content

Commit

Permalink
Unexpected EOF related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed May 30, 2022
1 parent 8016f55 commit cd740cd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
10 changes: 10 additions & 0 deletions flexparser/testsuite/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ class MyRoot(fp.RootBlock):
class MyParser(fp.Parser):

_root_block_class = MyRoot


class MyRootWithBlock(fp.RootBlock):

body: fp.Multi[typing.Union[Comment, EqualFloat, MyBlock]]


class MyParserWithBlock(fp.Parser):

_root_block_class = MyRootWithBlock
19 changes: 19 additions & 0 deletions flexparser/testsuite/test_element_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,22 @@ def test_block(klass):

assert tuple(mb) == (mb.opening, *mb.body, mb.closing)
assert not mb.has_errors


@pytest.mark.parametrize("klass", (MyBlock, MyBlock2))
def test_unfinished_block(klass):
lines = "@begin\n# hola\nx=1.0".split("\n")
si = fp.SequenceIterator.from_lines(lines)

mb = klass.consume(si, None)
assert mb.opening == Open().set_line_col(0, 0)
assert mb.closing == fp.UnexpectedEOF().set_line_col(-1, -1)
body = tuple(mb.body)
assert len(body) == 2
assert mb.body == (
Comment("# hola").set_line_col(1, 0),
EqualFloat("x", 1.0).set_line_col(2, 0),
)

assert tuple(mb) == (mb.opening, *mb.body, mb.closing)
assert mb.has_errors
40 changes: 39 additions & 1 deletion flexparser/testsuite/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from flexparser import flexparser as fp
from flexparser.testsuite.common import Comment, EqualFloat, MyParser
from flexparser.testsuite.common import (
Comment,
EqualFloat,
MyBlock,
MyParser,
MyParserWithBlock,
)


def test_consume():
Expand Down Expand Up @@ -48,3 +54,35 @@ def test_parse(tmp_path):
)
assert tuple(mb) == (mb.opening, *body, mb.closing)
assert not mb.has_errors


def test_unfinished_block(tmp_path):
content = "@begin\n# hola\nx=1.0"

tmp_file = tmp_path / "bla.txt"
tmp_file.write_text(content)
myparser = MyParserWithBlock(None)

psf = myparser.parse(tmp_file)
assert psf.has_errors
assert psf.config is None
assert psf.mtime == tmp_file.stat().st_mtime
assert psf.filename == tmp_file
assert tuple(psf.localized_errors()) == (
fp.UnexpectedEOF().set_line_col(-1, -1).copy_with(tmp_file),
)
assert psf.origin == psf.filename
# TODO:
# assert psf.content_hash == hashlib.sha1(content.encode("utf-8")).hexdigest()

mb = psf.parsed_source
assert isinstance(mb.opening, fp.BOS)
assert isinstance(mb.closing, fp.EOS)
body = tuple(mb.body)
assert len(body) == 1
assert isinstance(body[0], MyBlock)
assert body[0].closing == fp.UnexpectedEOF().set_line_col(-1, -1)
assert body[0].body == (
Comment("# hola").set_line_col(1, 0),
EqualFloat("x", 1.0).set_line_col(2, 0),
)

0 comments on commit cd740cd

Please sign in to comment.