Initial commit
This commit is contained in:
commit
9fba4b3d34
17 changed files with 2939 additions and 0 deletions
143
tests/test_stringify.py
Normal file
143
tests/test_stringify.py
Normal file
|
@ -0,0 +1,143 @@
|
|||
import pytest
|
||||
|
||||
from src.parser import (
|
||||
DescendantSelector,
|
||||
MultiSelector,
|
||||
NotPseudoClassSelector,
|
||||
NthChildPseudoClassSelector,
|
||||
SiblingSelector,
|
||||
SimpleSelector,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("selector", "expected"),
|
||||
[
|
||||
(SimpleSelector(tag="div"), "div"),
|
||||
(SimpleSelector(classes=["main"]), ".main"),
|
||||
(SimpleSelector(tag="span", classes=["big", "blue"]), "span.big.blue"),
|
||||
(SimpleSelector(tag="a", ids=["link"]), "a#link"),
|
||||
(SimpleSelector(classes=["one"], ids=["unique"]), ".one#unique"),
|
||||
(SimpleSelector(tag="p", classes=["x"], ids=["y"]), "p.x#y"),
|
||||
(SimpleSelector(ids=["onlyid"]), "#onlyid"),
|
||||
],
|
||||
)
|
||||
def test_simple_selector_str(selector: SimpleSelector, expected: str) -> None:
|
||||
assert str(selector) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("selector", "expected"),
|
||||
[
|
||||
(
|
||||
NotPseudoClassSelector(
|
||||
selector=SimpleSelector(tag="div"),
|
||||
not_selector=SimpleSelector(classes=["main"]),
|
||||
),
|
||||
"div:not(.main)",
|
||||
),
|
||||
(
|
||||
NotPseudoClassSelector(
|
||||
selector=None,
|
||||
not_selector=SimpleSelector(classes=["a", "b"]),
|
||||
),
|
||||
":not(.a.b)",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_not_pseudo_class_selector_str(selector: NotPseudoClassSelector, expected: str) -> None:
|
||||
assert str(selector) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("selector", "expected"),
|
||||
[
|
||||
(
|
||||
NthChildPseudoClassSelector(selector=None, n=2),
|
||||
":nth-child(2)",
|
||||
),
|
||||
(
|
||||
NthChildPseudoClassSelector(selector=SimpleSelector(tag="div"), n=2),
|
||||
"div:nth-child(2)",
|
||||
),
|
||||
(
|
||||
NthChildPseudoClassSelector(selector=SimpleSelector(tag="li"), n=1),
|
||||
"li:first-child",
|
||||
),
|
||||
(
|
||||
NthChildPseudoClassSelector(selector=SimpleSelector(tag="li"), n=-1),
|
||||
"li:last-child",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_nth_child_pseudo_class_selector_str(selector: NthChildPseudoClassSelector, expected: str) -> None:
|
||||
assert str(selector) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("selector", "expected"),
|
||||
[
|
||||
(
|
||||
DescendantSelector(
|
||||
parent=SimpleSelector(tag="div", classes=["main"]),
|
||||
child=SimpleSelector(tag="span"),
|
||||
direct=False,
|
||||
),
|
||||
"div.main span",
|
||||
),
|
||||
(
|
||||
DescendantSelector(
|
||||
parent=SimpleSelector(tag="ul"),
|
||||
child=SimpleSelector(tag="li", classes=["active"]),
|
||||
direct=True,
|
||||
),
|
||||
"ul > li.active",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_descendant_selector_str(selector: DescendantSelector, expected: str) -> None:
|
||||
assert str(selector) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("selector", "expected"),
|
||||
[
|
||||
(
|
||||
SiblingSelector(
|
||||
sibling_selector=SimpleSelector(tag="p"),
|
||||
selector=SimpleSelector(tag="span"),
|
||||
is_adjacent=True,
|
||||
),
|
||||
"p + span",
|
||||
),
|
||||
(
|
||||
SiblingSelector(
|
||||
sibling_selector=SimpleSelector(tag="div", classes=["a"]),
|
||||
selector=SimpleSelector(classes=["b"]),
|
||||
is_adjacent=False,
|
||||
),
|
||||
"div.a ~ .b",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_sibling_selector_str(selector: SiblingSelector, expected: str) -> None:
|
||||
assert str(selector) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("selector", "expected"),
|
||||
[
|
||||
(
|
||||
MultiSelector(
|
||||
[
|
||||
SimpleSelector("div", classes=["main"]),
|
||||
SimpleSelector("span"),
|
||||
SimpleSelector(ids=["x"]),
|
||||
]
|
||||
),
|
||||
"div.main, span, #x",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_multi_selector_str(selector: MultiSelector, expected: str) -> None:
|
||||
assert str(selector) == expected
|
Loading…
Add table
Add a link
Reference in a new issue