66
77from __future__ import absolute_import , print_function , unicode_literals
88
9- from behave import given , then
9+ from behave import given , then , when
1010
1111from docx import Document
12+ from docx .enum .section import WD_ORIENT , WD_SECTION
1213from docx .parts .document import Sections
1314from docx .section import Section
1415
@@ -28,6 +29,29 @@ def given_a_section_collection(context):
2829 context .sections = document .sections
2930
3031
32+ @given ('a single-section document having portrait layout' )
33+ def given_a_single_section_document_having_portrait_layout (context ):
34+ context .document = Document (test_docx ('doc-add-section' ))
35+ section = context .document .sections [- 1 ]
36+ context .original_dimensions = (section .page_width , section .page_height )
37+
38+
39+ # when ====================================================
40+
41+ @when ('I add an even-page section to the document' )
42+ def when_I_add_an_even_page_section_to_the_document (context ):
43+ context .section = context .document .add_section (WD_SECTION .EVEN_PAGE )
44+
45+
46+ @when ('I change the new section layout to landscape' )
47+ def when_I_change_the_new_section_layout_to_landscape (context ):
48+ new_height , new_width = context .original_dimensions
49+ section = context .section
50+ section .orientation = WD_ORIENT .LANDSCAPE
51+ section .page_width = new_width
52+ section .page_height = new_height
53+
54+
3155# then ====================================================
3256
3357@then ('I can access a section by index' )
@@ -55,9 +79,32 @@ def then_I_can_iterate_over_the_sections(context):
5579 assert actual_count == 3
5680
5781
82+ @then ('the document has two sections' )
83+ def then_the_document_has_two_sections (context ):
84+ assert len (context .document .sections ) == 2
85+
86+
87+ @then ('the first section is portrait' )
88+ def then_the_first_section_is_portrait (context ):
89+ first_section = context .document .sections [0 ]
90+ expected_width , expected_height = context .original_dimensions
91+ assert first_section .orientation == WD_ORIENT .PORTRAIT
92+ assert first_section .page_width == expected_width
93+ assert first_section .page_height == expected_height
94+
95+
5896@then ('the length of the section collection is 3' )
5997def then_the_length_of_the_section_collection_is_3 (context ):
6098 sections = context .document .sections
6199 assert len (sections ) == 3 , (
62100 'expected len(sections) of 2, got %s' % len (sections )
63101 )
102+
103+
104+ @then ('the second section is landscape' )
105+ def then_the_second_section_is_landscape (context ):
106+ new_section = context .document .sections [- 1 ]
107+ expected_height , expected_width = context .original_dimensions
108+ assert new_section .orientation == WD_ORIENT .LANDSCAPE
109+ assert new_section .page_width == expected_width
110+ assert new_section .page_height == expected_height
0 commit comments