From eb3ede7ccaa819bc720656d52aad62f718be811b Mon Sep 17 00:00:00 2001 From: omrzk <193513049+omrzk@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:29:41 -0600 Subject: [PATCH] Fix negative FSArray row indices --- curtsies/formatstringarray.py | 2 +- tests/test_fmtstr.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/curtsies/formatstringarray.py b/curtsies/formatstringarray.py index 24c906a..8b29ee6 100644 --- a/curtsies/formatstringarray.py +++ b/curtsies/formatstringarray.py @@ -80,7 +80,7 @@ def __getitem__( ) -> FmtStr | list[FmtStr]: if isinstance(slicetuple, int): if slicetuple < 0: - slicetuple = len(self.rows) - slicetuple + slicetuple = len(self.rows) + slicetuple if slicetuple < 0 or slicetuple >= len(self.rows): raise IndexError("out of bounds") return self.rows[slicetuple] diff --git a/tests/test_fmtstr.py b/tests/test_fmtstr.py index a568780..726bedc 100644 --- a/tests/test_fmtstr.py +++ b/tests/test_fmtstr.py @@ -550,6 +550,26 @@ def test_width_awareness(self) -> None: class TestFSArray(unittest.TestCase): + def test_negative_row_indices(self) -> None: + a = fsarray([red("first"), blue("middle"), green("last")]) + for index in range(-len(a), len(a)): + with self.subTest(index=index): + self.assertIs(a[index], a.rows[index]) + + a[2] = red("new") + self.assertEqual(a[-1], red("new")) + self.assertIs(a[-1], a[2]) + self.assertEqual(a[:-1], a.rows[:-1]) + self.assertEqual(a[-1:], a.rows[-1:]) + + def test_row_indices_out_of_bounds(self) -> None: + for rows in ([], ["a"], ["a", "b", "c"]): + a = fsarray(rows) + for index in (-len(a) - 1, len(a)): + with self.subTest(rows=rows, index=index): + with self.assertRaises(IndexError): + _ = a[index] + def test_no_hanging_space(self) -> None: a = FSArray(4, 2) self.assertEqual(len(a.rows[0]), 0)