Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format tests with Ruff #17

Open
wants to merge 4 commits into
base: update_docs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
653 changes: 377 additions & 276 deletions pyformlang/cfg/tests/test_cfg.py

Large diffs are not rendered by default.

428 changes: 272 additions & 156 deletions pyformlang/cfg/tests/test_llone_parser.py

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pyformlang/cfg/tests/test_production.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
""" Tests the productions """
"""Tests the productions."""

from pyformlang.cfg import Production, Variable, Terminal


class TestProduction:
""" Tests the production """
"""Tests the production."""

# pylint: disable=missing-function-docstring

def test_creation(self):
def test_creation(self) -> None:
prod0 = Production(Variable("S0"), [Terminal("S1"), Variable("a")])
prod1 = Production(Variable("S0"), [Terminal("S1"), Variable("a")])
prod2 = Production(Variable("S0'"), [Terminal("S1"), Variable("a")])
Expand Down
121 changes: 83 additions & 38 deletions pyformlang/cfg/tests/test_recursive_decent_parser.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,106 @@
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
from pyformlang.cfg import CFG, Variable, Terminal
from pyformlang.cfg.recursive_decent_parser import \
RecursiveDecentParser, NotParsableError
"""Tests for recursive top-down parser."""

import pytest

from pyformlang.cfg import CFG, Variable, Terminal
from pyformlang.cfg.recursive_decent_parser import (
RecursiveDecentParser,
NotParsableError,
)


@pytest.fixture
def parser():
def example_parser() -> RecursiveDecentParser:
cfg = CFG.from_text("""
E -> S + S
E -> S * S
S -> ( E )
S -> int
""")
yield RecursiveDecentParser(cfg)
return RecursiveDecentParser(cfg)


class TestRecursiveDecentParser:
def test_creation(self,
example_parser: RecursiveDecentParser) -> None:
assert example_parser is not None

def test_creation(self, parser):
assert parser is not None

def test_get_parsing_tree(self, parser):
assert parser.is_parsable(
def test_get_parsing_tree(self,
example_parser: RecursiveDecentParser) -> None:
assert example_parser.is_parsable(
["(", "int", "+", "(", "int", "*", "int", ")", ")"]
)
parse_tree = example_parser.get_parse_tree(
["(", "int", "+", "(", "int", "*", "int", ")", ")"]
)
parse_tree = parser.get_parse_tree(
["(", "int", "+", "(", "int", "*", "int", ")", ")"])
derivation = parse_tree.get_leftmost_derivation()
assert derivation == \
[[Variable("S")],
[Terminal("("), Variable("E"), Terminal(")")],
[Terminal("("), Variable("S"), Terminal("+"), Variable("S"),
Terminal(")")],
[Terminal("("), Terminal("int"), Terminal("+"), Variable("S"),
Terminal(")")],
[Terminal("("), Terminal("int"), Terminal("+"), Terminal("("),
Variable("E"), Terminal(")"), Terminal(")")],
[Terminal("("), Terminal("int"), Terminal("+"), Terminal("("),
Variable("S"), Terminal("*"), Variable("S"), Terminal(")"),
Terminal(")")],
[Terminal("("), Terminal("int"), Terminal("+"), Terminal("("),
Terminal("int"), Terminal("*"), Variable("S"), Terminal(")"),
Terminal(")")],
[Terminal("("), Terminal("int"), Terminal("+"), Terminal("("),
Terminal("int"), Terminal("*"), Terminal("int"), Terminal(")"),
Terminal(")")],
]
assert derivation == [
[Variable("S")],
[Terminal("("), Variable("E"), Terminal(")")],
[
Terminal("("),
Variable("S"),
Terminal("+"),
Variable("S"),
Terminal(")"),
],
[
Terminal("("),
Terminal("int"),
Terminal("+"),
Variable("S"),
Terminal(")"),
],
[
Terminal("("),
Terminal("int"),
Terminal("+"),
Terminal("("),
Variable("E"),
Terminal(")"),
Terminal(")"),
],
[
Terminal("("),
Terminal("int"),
Terminal("+"),
Terminal("("),
Variable("S"),
Terminal("*"),
Variable("S"),
Terminal(")"),
Terminal(")"),
],
[
Terminal("("),
Terminal("int"),
Terminal("+"),
Terminal("("),
Terminal("int"),
Terminal("*"),
Variable("S"),
Terminal(")"),
Terminal(")"),
],
[
Terminal("("),
Terminal("int"),
Terminal("+"),
Terminal("("),
Terminal("int"),
Terminal("*"),
Terminal("int"),
Terminal(")"),
Terminal(")"),
],
]

def test_no_parse_tree(self, parser):
def test_no_parse_tree(self, example_parser: RecursiveDecentParser) -> None:
with pytest.raises(NotParsableError):
parser.get_parse_tree([")"])
assert not (parser.is_parsable([")"]))
example_parser.get_parse_tree([")"])
assert not example_parser.is_parsable([")"])

def test_infinite_recursion(self):
def test_infinite_recursion(self) -> None:
cfg = CFG.from_text("""
S -> S E
""")
Expand Down
10 changes: 6 additions & 4 deletions pyformlang/cfg/tests/test_terminal.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
""" Tests the terminal """
"""Tests the terminal."""

from pyformlang.cfg import Variable, Terminal, Epsilon
from pyformlang.finite_automaton import State, Symbol, Epsilon as FAEpsilon


class TestTerminal:
""" Tests the terminal """
"""Tests the terminal."""

# pylint: disable=missing-function-docstring

def test_creation(self):
def test_creation(self) -> None:
terminal0 = Terminal(0)
terminal1 = Terminal(1)
terminal2 = Terminal(0)
Expand All @@ -27,7 +29,7 @@ def test_creation(self):
assert str(terminal0) == "0"
assert repr(terminal0) == "Terminal(0)"

def test_eq(self):
def test_eq(self) -> None:
assert "epsilon" == Epsilon()
assert Epsilon() == "ɛ"
assert Terminal("A") != Variable("A")
Expand Down
8 changes: 5 additions & 3 deletions pyformlang/cfg/tests/test_variable.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
""" Tests the variable """
"""Tests the variable."""

from pyformlang.cfg import Variable


class TestVariable:
""" Tests the variable """
"""Tests the variable."""

# pylint: disable=missing-function-docstring

def test_creation(self):
def test_creation(self) -> None:
variable0 = Variable(0)
variable1 = Variable(1)
variable2 = Variable(0)
Expand Down
Loading
Loading