Skip to content

🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev

test: add coverage for TextHandler regex, clean, and TextHandlers.re()

haosenwang1018717827958674
diff --git a/tests/parser/test_parser_advanced.py b/tests/parser/test_parser_advanced.py
index f34ba5c..969ccc3 100644
--- a/tests/parser/test_parser_advanced.py
+++ b/tests/parser/test_parser_advanced.py
@@ -250,6 +250,68 @@ class TestTextHandlerAdvanced:
matches = text3.re(r"He l lo", clean_match=True, case_sensitive=False)
assert len(matches) == 1
+ def test_text_handler_regex_check_match(self):
+ """Test TextHandler.re() with check_match=True returns bool"""
+ text = TextHandler("Price: $10.99")
+ assert text.re(r"\$[\d.]+", check_match=True) is True
+ assert text.re(r"no-match-pattern", check_match=True) is False
+
+ def test_text_handler_regex_replace_entities_false(self):
+ """Test TextHandler.re() with replace_entities=False preserves entities"""
+ text = TextHandler("Hello & World")
+ results = text.re(r"&", replace_entities=False)
+ assert len(results) == 1
+ assert results[0] == "&"
+
+ def test_text_handler_regex_with_groups(self):
+ """Test TextHandler.re() with capture groups flattens results"""
+ text = TextHandler("name=Alice age=30 name=Bob age=25")
+ results = text.re(r"name=(\w+) age=(\d+)")
+ assert len(results) == 4
+ assert "Alice" in results
+ assert "30" in results
+
+ def test_text_handler_re_first_with_default(self):
+ """Test TextHandler.re_first() returns default when no match"""
+ text = TextHandler("no numbers here")
+ result = text.re_first(r"\d+", default="N/A")
+ assert result == "N/A"
+
+ def test_text_handler_re_first_returns_first_match(self):
+ """Test TextHandler.re_first() returns first match"""
+ text = TextHandler("a1 b2 c3")
+ result = text.re_first(r"\d")
+ assert result == "1"
+ assert isinstance(result, TextHandler)
+
+ def test_text_handler_clean_with_entities(self):
+ """Test TextHandler.clean() with remove_entities=True"""
+ text = TextHandler("Hello\t&\nWorld")
+ cleaned = text.clean(remove_entities=True)
+ assert "&" not in cleaned
+ assert "&" in cleaned
+ assert "\t" not in cleaned
+ assert "\n" not in cleaned
+
+ def test_text_handler_clean_without_entities(self):
+ """Test TextHandler.clean() preserves entities by default"""
+ text = TextHandler("Hello\t&\nWorld")
+ cleaned = text.clean(remove_entities=False)
+ assert "&" in cleaned
+
+ def test_text_handler_json_valid(self):
+ """Test TextHandler.json() with valid JSON"""
+ text = TextHandler('{"key": "value", "num": 42}')
+ data = text.json()
+ assert data["key"] == "value"
+ assert data["num"] == 42
+
+ def test_text_handler_json_invalid(self):
+ """Test TextHandler.json() raises on invalid JSON"""
+ text = TextHandler("not json")
+ with pytest.raises(Exception):
+ text.json()
+
def test_text_handlers_operations(self):
"""Test TextHandlers list operations"""
handlers = TextHandlers([
@@ -266,6 +328,37 @@ class TestTextHandlerAdvanced:
assert handlers.get("default") == "First"
assert TextHandlers([]).get("default") == "default"
+ def test_text_handlers_re(self):
+ """Test TextHandlers.re() flattens results across all elements"""
+ handlers = TextHandlers([
+ TextHandler("a1 b2"),
+ TextHandler("c3 d4"),
+ ])
+ results = handlers.re(r"[a-z]\d")
+ assert isinstance(results, TextHandlers)
+ assert len(results) == 4
+ assert results[0] == "a1"
+ assert results[3] == "d4"
+
+ def test_text_handlers_re_empty(self):
+ """Test TextHandlers.re() on empty list"""
+ handlers = TextHandlers([])
+ results = handlers.re(r"\d+")
+ assert isinstance(results, TextHandlers)
+ assert len(results) == 0
+
+ def test_text_handlers_re_no_matches(self):
+ """Test TextHandlers.re() when no element matches"""
+ handlers = TextHandlers([TextHandler("abc"), TextHandler("def")])
+ results = handlers.re(r"\d+")
+ assert len(results) == 0
+
+ def test_text_handlers_extract(self):
+ """Test TextHandlers.extract() returns self"""
+ handlers = TextHandlers([TextHandler("a"), TextHandler("b")])
+ assert handlers.extract() is handlers
+ assert handlers.get_all() is handlers
+
class TestSelectorsAdvanced:
"""Test advanced Selectors functionality"""