From d7a3342471eee6daf07455798b575568d0cfd78c Mon Sep 17 00:00:00 2001 From: Anaaa Date: Tue, 27 Aug 2024 17:18:35 +0200 Subject: [PATCH] Fix replace_sublist when empty sublist is passed --- src/listwiz/replace.py | 2 ++ tests/test_replace.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/listwiz/replace.py b/src/listwiz/replace.py index 182f85f..51ab4ab 100644 --- a/src/listwiz/replace.py +++ b/src/listwiz/replace.py @@ -37,6 +37,8 @@ def replace_sublist(l, sublist, replacement): """ len_sublist = len(sublist) + if len_sublist == 0: + raise ValueError("The sublist cannot be empty. Please provide at least one element.") result = l[:] # copy the input list i = 0 diff --git a/tests/test_replace.py b/tests/test_replace.py index 66f2bb1..7db1e52 100644 --- a/tests/test_replace.py +++ b/tests/test_replace.py @@ -22,5 +22,5 @@ def test_replace_sublist_overlap(): def test_replace_sublist_empty(): - # This is a stub for testing the bug with empty sublists. - pass + with pytest.raises(ValueError, match="The sublist cannot be empty. Please provide at least one element."): + lwr.replace_sublist([1, 2, 3], [], [5, 6])