X Tutup
Skip to content

Commit ab625af

Browse files
author
Steve Canny
committed
sect: add Section.start_type.setter
Add key check to XmlEnumeration.to_xml() that raises ValueError on value not a member of enumeration.
1 parent 4c556a3 commit ab625af

File tree

6 files changed

+49
-9
lines changed

6 files changed

+49
-9
lines changed

docx/enum/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ def to_xml(cls, enum_val):
197197
"""
198198
Return the XML value of the enumeration value *enum_val*.
199199
"""
200+
if enum_val not in cls._member_to_xml:
201+
raise ValueError(
202+
"value '%s' not in enumeration %s" % (enum_val, cls.__name__)
203+
)
200204
return cls._member_to_xml[enum_val]
201205

202206

docx/oxml/section.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def start_type(self):
2626
return WD_SECTION_START.NEW_PAGE
2727
return type.val
2828

29+
@start_type.setter
30+
def start_type(self, value):
31+
if value is None or value is WD_SECTION_START.NEW_PAGE:
32+
self._remove_type()
33+
return
34+
type = self.get_or_add_type()
35+
type.val = value
36+
2937

3038
class CT_SectType(BaseOxmlElement):
3139
"""

docx/section.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ def start_type(self):
2323
if the section should begin on the next odd page.
2424
"""
2525
return self._sectPr.start_type
26+
27+
@start_type.setter
28+
def start_type(self, value):
29+
self._sectPr.start_type = value

features/sct-section-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Feature: Access and change section properties
1717
| ODD_PAGE |
1818

1919

20-
@wip
2120
Scenario Outline: Set section start type
2221
Given a section having start type <initial-start-type>
2322
When I set the section start type to <new-start-type>

tests/test_enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class DescribeXmlEnumeration(object):
9090
def it_knows_the_XML_value_for_each_of_its_xml_members(self):
9191
assert XMLFOO.to_xml(XMLFOO.XML_RW) == 'attrVal'
9292
assert XMLFOO.to_xml(42) == 'attrVal'
93-
with pytest.raises(KeyError):
93+
with pytest.raises(ValueError):
9494
XMLFOO.to_xml(XMLFOO.RO)
9595

9696
def it_can_map_each_of_its_xml_members_from_the_XML_value(self):

tests/test_section.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616

1717
class DescribeSection(object):
1818

19-
def it_knows_its_start_type(self, start_type_fixture):
20-
section, expected_start_type = start_type_fixture
19+
def it_knows_its_start_type(self, start_type_get_fixture):
20+
section, expected_start_type = start_type_get_fixture
2121
assert section.start_type is expected_start_type
2222

23+
def it_can_change_its_start_type(self, start_type_set_fixture):
24+
section, new_start_type, expected_xml = start_type_set_fixture
25+
section.start_type = new_start_type
26+
assert section._sectPr.xml == expected_xml
27+
2328
# fixtures -------------------------------------------------------
2429

2530
@pytest.fixture(params=[
@@ -30,11 +35,31 @@ def it_knows_its_start_type(self, start_type_fixture):
3035
('evenPage', WD_SECTION.EVEN_PAGE),
3136
('nextColumn', WD_SECTION.NEW_COLUMN),
3237
])
33-
def start_type_fixture(self, request):
38+
def start_type_get_fixture(self, request):
3439
type_val, expected_start_type = request.param
35-
sectPr_bldr = a_sectPr().with_nsdecls()
36-
if type_val is not None:
37-
sectPr_bldr.with_child(a_type().with_val(type_val))
38-
sectPr = sectPr_bldr.element
40+
sectPr = self.sectPr_bldr(type_val).element
3941
section = Section(sectPr)
4042
return section, expected_start_type
43+
44+
@pytest.fixture(params=[
45+
('oddPage', WD_SECTION.EVEN_PAGE, 'evenPage'),
46+
('nextPage', None, None),
47+
('continuous', WD_SECTION.NEW_PAGE, None),
48+
(None, WD_SECTION.NEW_COLUMN, 'nextColumn'),
49+
])
50+
def start_type_set_fixture(self, request):
51+
initial_type_val, new_type, expected_type_val = request.param
52+
sectPr = self.sectPr_bldr(initial_type_val).element
53+
section = Section(sectPr)
54+
expected_xml = self.sectPr_bldr(expected_type_val).xml()
55+
return section, new_type, expected_xml
56+
57+
# fixture components ---------------------------------------------
58+
59+
def sectPr_bldr(self, start_type=None):
60+
sectPr_bldr = a_sectPr().with_nsdecls()
61+
if start_type is not None:
62+
sectPr_bldr.with_child(
63+
a_type().with_val(start_type)
64+
)
65+
return sectPr_bldr

0 commit comments

Comments
 (0)
X Tutup