From 75aba18ce4e90802436aea18637898d0f234a503 Mon Sep 17 00:00:00 2001 From: torrua Date: Wed, 1 Oct 2025 22:22:06 +0300 Subject: [PATCH] feat(word-sourcer): implement get_parent_complex method with comprehensive test suite Adds the `get_parent_complex` method to the `WordSourcer` class, which processes word origins by removing the first element and any initial linkers (y, r, n). Includes robust test coverage for various cases including single elements,linker removal, and edge cases like empty inputs or trailing linkers.Also fixes a potential IndexError in the linker removal logic by adding a safety check for empty lists. --- loglan_core/addons/word_sourcer.py | 4 +- .../test_addons/test_word_sourcer.py | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/loglan_core/addons/word_sourcer.py b/loglan_core/addons/word_sourcer.py index 56be024..7558ac4 100644 --- a/loglan_core/addons/word_sourcer.py +++ b/loglan_core/addons/word_sourcer.py @@ -303,7 +303,9 @@ def get_parent_complex(origin: str) -> str: origin_list = WordSourcer.prepare_origin(origin).split("+") origin_list = origin_list[1:] origin_list = ( - origin_list if origin_list[0] not in ["y", "r", "n"] else origin_list[1:] + origin_list + if origin_list and origin_list[0] not in ["y", "r", "n"] + else origin_list[1:] ) return "".join(origin_list) diff --git a/tests/test_sync/test_loglan_core/test_addons/test_word_sourcer.py b/tests/test_sync/test_loglan_core/test_addons/test_word_sourcer.py index 6dc49cf..408d96b 100644 --- a/tests/test_sync/test_loglan_core/test_addons/test_word_sourcer.py +++ b/tests/test_sync/test_loglan_core/test_addons/test_word_sourcer.py @@ -139,3 +139,60 @@ def test_adjacent_operations(self): """Test parentheses adjacent to slashes""" result = WordSourcer.prepare_origin("a(b)/c(d)+e(f)/g(h)") assert result == "ca+ge" + + +class TestGetParentComplex: + """Test suite for WordSourcer.get_parent_complex method""" + + def test_example_cases(self): + """Test the examples provided in the docstring""" + # zav(lo)+da(n)z(a)+fo/l(ma) => zav+daz+flo => dazflo + result = WordSourcer.get_parent_complex("zav(lo)+da(n)z(a)+fo/l(ma)") + assert result == "dazflo" + + # zanynurkokmio -> za(v)n(o)+y+nur+kok(fa)+mi(tr)o => za+no+y+nur+kok+mio => no+y+nur+kok+mio => nurkokmio + result = WordSourcer.get_parent_complex("za(v)n(o)+y+nur+kok(fa)+mi(tr)o") + assert result == "nurkokmio" + + # cabsrusia -> cab(ro)+su/r(na)+si(tf)a => cab+sur+sitfa => sursitfa + result = WordSourcer.get_parent_complex("cab(ro)+su/r(na)+si(tf)a") + assert result == "srusia" + + # be(rt)i+n+(t)rac(i)+ve(sl)o => bei+n+rac+i+velo => n+rac+i+velo => raciveloo + result = WordSourcer.get_parent_complex("be(rt)i+n+(t)rac(i)+ve(sl)o") + assert result == "racveo" + + def test_single_element(self): + """Test case where there's only one element after prepare_origin""" + result = WordSourcer.get_parent_complex("abc(def)") + assert result == "" # After removing first (and only) element, nothing remains + + def test_with_linker_y(self): + """Test case with 'y' linker that should be removed""" + result = WordSourcer.get_parent_complex("first+y+third+fourth") + assert result == "thirdfourth" + + def test_with_linker_r(self): + """Test case with 'r' linker that should be removed""" + result = WordSourcer.get_parent_complex("first+r+third+fourth") + assert result == "thirdfourth" + + def test_with_linker_n(self): + """Test case with 'n' linker that should be removed""" + result = WordSourcer.get_parent_complex("first+n+third+fourth") + assert result == "thirdfourth" + + def test_without_linkers(self): + """Test case without linker elements""" + result = WordSourcer.get_parent_complex("first+second+third") + assert result == "secondthird" + + def test_empty_input(self): + """Test case with empty input""" + result = WordSourcer.get_parent_complex("") + assert result == "" + + def test_only_linker_after_first(self): + """Test case where only linker remains after removing first element""" + result = WordSourcer.get_parent_complex("first+y") + assert result == "" # After removing "first" and "y", nothing remains