From 7178279586749a802337b2426b95218b13712775 Mon Sep 17 00:00:00 2001 From: haosenwang1018 <1293965075@qq.com> Date: Sun, 15 Mar 2026 17:27:55 +0800 Subject: [PATCH] test: add coverage for TextHandler regex, clean, and TextHandlers.re() Several critical code paths in custom_types.py lacked test coverage: - TextHandler.re(check_match=True): returns bool, not TextHandlers - TextHandler.re(replace_entities=False): entity preservation path - TextHandler.re() with capture groups: flatten behavior - TextHandler.re_first() default value when no match - TextHandler.clean(remove_entities=True): entity replacement path - TextHandler.json() valid and invalid input - TextHandlers.re(): list-level regex with result flattening - TextHandlers.extract()/get_all(): identity return Co-Authored-By: Claude Opus 4.6 (1M context) --- tests/parser/test_parser_advanced.py | 93 ++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) 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"""