Skip to content

Commit

Permalink
format tests with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
bygu4 committed Jan 3, 2025
1 parent 148445f commit d006c35
Show file tree
Hide file tree
Showing 24 changed files with 1,754 additions and 1,388 deletions.
649 changes: 375 additions & 274 deletions pyformlang/cfg/tests/test_cfg.py

Large diffs are not rendered by default.

416 changes: 266 additions & 150 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
97 changes: 70 additions & 27 deletions pyformlang/cfg/tests/test_recursive_decent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# 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
from pyformlang.cfg.recursive_decent_parser import (
RecursiveDecentParser,
NotParsableError,
)
import pytest


Expand All @@ -19,43 +21,84 @@ def parser():


class TestRecursiveDecentParser:

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

def test_get_parsing_tree(self, parser):
def test_get_parsing_tree(self, parser) -> None:
assert parser.is_parsable(
["(", "int", "+", "(", "int", "*", "int", ")", ")"]
)
parse_tree = parser.get_parse_tree(
["(", "int", "+", "(", "int", "*", "int", ")", ")"])
["(", "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, parser) -> None:
with pytest.raises(NotParsableError):
parser.get_parse_tree([")"])
assert not (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

0 comments on commit d006c35

Please sign in to comment.