Skip to content

Commit

Permalink
[section] Add __repr__ function to Section class
Browse files Browse the repository at this point in the history
  • Loading branch information
paulopaixaoamaral committed May 22, 2020
1 parent 364f797 commit 116e924
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions py_pdf_parser/sectioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def __len__(self):
"""
return len(self.elements)

def __repr__(self):
return (
f"<Section name: '{self.name}', unique_name: '{self.unique_name}', "
f"number of elements: {len(self)}>"
)


class Sectioning:
"""
Expand Down
34 changes: 34 additions & 0 deletions tests/test_sectioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ def test_len(self):
pdf_elem_2.ignore()
self.assertEqual(len(section), 2)

def test_repr(self):
elem_1 = FakePDFMinerTextElement()
elem_2 = FakePDFMinerTextElement()
document = create_pdf_document([elem_1, elem_2])

pdf_elem_1 = self.extract_element_from_list(elem_1, document._element_list)
pdf_elem_2 = self.extract_element_from_list(elem_2, document._element_list)

section = create_section(
document,
name="fake_section",
unique_name="fake_section_0",
start_element=pdf_elem_1,
end_element=pdf_elem_2,
)

self.assertEqual(
repr(section),
(
"<Section name: 'fake_section', unique_name: 'fake_section_0', "
"number of elements: 2>"
),
)

# Ignoring an element should affect the number of elements of the section.
pdf_elem_2.ignore()
self.assertEqual(
repr(section),
(
"<Section name: 'fake_section', unique_name: 'fake_section_0', "
"number of elements: 1>"
),
)


class TestSectioning(BaseTestCase):
def test_create_section(self):
Expand Down

0 comments on commit 116e924

Please sign in to comment.