Skip to content

Commit

Permalink
Fix for #27: support negative indexing of sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Nov 2, 2014
1 parent e128c16 commit 4843475
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions holmium/core/pageobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ def __len__(self):
return len(self.__getelements__())

def __getitem__(self, idx):
if idx < 0:
idx = len(self) + idx
if idx < 0 or idx >= len(self):
raise IndexError("Sections index (%d) out of range" % idx)
for i, _ in enumerate(self):
Expand Down
11 changes: 10 additions & 1 deletion tests/sectionobject_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,13 @@ def test_sections_list_behavior(self):
self.assertEqual("element 4", po.sections[0].tokens[1].text)
self.assertEqual("element 3", po.sections[1].tokens[1].text)
self.assertEqual("element 6", po.sections[2].tokens[1].text)

self.assertEqual("element 6", po.sections[-1].tokens[1].text)
self.assertEqual("element 3", po.sections[-2].tokens[1].text)
self.assertRaises(
IndexError,
lambda: po.sections[-4]
)
self.assertRaises(
IndexError,
lambda: po.sections[3]
)

0 comments on commit 4843475

Please sign in to comment.