Skip to content

Commit 668f0ed

Browse files
author
Steve Canny
committed
test: refactor tests.test_text to use cxml
1 parent 4321a40 commit 668f0ed

File tree

6 files changed

+322
-392
lines changed

6 files changed

+322
-392
lines changed

docx/enum/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ class EnumerationBase(object):
159159
__ms_name__ = ''
160160

161161
@classmethod
162-
def is_valid_setting(cls, value):
162+
def validate(cls, value):
163163
"""
164-
Return |True| if *value* is an assignable value, |False| if it is
165-
a return value-only member or not a member value.
164+
Raise |ValueError| if *value* is not an assignable value.
166165
"""
167-
return value in cls._valid_settings
166+
if value not in cls._valid_settings:
167+
raise ValueError(
168+
"%s not a member of %s enumeration" % (value, cls.__name__)
169+
)
168170

169171

170172
Enumeration = MetaEnumeration(

docx/oxml/simpletypes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from __future__ import absolute_import, print_function
1010

11+
from ..exceptions import InvalidXmlError
1112
from ..shared import Emu, Twips
1213

1314

@@ -95,6 +96,11 @@ class XsdBoolean(BaseSimpleType):
9596

9697
@classmethod
9798
def convert_from_xml(cls, str_value):
99+
if str_value not in ('1', '0', 'true', 'false'):
100+
raise InvalidXmlError(
101+
"value must be one of '1', '0', 'true' or 'false', got '%s'"
102+
% str_value
103+
)
98104
return str_value in ('1', 'true')
99105

100106
@classmethod
@@ -216,6 +222,11 @@ class ST_OnOff(XsdBoolean):
216222

217223
@classmethod
218224
def convert_from_xml(cls, str_value):
225+
if str_value not in ('1', '0', 'true', 'false', 'on', 'off'):
226+
raise InvalidXmlError(
227+
"value must be one of '1', '0', 'true', 'false', 'on', or 'o"
228+
"ff', got '%s'" % str_value
229+
)
219230
return str_value in ('1', 'true', 'on')
220231

221232

docx/oxml/text.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ def text(self):
237237
text = ''
238238
for child in self:
239239
if child.tag == qn('w:t'):
240-
text += child.text
240+
t_text = child.text
241+
text += t_text if t_text is not None else ''
241242
elif child.tag == qn('w:tab'):
242243
text += '\t'
243244
elif child.tag in (qn('w:br'), qn('w:cr')):

docx/text.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9-
from docx.enum.text import WD_BREAK, WD_UNDERLINE
9+
from docx.enum.text import WD_BREAK
1010

1111

1212
def boolproperty(f):
@@ -39,13 +39,17 @@ def getter(obj):
3939
return prop_value.val
4040

4141
def setter(obj, value):
42+
if value not in (True, False, None):
43+
raise ValueError(
44+
"assigned value must be True, False, or None, got '%s'"
45+
% value
46+
)
4247
r, attr_name = obj._r, f(obj)
4348
rPr = r.get_or_add_rPr()
4449
_remove_prop(rPr, attr_name)
4550
if value is not None:
4651
elm = _add_prop(rPr, attr_name)
47-
if bool(value) is False:
48-
elm.val = False
52+
elm.val = value
4953

5054
return property(getter, setter, doc=f.__doc__)
5155

@@ -432,9 +436,6 @@ def underline(self):
432436

433437
@underline.setter
434438
def underline(self, value):
435-
if not WD_UNDERLINE.is_valid_setting(value):
436-
tmpl = "'%s' is not a valid setting for Run.underline"
437-
raise ValueError(tmpl % value)
438439
self._r.underline = value
439440

440441
@boolproperty

tests/test_enum.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ def it_provides_the_enumeration_value_for_each_named_member(self):
6767
assert FOOBAR.READ_ONLY == -2
6868

6969
def it_knows_if_a_setting_is_valid(self):
70-
assert FOOBAR.is_valid_setting(None)
71-
assert FOOBAR.is_valid_setting(FOOBAR.READ_WRITE)
72-
assert not FOOBAR.is_valid_setting('foobar')
73-
assert not FOOBAR.is_valid_setting(FOOBAR.READ_ONLY)
70+
FOOBAR.validate(None)
71+
FOOBAR.validate(FOOBAR.READ_WRITE)
72+
with pytest.raises(ValueError):
73+
FOOBAR.validate('foobar')
74+
with pytest.raises(ValueError):
75+
FOOBAR.validate(FOOBAR.READ_ONLY)
7476

7577
def it_can_be_referred_to_by_a_convenience_alias_if_defined(self):
7678
assert BARFOO is FOOBAR # noqa

0 commit comments

Comments
 (0)