Skip to content

Commit 900807f

Browse files
author
Steve Canny
committed
acpt: migrate scenario for Document.sections
* Migrate Document.sections access to doc-access-collections.feature * Migrate section access steps to steps/section.py * Revise doc-access-collections steps for uniformity
1 parent e5b84e7 commit 900807f

File tree

4 files changed

+56
-56
lines changed

4 files changed

+56
-56
lines changed

features/doc-access-collections.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ Feature: Access document collections
66

77
Scenario: Access the inline shapes collection of a document
88
Given a document having inline shapes
9-
Then I can access the inline shape collection of the document
9+
Then document.inline_shapes is an InlineShapes object
1010

1111

1212
Scenario: Access the paragraphs in the document body as a list
1313
Given a document containing three paragraphs
1414
Then document.paragraphs is a list containing three paragraphs
15+
16+
17+
Scenario: Access the section collection of a document
18+
Given a document having sections
19+
Then document.sections is a Sections object

features/doc-access-sections.feature

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
Feature: Access document sections
2-
In order to discover and apply section-level settings
2+
In order to operate on an individual section
33
As a developer using python-docx
4-
I need a way to access document sections
5-
6-
7-
Scenario: Access section collection of a document
8-
Given a document having three sections
9-
Then I can access the section collection of the document
10-
And the length of the section collection is 3
4+
I need access to each section in the section collection
115

126

137
Scenario: Access section in section collection
14-
Given a section collection
15-
Then I can iterate over the sections
8+
Given a section collection containing 3 sections
9+
Then len(sections) is 3
10+
And I can iterate over the sections
1611
And I can access a section by index

features/steps/document.py

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from docx import Document
1212
from docx.enum.section import WD_ORIENT, WD_SECTION
1313
from docx.parts.document import InlineShapes, Sections
14-
from docx.section import Section
1514
from docx.shared import Inches
1615
from docx.table import Table
1716

@@ -30,17 +29,11 @@ def given_a_document_having_inline_shapes(context):
3029
context.document = Document(test_docx('shp-inline-shape-access'))
3130

3231

33-
@given('a document having three sections')
34-
def given_a_document_having_three_sections(context):
32+
@given('a document having sections')
33+
def given_a_document_having_sections(context):
3534
context.document = Document(test_docx('doc-access-sections'))
3635

3736

38-
@given('a section collection')
39-
def given_a_section_collection(context):
40-
document = Document(test_docx('doc-access-sections'))
41-
context.sections = document.sections
42-
43-
4437
@given('a single-section document having portrait layout')
4538
def given_a_single_section_document_having_portrait_layout(context):
4639
context.document = Document(test_docx('doc-add-section'))
@@ -151,44 +144,26 @@ def when_I_change_the_new_section_layout_to_landscape(context):
151144

152145
# then ====================================================
153146

154-
@then('document.paragraphs is a list containing three paragraphs')
155-
def then_document_paragraphs_is_a_list_containing_three_paragraphs(context):
156-
document = context.document
157-
paragraphs = document.paragraphs
158-
assert isinstance(paragraphs, list)
159-
assert len(paragraphs) == 3
160-
161-
162-
@then('I can access a section by index')
163-
def then_I_can_access_a_section_by_index(context):
164-
sections = context.sections
165-
for idx in range(3):
166-
section = sections[idx]
167-
assert isinstance(section, Section)
168-
169-
170-
@then('I can access the inline shape collection of the document')
171-
def then_can_access_inline_shape_collection_of_document(context):
147+
@then('document.inline_shapes is an InlineShapes object')
148+
def then_document_inline_shapes_is_an_InlineShapes_object(context):
172149
document = context.document
173150
inline_shapes = document.inline_shapes
174151
assert isinstance(inline_shapes, InlineShapes)
175152

176153

177-
@then('I can access the section collection of the document')
178-
def then_I_can_access_the_section_collection_of_the_document(context):
154+
@then('document.sections is a Sections object')
155+
def then_document_sections_is_a_Sections_object(context):
179156
sections = context.document.sections
180157
msg = 'document.sections not instance of Sections'
181158
assert isinstance(sections, Sections), msg
182159

183160

184-
@then('I can iterate over the sections')
185-
def then_I_can_iterate_over_the_sections(context):
186-
sections = context.sections
187-
actual_count = 0
188-
for section in sections:
189-
actual_count += 1
190-
assert isinstance(section, Section)
191-
assert actual_count == 3
161+
@then('document.paragraphs is a list containing three paragraphs')
162+
def then_document_paragraphs_is_a_list_containing_three_paragraphs(context):
163+
document = context.document
164+
paragraphs = document.paragraphs
165+
assert isinstance(paragraphs, list)
166+
assert len(paragraphs) == 3
192167

193168

194169
@then('the document contains a 2 x 2 table')
@@ -232,14 +207,6 @@ def then_last_p_contains_heading_text(context):
232207
assert paragraph.text == text
233208

234209

235-
@then('the length of the section collection is 3')
236-
def then_the_length_of_the_section_collection_is_3(context):
237-
sections = context.document.sections
238-
assert len(sections) == 3, (
239-
'expected len(sections) of 2, got %s' % len(sections)
240-
)
241-
242-
243210
@then('the second section is landscape')
244211
def then_the_second_section_is_landscape(context):
245212
new_section = context.document.sections[-1]

features/steps/section.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010

1111
from docx import Document
1212
from docx.enum.section import WD_ORIENT, WD_SECTION
13+
from docx.section import Section
1314
from docx.shared import Inches
1415

1516
from helpers import test_docx
1617

1718

1819
# given ====================================================
1920

21+
@given('a section collection containing 3 sections')
22+
def given_a_section_collection_containing_3_sections(context):
23+
document = Document(test_docx('doc-access-sections'))
24+
context.sections = document.sections
25+
26+
2027
@given('a section having known page dimension')
2128
def given_a_section_having_known_page_dimension(context):
2229
document = Document(test_docx('sct-section-props'))
@@ -104,6 +111,32 @@ def when_I_set_the_section_start_type_to_start_type(context, start_type):
104111

105112
# then =====================================================
106113

114+
@then('I can access a section by index')
115+
def then_I_can_access_a_section_by_index(context):
116+
sections = context.sections
117+
for idx in range(3):
118+
section = sections[idx]
119+
assert isinstance(section, Section)
120+
121+
122+
@then('I can iterate over the sections')
123+
def then_I_can_iterate_over_the_sections(context):
124+
sections = context.sections
125+
actual_count = 0
126+
for section in sections:
127+
actual_count += 1
128+
assert isinstance(section, Section)
129+
assert actual_count == 3
130+
131+
132+
@then('len(sections) is 3')
133+
def then_len_sections_is_3(context):
134+
sections = context.sections
135+
assert len(sections) == 3, (
136+
'expected len(sections) of 3, got %s' % len(sections)
137+
)
138+
139+
107140
@then('the reported {margin_side} margin is {inches} inches')
108141
def then_the_reported_margin_is_inches(context, margin_side, inches):
109142
prop_name = {

0 commit comments

Comments
 (0)