❌
@@ -231,20 +203,10 @@ class LastChild(CSSSoupSelector):
Tag is only selected if it is the last child of its parent.
Element that is the first and only child is matched as well.
- In case of passing tag parameter, selector is `{tag}:last-child`.
- Otherwise, selector is `:last-child`, which is equal to passing "*",
- css wildcard selector as an argument.
-
Example
--------
>>> LastChild().selector
:last-child
- >>> LastChild("div").selector
- div:last-child
-
- If tag is specified, two conditions must be met:
- - Tag is the last child of its parent
- - Tag has the specified tag name
Notes
--------
@@ -254,11 +216,22 @@ class LastChild(CSSSoupSelector):
https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child
"""
- def __init__(self, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:last-child")
+ _SELECTOR = ":last-child"
+
+
+class _NthBaseSelector(CSSSoupSelector):
+ """Base class for selectors based on nth formula."""
+
+ def __init__(self, nth: str) -> None:
+ self._nth = nth
+ super().__init__()
+
+ @property
+ def _formats(self) -> list[str]:
+ return [self._nth]
-class NthChild(CSSSoupSelector):
+class NthChild(_NthBaseSelector):
"""
Class to select tags that are the nth child of their parent.
It uses the CSS selector `:nth-child(n)`.
@@ -267,37 +240,24 @@ class NthChild(CSSSoupSelector):
----------
nth : str, positional
Number of the child to be selected. Can be a number or a formula.
- tag : str, optional
- Tag to be selected. If None, any tag is selected.
-
- In case of passing tag parameter, selector is `{tag}:nth-child(n)`.
- Otherwise, selector is `:nth-child(n)`, which is equal to passing "*",
- css wildcard selector as an argument.
Example
--------
>>> NthChild("2").selector
:nth-child(2)
- >>> NthChild("2", "li").selector
- li:nth-child(2)
>>> NthChild("2n+1").selector
:nth-child(2n+1)
- >>> NthChild("odd", "div").selector
- div:nth-child(odd)
-
- If tag is specified, two conditions must be met:
- - Tag is the nth child of its parent
- - Tag has the specified tag name
+ >>> NthChild("odd").selector
+ :nth-child(odd)
For more information on the formula, see:
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
"""
- def __init__(self, nth: str, /, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:nth-child({nth})")
+ _SELECTOR = ":nth-child({})"
-class NthLastChild(CSSSoupSelector):
+class NthLastChild(_NthBaseSelector):
"""
Class to select tags that are the nth last child of their parent.
It uses the CSS selector `:nth-last-child(n)`.
@@ -306,34 +266,21 @@ class NthLastChild(CSSSoupSelector):
----------
nth : str, positional
Number of the child to be selected. Can be a number or a formula.
- tag : str, optional
- Tag to be selected. If None, any tag is selected.
-
- In case of passing tag parameter, selector is `{tag}:nth-last-child(n)`.
- Otherwise, selector is `:nth-last-child(n)`, which is equal to passing "*",
- css wildcard selector as an argument.
Example
--------
>>> NthLastChild("2").selector
:nth-last-child(2)
- >>> NthLastChild("2", "li").selector
- li:nth-last-child(2)
>>> NthLastChild("2n+1").selector
:nth-last-child(2n+1)
- >>> NthLastChild("odd", "div").selector
- div:nth-last-child(odd)
-
- If tag is specified, two conditions must be met:
- - Tag is the nth last child of its parent
- - Tag has the specified tag name
+ >>> NthLastChild("odd").selector
+ :nth-last-child(odd)
For more information on the formula, see:
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child
"""
- def __init__(self, nth: str, /, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:nth-last-child({nth})")
+ _SELECTOR = ":nth-last-child({})"
class FirstOfType(CSSSoupSelector):
@@ -341,11 +288,6 @@ class FirstOfType(CSSSoupSelector):
Class to select tags that are the first of their type in their parent.
It uses the CSS selector `:first-of-type`.
- Parameters
- ----------
- tag : str, optional
- Tag to be selected. If None, any tag is selected.
-
Example
--------
>>>
✔️
@@ -359,32 +301,21 @@ class FirstOfType(CSSSoupSelector):
Tag is only selected if it is the first of its type in its parent.
- In case of passing tag parameter, selector is `{tag}:first-of-type`.
- Otherwise, selector is `:first-of-type`, which is equal to passing "*",
- css wildcard selector as an argument.
-
Example
--------
>>> FirstOfType().selector
:first-of-type
- >>> FirstOfType("div").selector
- div:first-of-type
-
- If tag is specified, two conditions must be met:
- - Tag is the first of its type in its parent
- - Tag has the specified tag name
Notes
--------
- If tag is not specified, the first tag of any type is selected, which in
+ For this selector the first tag of any type is selected, which in
case of finding single tag is equivalent to FirstChild() results.
For more information on the formula, see:
https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type
"""
- def __init__(self, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:first-of-type")
+ _SELECTOR = ":first-of-type"
class LastOfType(CSSSoupSelector):
@@ -392,11 +323,6 @@ class LastOfType(CSSSoupSelector):
Class to select tags that are the last of their type in their parent.
It uses the CSS selector `:last-of-type`.
- Parameters
- ----------
- tag : str, optional
- Tag to be selected. If None, any tag is selected.
-
Example
--------
>>>
❌
@@ -411,35 +337,24 @@ class LastOfType(CSSSoupSelector):
Tag is only selected if it is the last of its type in its parent.
Element that is the first and only child is matched as well.
- In case of passing tag parameter, selector is `{tag}:last-of-type`.
- Otherwise, selector is `:last-of-type`, which is equal to passing "*",
- css wildcard selector as an argument.
-
Example
--------
>>> LastOfType().selector
:last-of-type
- >>> LastOfType("div").selector
- div:last-of-type
-
- If tag is specified, two conditions must be met:
- - Tag is the last of its type in its parent
- - Tag has the specified tag name
Notes
--------
- If tag is not specified, the first tag of any type is selected, which in
+ For this selector the last tag of any type is selected, which in
case of finding single tag is the equivalent to LastChild() results.
For more information on the formula, see:
https://developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type
"""
- def __init__(self, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:last-of-type")
+ _SELECTOR = ":last-of-type"
-class NthOfType(CSSSoupSelector):
+class NthOfType(_NthBaseSelector):
"""
Class to select tags that are the nth of their type in their parent.
It uses the CSS selector `:nth-of-type(n)`.
@@ -448,37 +363,24 @@ class NthOfType(CSSSoupSelector):
----------
nth : str, positional
Number of the tag to be selected. Can be a number or a formula.
- tag : str, optional
- Tag to be selected. If None, any tag is selected.
-
- In case of passing tag parameter, selector is `{tag}:nth-of-type(n)`.
- Otherwise, selector is `:nth-of-type(n)`, which is equal to passing "*",
- css wildcard selector as an argument.
Example
--------
>>> NthOfType("2").selector
:nth-of-type(2)
- >>> NthOfType("2", "li").selector
- li:nth-of-type(2)
>>> NthOfType("2n+1").selector
:nth-of-type(2n+1)
- >>> NthOfType("even", "div").selector
- div:nth-of-type(even)
-
- If tag is specified, two conditions must be met:
- - Tag is the nth of its type in its parent
- - Tag has the specified tag name
+ >>> NthOfType("even").selector
+ :nth-of-type(even)
For more information on the formula, see:
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type
"""
- def __init__(self, nth: str, /, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:nth-of-type({nth})")
+ _SELECTOR = ":nth-of-type({})"
-class NthLastOfType(CSSSoupSelector):
+class NthLastOfType(_NthBaseSelector):
"""
Class to select tags that are the nth last of their type in their parent.
It uses the CSS selector `:nth-last-of-type(n)`.
@@ -487,34 +389,21 @@ class NthLastOfType(CSSSoupSelector):
----------
nth : str, positional
Number of the tag to be selected. Can be a number or a formula.
- tag : str, optional
- Tag to be selected. If None, any tag is selected.
-
- In case of passing tag parameter, selector is `{tag}:nth-last-of-type(n)`.
- Otherwise, selector is `:nth-last-of-type(n)`, which is equal to passing "*",
- css wildcard selector as an argument.
Example
--------
>>> NthLastOfType("2").selector
:nth-last-of-type(2)
- >>> NthLastOfType("2", "li").selector
- li:nth-last-of-type(2)
>>> NthLastOfType("2n+1").selector
:nth-last-of-type(2n+1)
- >>> NthLastOfType("even", "div").selector
- div:nth-last-of-type(even)
-
- If tag is specified, two conditions must be met:
- - Tag is the nth last of its type in its parent
- - Tag has the specified tag name
+ >>> NthLastOfType("even").selector
+ :nth-last-of-type(even)
For more information on the formula, see:
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-of-type
"""
- def __init__(self, nth: str, /, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:nth-last-of-type({nth})")
+ _SELECTOR = ":nth-last-of-type({})"
class OnlyOfType(CSSSoupSelector):
@@ -522,11 +411,6 @@ class OnlyOfType(CSSSoupSelector):
Class to select tags that are the only of their type in their parent.
It uses the CSS selector `:only-of-type`.
- Parameters
- ----------
- tag : str, optional
- Tag to be selected. If None, any tag is selected.
-
Example
--------
>>>
❌
@@ -541,27 +425,16 @@ class OnlyOfType(CSSSoupSelector):
Tag is only selected if it is the only tag of its type in its parent.
- In case of passing tag parameter, selector is `{tag}:only-of-type`.
- Otherwise, selector is `:only-of-type`, which is equal to passing "*",
- css wildcard selector as an argument.
-
Example
--------
>>> OnlyOfType().selector
:only-of-type
- >>> OnlyOfType("div").selector
- div:only-of-type
-
- If tag is specified, two conditions must be met:
- - Tag is the only tag of its type in its parent
- - Tag has the specified tag name
For more information on the :only-of-type selector, see:
https://developer.mozilla.org/en-US/docs/Web/CSS/:only-of-type
"""
- def __init__(self, tag: Optional[str] = None) -> None:
- super().__init__(f"{tag or ''}:only-of-type")
+ _SELECTOR = ":only-of-type"
class CSS(CSSSoupSelector):
@@ -594,5 +467,12 @@ class CSS(CSSSoupSelector):
... ✔️
"""
+ _SELECTOR = "{}"
+
def __init__(self, css: str) -> None:
- super().__init__(css)
+ self._css = css
+ super().__init__()
+
+ @property
+ def _formats(self) -> list[str]:
+ return [self._css]
diff --git a/soupsavvy/testing/__init__.py b/soupsavvy/testing/__init__.py
index e69de29b..41f63ccd 100644
--- a/soupsavvy/testing/__init__.py
+++ b/soupsavvy/testing/__init__.py
@@ -0,0 +1,11 @@
+from .generators import AttributeGenerator, TagGenerator
+from .generators.templates import ChoiceTemplate, RandomTemplate
+from .generators.templates.base import BaseTemplate
+
+__all__ = [
+ "AttributeGenerator",
+ "TagGenerator",
+ "ChoiceTemplate",
+ "RandomTemplate",
+ "BaseTemplate",
+]
diff --git a/soupsavvy/testing/generators/generators.py b/soupsavvy/testing/generators/generators.py
index 828efb48..5f5ece63 100644
--- a/soupsavvy/testing/generators/generators.py
+++ b/soupsavvy/testing/generators/generators.py
@@ -117,7 +117,6 @@ def __init__(self, name: str, value: TemplateType = None) -> None:
value : TemplateType, optional
The value of the attribute. Defaults to None.
"""
-
self._check_name(name)
self.name = name
self.value = _get_template_type(
@@ -276,11 +275,14 @@ def __init__(
The text content of the tag.
Defaults to None, which generates empty string.
"""
+ if isinstance(attrs, str):
+ raise TypeError("'attrs' must be an iterable of attributes, not a string")
+
self._void = name in namespace.VOID_TAGS
if self._void and children:
raise exc.VoidTagWithChildrenException(
- f"{name} is a void tag and cannot have children"
+ f"'{name}' is a void tag and cannot have children"
)
self._check_name(name)
@@ -291,6 +293,7 @@ def __init__(
param="text",
default=DEFAULT_TEXT_TEMPLATE,
)
+
self.attributes = self._process_attributes(attrs)
self.children = [
child if isinstance(child, TagGenerator) else TagGenerator(child)
@@ -356,7 +359,7 @@ def _process_attributes(
except AttributeGeneratorInitExceptions as e:
raise exc.AttributeParsingError(
f"Attribute {attr} could not be parsed into AttributeGenerator "
- "due to following error."
+ "due to following error:"
) from e
attr_name = attr.name
diff --git a/tests/soupsavvy/selectors/css/selectors/css_wrapper_test.py b/tests/soupsavvy/selectors/css/selectors/css_wrapper_test.py
index 388f4681..b6088bb6 100644
--- a/tests/soupsavvy/selectors/css/selectors/css_wrapper_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/css_wrapper_test.py
@@ -55,7 +55,7 @@ def test_find_returns_first_tag_matching_selector(self):
"""
- bs = to_bs(text)
+ bs = find_body_element(to_bs(text))
selector = CSS("div.widget")
result = selector.find(bs)
assert strip(str(result)) == strip("""
1
""")
@@ -74,7 +74,7 @@ def test_find_returns_none_if_no_match_and_strict_false(self):
"""
- bs = to_bs(text)
+ bs = find_body_element(to_bs(text))
selector = CSS("div.widget")
result = selector.find(bs)
assert result is None
@@ -93,7 +93,7 @@ def test_find_raises_exception_if_no_match_and_strict_true(self):
"""
- bs = to_bs(text)
+ bs = find_body_element(to_bs(text))
selector = CSS("div.widget")
with pytest.raises(TagNotFoundException):
@@ -112,7 +112,7 @@ def test_find_all_returns_all_matching_elements(self):
"""
- bs = to_bs(text)
+ bs = find_body_element(to_bs(text))
selector = CSS("div.widget")
result = selector.find_all(bs)
@@ -133,7 +133,7 @@ def test_find_all_returns_empty_list_when_no_match(self):
"""
- bs = to_bs(text)
+ bs = find_body_element(to_bs(text))
selector = CSS("div.widget")
result = selector.find_all(bs)
assert result == []
diff --git a/tests/soupsavvy/selectors/css/selectors/empty_test.py b/tests/soupsavvy/selectors/css/selectors/empty_test.py
index e29d2ae7..32276172 100644
--- a/tests/soupsavvy/selectors/css/selectors/empty_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/empty_test.py
@@ -12,14 +12,10 @@
class TestEmpty:
"""Class with unit tests for Empty tag selector."""
- def test_selector_is_correct_without_tag(self):
- """Tests if selector property returns correct value without specifying tag."""
+ def test_css_selector_is_correct(self):
+ """Tests if css property returns correct value."""
assert Empty().css == ":empty"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert Empty("div").css == "div:empty"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -45,27 +41,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
Hello
-
-
-
-
Hello
-
- """
- bs = find_body_element(to_bs(text))
- selector = Empty("div")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
"""),
- strip("""
"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
diff --git a/tests/soupsavvy/selectors/css/selectors/first_child_test.py b/tests/soupsavvy/selectors/css/selectors/first_child_test.py
index 3b3fe2fd..8f2cbca5 100644
--- a/tests/soupsavvy/selectors/css/selectors/first_child_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/first_child_test.py
@@ -12,14 +12,10 @@
class TestFirstChild:
"""Class with unit tests for FirstChild tag selector."""
- def test_selector_is_correct_without_tag(self):
- """Tests if selector property returns correct value without specifying tag."""
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert FirstChild().css == ":first-child"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert FirstChild("div").css == "div:first-child"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -40,104 +36,22 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip(""""""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
1
-
-
-
- """
- bs = find_body_element(to_bs(text))
- selector = FirstChild("div")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
1
"""),
- strip("""
"""),
- strip("""
3
"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
-
Hello
-
-
2
-
-
- """
- bs = to_bs(text)
- selector = FirstChild("a")
- result = selector.find(bs)
- assert strip(str(result)) == strip("""
1""")
-
- def test_find_returns_none_if_no_match_and_strict_false(self):
- """
- Tests if find returns None if no element matches the selector
- and strict is False.
- """
- text = """
-
+
1
-
Hello
+
3
"""
- bs = to_bs(text)
- selector = FirstChild("a")
+ bs = find_body_element(to_bs(text))
+ selector = FirstChild()
result = selector.find(bs)
- assert result is None
-
- def test_find_raises_exception_if_no_match_and_strict_true(self):
- """
- Tests if find raises TagNotFoundException if no element matches the selector
- and strict is True.
- """
- text = """
-
-
- Hello
-
- """
- bs = to_bs(text)
- selector = FirstChild("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True)
-
- def test_find_all_returns_empty_list_when_no_match(self):
- """Tests if find returns an empty list if no element matches the selector."""
- text = """
-
-
- Hello
-
- """
- bs = to_bs(text)
- selector = FirstChild("a")
- result = selector.find_all(bs)
- assert result == []
+ assert strip(str(result)) == strip("""1
""")
def test_find_returns_first_matching_child_if_recursive_false(self):
"""
@@ -154,70 +68,10 @@ def test_find_returns_first_matching_child_if_recursive_false(self):
Not child
"""
bs = find_body_element(to_bs(text))
- selector = FirstChild("a")
+ selector = FirstChild()
result = selector.find(bs, recursive=False)
assert strip(str(result)) == strip("""1""")
- def test_find_returns_none_if_recursive_false_and_no_matching_child(self):
- """
- Tests if find returns None if no child element matches the selector
- and recursive is False.
- """
- text = """
-
-
- Not child
- """
- bs = find_body_element(to_bs(text))
- selector = FirstChild("a")
- result = selector.find(bs, recursive=False)
- assert result is None
-
- def test_find_raises_exception_with_recursive_false_and_strict_mode(self):
- """
- Tests if find raises TagNotFoundException if no child element
- matches the selector, when recursive is False and strict is True.
- """
- text = """
-
-
- Not child
- """
- bs = find_body_element(to_bs(text))
- selector = FirstChild("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True, recursive=False)
-
- def test_find_all_returns_empty_list_if_none_matching_children_when_recursive_false(
- self,
- ):
- """
- Tests if find_all returns an empty list if no child element matches the selector
- and recursive is False.
- """
- text = """
-
-
- Not child
- """
- bs = find_body_element(to_bs(text))
- selector = FirstChild("a")
- result = selector.find_all(bs, recursive=False)
- assert result == []
-
def test_find_all_returns_all_matching_children_when_recursive_false(self):
"""
Tests if find_all returns all matching children if recursive is False.
@@ -245,21 +99,20 @@ def test_find_all_returns_only_x_elements_when_limit_is_set(self):
In this case only 2 first in order elements are returned.
"""
text = """
- Hello
+ 1
- 2
+ 3
-
"""
bs = find_body_element(to_bs(text))
- selector = FirstChild("a")
+ selector = FirstChild()
result = selector.find_all(bs, limit=2)
assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""1"""),
+ strip("""1
"""),
strip("""2"""),
]
diff --git a/tests/soupsavvy/selectors/css/selectors/first_of_type_test.py b/tests/soupsavvy/selectors/css/selectors/first_of_type_test.py
index a9ecdf4b..23337a38 100644
--- a/tests/soupsavvy/selectors/css/selectors/first_of_type_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/first_of_type_test.py
@@ -12,14 +12,10 @@
class TestFirstOfType:
"""Class with unit tests for FirstOfType tag selector."""
- def test_selector_is_correct_without_tag(self):
- """Tests if selector property returns correct value without specifying tag."""
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert FirstOfType().css == ":first-of-type"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert FirstOfType("div").css == "div:first-of-type"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -46,167 +42,37 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""56"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
- Hello
-
- 1
-
-
- """
- bs = find_body_element(to_bs(text))
- selector = FirstOfType("a")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""1"""),
- strip("""2"""),
- strip("""3"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
-
+ 1
Hello
- 1
- 2
- Hello
+ 2
+ 3
+ 4
Hello
-
- """
- bs = to_bs(text)
- selector = FirstOfType("a")
- result = selector.find(bs)
- assert strip(str(result)) == strip("""1""")
-
- def test_find_returns_none_if_no_match_and_strict_false(self):
- """
- Tests if find returns None if no element matches the selector
- and strict is False.
"""
- text = """
-
- Hello
- Not child
- Hello
- """
- bs = to_bs(text)
- selector = FirstOfType("a")
+ bs = find_body_element(to_bs(text))
+ selector = FirstOfType()
result = selector.find(bs)
- assert result is None
-
- def test_find_raises_exception_if_no_match_and_strict_true(self):
- """
- Tests if find raises TagNotFoundException if no element matches the selector
- and strict is True.
- """
- text = """
-
- Hello
- Not child
- Hello
- """
- bs = to_bs(text)
- selector = FirstOfType("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True)
-
- def test_find_all_returns_empty_list_when_no_match(self):
- """Tests if find returns an empty list if no element matches the selector."""
- text = """
-
- Hello
- Not child
- Hello
- """
- bs = to_bs(text)
- selector = FirstOfType("a")
- result = selector.find_all(bs)
- assert result == []
+ assert strip(str(result)) == strip("""1
""")
def test_find_returns_first_matching_child_if_recursive_false(self):
"""
Tests if find returns first matching child element if recursive is False.
"""
text = """
-
- Hello
+ 1
Not child
- 1
- Hello
- Hello
-
- """
- bs = find_body_element(to_bs(text))
- selector = FirstOfType("a")
- result = selector.find(bs, recursive=False)
- assert strip(str(result)) == strip("""1""")
-
- def test_find_returns_none_if_recursive_false_and_no_matching_child(self):
- """
- Tests if find returns None if no child element matches the selector
- and recursive is False.
- """
- text = """
-
+ 2
Hello
- Not child
- Hello
-
+ 3
+ Not child
"""
bs = find_body_element(to_bs(text))
- selector = FirstOfType("a")
+ selector = FirstOfType()
result = selector.find(bs, recursive=False)
- assert result is None
-
- def test_find_raises_exception_with_recursive_false_and_strict_mode(self):
- """
- Tests if find raises TagNotFoundException if no child element
- matches the selector, when recursive is False and strict is True.
- """
- text = """
-
- Hello
- Not child
- Hello
-
- """
- bs = find_body_element(to_bs(text))
- selector = FirstOfType("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True, recursive=False)
-
- def test_find_all_returns_empty_list_if_none_matching_children_when_recursive_false(
- self,
- ):
- """
- Tests if find_all returns an empty list if no child element matches the selector
- and recursive is False.
- """
- text = """
-
- Hello
- Not child
- Hello
-
- """
- bs = find_body_element(to_bs(text))
- selector = FirstOfType("a")
- result = selector.find_all(bs, recursive=False)
- assert result == []
+ assert strip(str(result)) == strip("""1""")
def test_find_all_returns_all_matching_children_when_recursive_false(self):
"""
diff --git a/tests/soupsavvy/selectors/css/selectors/last_child_test.py b/tests/soupsavvy/selectors/css/selectors/last_child_test.py
index 20b77e07..e40ea847 100644
--- a/tests/soupsavvy/selectors/css/selectors/last_child_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/last_child_test.py
@@ -12,14 +12,10 @@
class TestLastChild:
"""Class with unit tests for LastChild tag selector."""
- def test_selector_is_correct_without_tag(self):
- """Tests if selector property returns correct value without specifying tag."""
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert LastChild().css == ":last-child"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert LastChild("div").css == "div:last-child"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -40,34 +36,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip(""""""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
-
-
-
-
- """
- bs = find_body_element(to_bs(text))
- selector = LastChild("div")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""1
"""),
- strip(""""""),
- strip("""3
"""),
- strip(""""""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
@@ -78,69 +46,14 @@ def test_find_returns_first_tag_matching_selector(self):
1
2
-
Hello
-
+
+
4
"""
- bs = to_bs(text)
- selector = LastChild("a")
+ bs = find_body_element(to_bs(text))
+ selector = LastChild()
result = selector.find(bs)
assert strip(str(result)) == strip("""
1""")
- def test_find_returns_none_if_no_match_and_strict_false(self):
- """
- Tests if find returns None if no element matches the selector
- and strict is False.
- """
- text = """
-
-
-
-
Hello
- """
- bs = to_bs(text)
- selector = LastChild("a")
- result = selector.find(bs)
- assert result is None
-
- def test_find_raises_exception_if_no_match_and_strict_true(self):
- """
- Tests if find raises TagNotFoundException if no element matches the selector
- and strict is True.
- """
- text = """
-
-
-
-
Hello
- """
- bs = to_bs(text)
- selector = LastChild("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True)
-
- def test_find_all_returns_empty_list_when_no_match(self):
- """Tests if find returns an empty list if no element matches the selector."""
- text = """
-
-
-
-
Hello
- """
- bs = to_bs(text)
- selector = LastChild("a")
- result = selector.find_all(bs)
- assert result == []
-
def test_find_returns_first_matching_child_if_recursive_false(self):
"""
Tests if find returns first matching child element if recursive is False.
@@ -157,73 +70,10 @@ def test_find_returns_first_matching_child_if_recursive_false(self):
1
"""
bs = find_body_element(to_bs(text))
- selector = LastChild("a")
+ selector = LastChild()
result = selector.find(bs, recursive=False)
assert strip(str(result)) == strip("""
1""")
- def test_find_returns_none_if_recursive_false_and_no_matching_child(self):
- """
- Tests if find returns None if no child element matches the selector
- and recursive is False.
- """
- text = """
-
Hello
-
-
Hello
-
Not child
- """
- bs = find_body_element(to_bs(text))
- selector = LastChild("a")
- result = selector.find(bs, recursive=False)
- assert result is None
-
- def test_find_raises_exception_with_recursive_false_and_strict_mode(self):
- """
- Tests if find raises TagNotFoundException if no child element
- matches the selector, when recursive is False and strict is True.
- """
- text = """
-
Hello
-
-
Hello
-
Not child
- """
- bs = find_body_element(to_bs(text))
- selector = LastChild("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True, recursive=False)
-
- def test_find_all_returns_empty_list_if_none_matching_children_when_recursive_false(
- self,
- ):
- """
- Tests if find_all returns an empty list if no child element matches the selector
- and recursive is False.
- """
- text = """
-
Hello
-
-
Hello
-
Not child
- """
- bs = find_body_element(to_bs(text))
- selector = LastChild("a")
- result = selector.find_all(bs, recursive=False)
- assert result == []
-
def test_find_all_returns_all_matching_children_when_recursive_false(self):
"""
Tests if find_all returns all matching children if recursive is False.
@@ -259,11 +109,11 @@ def test_find_all_returns_only_x_elements_when_limit_is_set(self):
1
2
-
Hello
-
+
+
4
"""
bs = find_body_element(to_bs(text))
- selector = LastChild("a")
+ selector = LastChild()
result = selector.find_all(bs, limit=2)
assert list(map(lambda x: strip(str(x)), result)) == [
diff --git a/tests/soupsavvy/selectors/css/selectors/last_of_type_test.py b/tests/soupsavvy/selectors/css/selectors/last_of_type_test.py
index c1ba9601..950dc6f4 100644
--- a/tests/soupsavvy/selectors/css/selectors/last_of_type_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/last_of_type_test.py
@@ -12,14 +12,10 @@
class TestLastOfType:
"""Class with unit tests for LastOfType tag selector."""
- def test_selector_is_correct_without_tag(self):
- """Tests if selector property returns correct value without specifying tag."""
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert LastOfType().css == ":last-of-type"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert LastOfType("div").css == "div:last-of-type"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -46,34 +42,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
56"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
Hello
-
-
-
-
3
- """
- bs = find_body_element(to_bs(text))
- selector = LastOfType("a")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
1"""),
- strip("""
2"""),
- strip("""
3"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
@@ -81,132 +49,32 @@ def test_find_returns_first_tag_matching_selector(self):
Hello
Hello
1
-
Hello
-
2
-
+
2
+
3
+
"""
- bs = to_bs(text)
- selector = LastOfType("a")
+ bs = find_body_element(to_bs(text))
+ selector = LastOfType()
result = selector.find(bs)
assert strip(str(result)) == strip("""
1""")
- def test_find_returns_none_if_no_match_and_strict_false(self):
- """
- Tests if find returns None if no element matches the selector
- and strict is False.
- """
- text = """
-
-
Hello
-
-
Hello
- """
- bs = to_bs(text)
- selector = LastOfType("a")
- result = selector.find(bs)
- assert result is None
-
- def test_find_raises_exception_if_no_match_and_strict_true(self):
- """
- Tests if find raises TagNotFoundException if no element matches the selector
- and strict is True.
- """
- text = """
-
-
Hello
-
-
Hello
- """
- bs = to_bs(text)
- selector = LastOfType("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True)
-
- def test_find_all_returns_empty_list_when_no_match(self):
- """Tests if find returns an empty list if no element matches the selector."""
- text = """
-
-
Hello
-
-
Hello
- """
- bs = to_bs(text)
- selector = LastOfType("a")
- result = selector.find_all(bs)
- assert result == []
-
def test_find_returns_first_matching_child_if_recursive_false(self):
"""
Tests if find returns first matching child element if recursive is False.
"""
text = """
-
Hello
-
Not child
Hello
-
Hello
-
1
-
- """
- bs = find_body_element(to_bs(text))
- selector = LastOfType("a")
- result = selector.find(bs, recursive=False)
- assert strip(str(result)) == strip("""
1""")
-
- def test_find_returns_none_if_recursive_false_and_no_matching_child(self):
- """
- Tests if find returns None if no child element matches the selector
- and recursive is False.
- """
- text = """
-
Hello
Not child
-
Hello
-
+
1
+
2
+
"""
bs = find_body_element(to_bs(text))
- selector = LastOfType("a")
+ selector = LastOfType()
result = selector.find(bs, recursive=False)
- assert result is None
-
- def test_find_raises_exception_with_recursive_false_and_strict_mode(self):
- """
- Tests if find raises TagNotFoundException if no child element
- matches the selector, when recursive is False and strict is True.
- """
- text = """
-
-
Hello
-
Not child
-
Hello
-
- """
- bs = find_body_element(to_bs(text))
- selector = LastOfType("a")
-
- with pytest.raises(TagNotFoundException):
- selector.find(bs, strict=True, recursive=False)
-
- def test_find_all_returns_empty_list_if_none_matching_children_when_recursive_false(
- self,
- ):
- """
- Tests if find_all returns an empty list if no child element matches the selector
- and recursive is False.
- """
- text = """
-
-
Hello
-
Not child
-
Hello
-
- """
- bs = find_body_element(to_bs(text))
- selector = LastOfType("a")
- result = selector.find_all(bs, recursive=False)
- assert result == []
+ assert strip(str(result)) == strip("""
1""")
def test_find_all_returns_all_matching_children_when_recursive_false(self):
"""
diff --git a/tests/soupsavvy/selectors/css/selectors/nth_child_test.py b/tests/soupsavvy/selectors/css/selectors/nth_child_test.py
index 58e49712..1898ac7e 100644
--- a/tests/soupsavvy/selectors/css/selectors/nth_child_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/nth_child_test.py
@@ -12,16 +12,10 @@
class TestNthChild:
"""Class with unit tests for NthChild tag selector."""
- def test_selector_is_correct_without_tag(self):
- """
- Tests if selector property returns correct value without specifying tag.
- """
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert NthChild("2n").css == ":nth-child(2n)"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert NthChild("2n", tag="div").css == "div:nth-child(2n)"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -46,30 +40,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
5
"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
-
Not p
-
-
Hello
-
1
-
Hello
-
2
-
-
Not p
-
Hello
-
3
- """
- bs = find_body_element(to_bs(text))
- selector = NthChild("2n", tag="p")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
1
"""),
- strip("""
2
"""),
- strip("""
3
"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
@@ -281,47 +251,6 @@ def test_init_raise_exception_with_invalid_selector(self):
with pytest.raises(InvalidCSSSelector):
NthChild("2x+1")
- @pytest.mark.parametrize(
- argnames="nth, expected",
- argvalues=[
- ("2n", [2, 4, 6]),
- ("2n+1", [1, 3, 5]),
- # ignores whitespaces
- (" 2n + 1", [1, 3, 5]),
- ("-n+3", [1, 2, 3]),
- ("even", [2, 4, 6]),
- ("odd", [1, 3, 5]),
- ("3", [3]),
- ("-3n", []),
- ("-3n+10", [1, 4]),
- ],
- )
- def test_returns_elements_based_on_nth_selector_and_tag(
- self, nth: str, expected: list[int]
- ):
- """
- Tests if find_all returns all elements with specified tag name
- matching various nth selectors.
- """
- text = """
-
1
-
2
-
3
-
4
-
5
-
6
-
Hello
-
Hello
-
- """
- bs = find_body_element(to_bs(text))
- selector = NthChild(nth, tag="div")
- results = selector.find_all(bs)
-
- assert list(map(lambda x: strip(str(x)), results)) == [
- f"""
{i}
""" for i in expected
- ]
-
@pytest.mark.parametrize(
argnames="nth, expected",
argvalues=[
diff --git a/tests/soupsavvy/selectors/css/selectors/nth_last_child_test.py b/tests/soupsavvy/selectors/css/selectors/nth_last_child_test.py
index d7053ae9..1d610df3 100644
--- a/tests/soupsavvy/selectors/css/selectors/nth_last_child_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/nth_last_child_test.py
@@ -12,16 +12,10 @@
class TestNthLastChild:
"""Class with unit tests for NthLastChild tag selector."""
- def test_selector_is_correct_without_tag(self):
- """
- Tests if selector property returns correct value without specifying tag.
- """
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert NthLastChild("2n").css == ":nth-last-child(2n)"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert NthLastChild("2n", tag="div").css == "div:nth-last-child(2n)"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -46,31 +40,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
5
"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
Not p
-
-
4
-
Not nth
-
Not p
-
- """
- bs = find_body_element(to_bs(text))
- selector = NthLastChild("2n", tag="p")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
1
"""),
- strip("""
2Hello
"""),
- strip("""
3
"""),
- strip("""
4
"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
@@ -282,47 +251,6 @@ def test_init_raise_exception_with_invalid_selector(self):
with pytest.raises(InvalidCSSSelector):
NthLastChild("2x+1")
- @pytest.mark.parametrize(
- argnames="nth, expected",
- argvalues=[
- ("2n", [1, 3, 5]),
- ("2n+1", [2, 4, 6]),
- # ignores whitespaces
- (" 2n + 1", [2, 4, 6]),
- ("-n+3", [4, 5, 6]),
- ("even", [1, 3, 5]),
- ("odd", [2, 4, 6]),
- ("3", [4]),
- ("-3n", []),
- ("-3n+10", [3, 6]),
- ],
- )
- def test_returns_elements_based_on_nth_selector_and_tag(
- self, nth: str, expected: list[int]
- ):
- """
- Tests if find_all returns all elements with specified tag name
- matching various nth selectors.
- """
- text = """
-
Hello
-
Hello
-
-
1
-
2
-
3
-
4
-
5
-
6
- """
- bs = find_body_element(to_bs(text))
- selector = NthLastChild(nth, tag="div")
- results = selector.find_all(bs)
-
- assert list(map(lambda x: strip(str(x)), results)) == [
- f"""
{i}
""" for i in expected
- ]
-
@pytest.mark.parametrize(
argnames="nth, expected",
argvalues=[
diff --git a/tests/soupsavvy/selectors/css/selectors/nth_last_of_type_test.py b/tests/soupsavvy/selectors/css/selectors/nth_last_of_type_test.py
index 61eab3fc..c880d877 100644
--- a/tests/soupsavvy/selectors/css/selectors/nth_last_of_type_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/nth_last_of_type_test.py
@@ -14,16 +14,10 @@
class TestNthLastOfType:
"""Class with unit tests for NthLastOfType tag selector."""
- def test_selector_is_correct_without_tag(self):
- """
- Tests if selector property returns correct value without specifying tag.
- """
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert NthLastOfType("2n").css == ":nth-last-of-type(2n)"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert NthLastOfType("2n", tag="div").css == "div:nth-last-of-type(2n)"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -51,31 +45,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
5"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
-
1
-
Not a
-
Hello
-
-
3
-
Hello
- """
- bs = find_body_element(to_bs(text))
- selector = NthLastOfType("2n", tag="a")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
1"""),
- strip("""
2"""),
- strip("""
3
"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
@@ -322,47 +291,6 @@ def test_init_raise_exception_with_invalid_selector(self):
with pytest.raises(InvalidCSSSelector):
NthLastOfType("2x+1")
- @pytest.mark.parametrize(
- argnames="nth, expected",
- argvalues=[
- ("2n", [1, 3, 5]),
- ("2n+1", [2, 4, 6]),
- # ignores whitespaces
- (" 2n + 1", [2, 4, 6]),
- ("-n+3", [4, 5, 6]),
- ("even", [1, 3, 5]),
- ("odd", [2, 4, 6]),
- ("3", [4]),
- ("-3n", []),
- ("-3n+10", [3, 6]),
- ],
- )
- def test_returns_elements_based_on_nth_selector_and_tag(
- self, nth: str, expected: list[int]
- ):
- """
- Tests if find_all returns all elements with specified tag name
- matching various nth selectors.
- """
- text = """
-
text 1
-
Hello
-
text 2
-
text 3
-
Hello
-
Hello
-
text 4
-
Hello world
-
text 5
-
text 6
- """
- bs = find_body_element(to_bs(text))
- selector = NthLastOfType(nth, tag="p")
- results = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), results)) == [
- f"""
text {i}
""" for i in expected
- ]
-
@pytest.mark.parametrize(
argnames="nth, expected",
argvalues=[
diff --git a/tests/soupsavvy/selectors/css/selectors/nth_of_type_test.py b/tests/soupsavvy/selectors/css/selectors/nth_of_type_test.py
index b6d62592..4714b55f 100644
--- a/tests/soupsavvy/selectors/css/selectors/nth_of_type_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/nth_of_type_test.py
@@ -14,16 +14,10 @@
class TestNthOfType:
"""Class with unit tests for NthOfType tag selector."""
- def test_selector_is_correct_without_tag(self):
- """
- Tests if selector property returns correct value without specifying tag.
- """
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert NthOfType("2n").css == ":nth-of-type(2n)"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert NthOfType("2n", tag="div").css == "div:nth-of-type(2n)"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -51,31 +45,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
5"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
-
Hello
-
1
-
Hello
-
-
Hello
-
3
- """
- bs = find_body_element(to_bs(text))
- selector = NthOfType("2n", tag="a")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
1"""),
- strip("""
2"""),
- strip("""
3
"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
@@ -322,47 +291,6 @@ def test_init_raise_exception_with_invalid_selector(self):
with pytest.raises(InvalidCSSSelector):
NthOfType("2x+1")
- @pytest.mark.parametrize(
- argnames="nth, expected",
- argvalues=[
- ("2n", [2, 4, 6]),
- ("2n+1", [1, 3, 5]),
- # ignores whitespaces
- (" 2n + 1", [1, 3, 5]),
- ("-n+3", [1, 2, 3]),
- ("even", [2, 4, 6]),
- ("odd", [1, 3, 5]),
- ("3", [3]),
- ("-3n", []),
- ("-3n+10", [1, 4]),
- ],
- )
- def test_returns_elements_based_on_nth_selector_and_tag(
- self, nth: str, expected: list[int]
- ):
- """
- Tests if find_all returns all elements with specified tag name
- matching various nth selectors.
- """
- text = """
-
text 1
-
Hello
-
text 2
-
text 3
-
Hello
-
Hello
-
text 4
-
Hello world
-
text 5
-
text 6
- """
- bs = find_body_element(to_bs(text))
- selector = NthOfType(nth, tag="p")
- results = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), results)) == [
- f"""
text {i}
""" for i in expected
- ]
-
@pytest.mark.parametrize(
argnames="nth, expected",
argvalues=[
diff --git a/tests/soupsavvy/selectors/css/selectors/only_child_test.py b/tests/soupsavvy/selectors/css/selectors/only_child_test.py
index eaab59a3..fabace18 100644
--- a/tests/soupsavvy/selectors/css/selectors/only_child_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/only_child_test.py
@@ -12,14 +12,10 @@
class TestOnlyChild:
"""Class with unit tests for OnlyChild tag selector."""
- def test_selector_is_correct_without_tag(self):
- """Tests if selector property returns correct value without specifying tag."""
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert OnlyChild().css == ":only-child"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert OnlyChild("div").css == "div:only-child"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -41,26 +37,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
3"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
-
-
-
3
-
- """
- bs = find_body_element(to_bs(text))
- selector = OnlyChild("a")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
3
"""),
- strip("""
3"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
diff --git a/tests/soupsavvy/selectors/css/selectors/only_of_type_test.py b/tests/soupsavvy/selectors/css/selectors/only_of_type_test.py
index c7197c8a..e2f8d5a3 100644
--- a/tests/soupsavvy/selectors/css/selectors/only_of_type_test.py
+++ b/tests/soupsavvy/selectors/css/selectors/only_of_type_test.py
@@ -12,14 +12,10 @@
class TestOnlyOfType:
"""Class with unit tests for OnlyOfType tag selector."""
- def test_selector_is_correct_without_tag(self):
- """Tests if selector property returns correct value without specifying tag."""
+ def test_css_selector_is_correct(self):
+ """Tests if selector property returns correct value."""
assert OnlyOfType().css == ":only-of-type"
- def test_selector_is_correct_with_tag(self):
- """Tests if selector property returns correct value when specifying tag."""
- assert OnlyOfType("div").css == "div:only-of-type"
-
def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
"""Tests if find_all method returns all tags for selector without tag name."""
text = """
@@ -42,26 +38,6 @@ def test_find_all_returns_all_tags_for_selector_without_tag_name(self):
strip("""
4
"""),
]
- def test_find_all_returns_all_tags_for_selector_with_tag_name(self):
- """Tests if find_all method returns all tags for selector with tag name."""
- text = """
-
-
1
-
-
-
- """
- bs = find_body_element(to_bs(text))
- selector = OnlyOfType("p")
- result = selector.find_all(bs)
- assert list(map(lambda x: strip(str(x)), result)) == [
- strip("""
1
"""),
- strip("""
2
"""),
- ]
-
def test_find_returns_first_tag_matching_selector(self):
"""Tests if find method returns first tag matching selector."""
text = """
diff --git a/tests/soupsavvy/testing/generators/generators_test.py b/tests/soupsavvy/testing/generators/generators_test.py
index eb4b5d36..139067bc 100644
--- a/tests/soupsavvy/testing/generators/generators_test.py
+++ b/tests/soupsavvy/testing/generators/generators_test.py
@@ -100,6 +100,14 @@ def test_raises_exception_if_empty_string_passed_as_name(self):
class TestTagGenerator:
"""Component with unit tests for the TagGenerator class."""
+ def test_raises_error_if_attrs_parameter_is_string(self):
+ """
+ Test that the generator raises TypeError upon initialization
+ if the attrs parameter is a string.
+ """
+ with pytest.raises(TypeError):
+ TagGenerator(name="div", attrs="class")
+
def test_generates_empty_tag_if_only_name_specified(self):
"""
Test that the generator returns string with empty tag