Skip to content

Commit cbd587e

Browse files
author
Steve Canny
committed
reorg: extract tests.text.test_run module
1 parent 5b924f4 commit cbd587e

File tree

3 files changed

+368
-351
lines changed

3 files changed

+368
-351
lines changed

tests/test_text.py

Lines changed: 1 addition & 351 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11-
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK, WD_UNDERLINE
11+
from docx.enum.text import WD_ALIGN_PARAGRAPH
1212
from docx.oxml.ns import qn
1313
from docx.oxml.text.paragraph import CT_P
1414
from docx.oxml.text.run import CT_R
15-
from docx.parts.document import InlineShapes
16-
from docx.shape import InlineShape
1715
from docx.text import Paragraph, Run
1816

1917
import pytest
@@ -230,351 +228,3 @@ def runs_(self, request):
230228
run_ = instance_mock(request, Run, name='run_')
231229
run_2_ = instance_mock(request, Run, name='run_2_')
232230
return run_, run_2_
233-
234-
235-
class DescribeRun(object):
236-
237-
def it_knows_its_bool_prop_states(self, bool_prop_get_fixture):
238-
run, prop_name, expected_state = bool_prop_get_fixture
239-
assert getattr(run, prop_name) == expected_state
240-
241-
def it_can_change_its_bool_prop_settings(self, bool_prop_set_fixture):
242-
run, prop_name, value, expected_xml = bool_prop_set_fixture
243-
setattr(run, prop_name, value)
244-
assert run._r.xml == expected_xml
245-
246-
def it_knows_its_character_style(self, style_get_fixture):
247-
run, expected_style = style_get_fixture
248-
assert run.style == expected_style
249-
250-
def it_can_change_its_character_style(self, style_set_fixture):
251-
run, style, expected_xml = style_set_fixture
252-
run.style = style
253-
assert run._r.xml == expected_xml
254-
255-
def it_knows_its_underline_type(self, underline_get_fixture):
256-
run, expected_value = underline_get_fixture
257-
assert run.underline is expected_value
258-
259-
def it_can_change_its_underline_type(self, underline_set_fixture):
260-
run, underline, expected_xml = underline_set_fixture
261-
run.underline = underline
262-
assert run._r.xml == expected_xml
263-
264-
def it_raises_on_assign_invalid_underline_type(
265-
self, underline_raise_fixture):
266-
run, underline = underline_raise_fixture
267-
with pytest.raises(ValueError):
268-
run.underline = underline
269-
270-
def it_can_add_text(self, add_text_fixture):
271-
run, text_str, expected_xml, Text_ = add_text_fixture
272-
_text = run.add_text(text_str)
273-
assert run._r.xml == expected_xml
274-
assert _text is Text_.return_value
275-
276-
def it_can_add_a_break(self, add_break_fixture):
277-
run, break_type, expected_xml = add_break_fixture
278-
run.add_break(break_type)
279-
assert run._r.xml == expected_xml
280-
281-
def it_can_add_a_tab(self, add_tab_fixture):
282-
run, expected_xml = add_tab_fixture
283-
run.add_tab()
284-
assert run._r.xml == expected_xml
285-
286-
def it_can_add_a_picture(self, add_picture_fixture):
287-
(run, image_descriptor_, width, height, inline_shapes_,
288-
expected_width, expected_height, picture_) = add_picture_fixture
289-
picture = run.add_picture(image_descriptor_, width, height)
290-
inline_shapes_.add_picture.assert_called_once_with(
291-
image_descriptor_, run
292-
)
293-
assert picture is picture_
294-
assert picture.width == expected_width
295-
assert picture.height == expected_height
296-
297-
def it_can_remove_its_content_but_keep_formatting(self, clear_fixture):
298-
run, expected_xml = clear_fixture
299-
_run = run.clear()
300-
assert run._r.xml == expected_xml
301-
assert _run is run
302-
303-
def it_knows_the_text_it_contains(self, text_get_fixture):
304-
run, expected_text = text_get_fixture
305-
assert run.text == expected_text
306-
307-
def it_can_replace_the_text_it_contains(self, text_set_fixture):
308-
run, text, expected_xml = text_set_fixture
309-
run.text = text
310-
assert run._r.xml == expected_xml
311-
312-
# fixtures -------------------------------------------------------
313-
314-
@pytest.fixture(params=[
315-
(WD_BREAK.LINE, 'w:r/w:br'),
316-
(WD_BREAK.PAGE, 'w:r/w:br{w:type=page}'),
317-
(WD_BREAK.COLUMN, 'w:r/w:br{w:type=column}'),
318-
(WD_BREAK.LINE_CLEAR_LEFT,
319-
'w:r/w:br{w:type=textWrapping, w:clear=left}'),
320-
(WD_BREAK.LINE_CLEAR_RIGHT,
321-
'w:r/w:br{w:type=textWrapping, w:clear=right}'),
322-
(WD_BREAK.LINE_CLEAR_ALL,
323-
'w:r/w:br{w:type=textWrapping, w:clear=all}'),
324-
])
325-
def add_break_fixture(self, request):
326-
break_type, expected_cxml = request.param
327-
run = Run(element('w:r'), None)
328-
expected_xml = xml(expected_cxml)
329-
return run, break_type, expected_xml
330-
331-
@pytest.fixture(params=[
332-
(None, None, 200, 100),
333-
(1000, 500, 1000, 500),
334-
(2000, None, 2000, 1000),
335-
(None, 2000, 4000, 2000),
336-
])
337-
def add_picture_fixture(
338-
self, request, paragraph_, inline_shapes_, picture_):
339-
width, height, expected_width, expected_height = request.param
340-
paragraph_.part.inline_shapes = inline_shapes_
341-
run = Run(None, paragraph_)
342-
image_descriptor_ = 'image_descriptor_'
343-
picture_.width, picture_.height = 200, 100
344-
return (
345-
run, image_descriptor_, width, height, inline_shapes_,
346-
expected_width, expected_height, picture_
347-
)
348-
349-
@pytest.fixture(params=[
350-
('w:r/w:t"foo"', 'w:r/(w:t"foo", w:tab)'),
351-
])
352-
def add_tab_fixture(self, request):
353-
r_cxml, expected_cxml = request.param
354-
run = Run(element(r_cxml), None)
355-
expected_xml = xml(expected_cxml)
356-
return run, expected_xml
357-
358-
@pytest.fixture(params=[
359-
('w:r', 'foo', 'w:r/w:t"foo"'),
360-
('w:r/w:t"foo"', 'bar', 'w:r/(w:t"foo", w:t"bar")'),
361-
('w:r', 'fo ', 'w:r/w:t{xml:space=preserve}"fo "'),
362-
('w:r', 'f o', 'w:r/w:t"f o"'),
363-
])
364-
def add_text_fixture(self, request, Text_):
365-
r_cxml, text, expected_cxml = request.param
366-
run = Run(element(r_cxml), None)
367-
expected_xml = xml(expected_cxml)
368-
return run, text, expected_xml, Text_
369-
370-
@pytest.fixture(params=[
371-
('w:r/w:rPr', 'all_caps', None),
372-
('w:r/w:rPr/w:caps', 'all_caps', True),
373-
('w:r/w:rPr/w:caps{w:val=on}', 'all_caps', True),
374-
('w:r/w:rPr/w:caps{w:val=off}', 'all_caps', False),
375-
('w:r/w:rPr/w:b{w:val=1}', 'bold', True),
376-
('w:r/w:rPr/w:i{w:val=0}', 'italic', False),
377-
('w:r/w:rPr/w:cs{w:val=true}', 'complex_script', True),
378-
('w:r/w:rPr/w:bCs{w:val=false}', 'cs_bold', False),
379-
('w:r/w:rPr/w:iCs{w:val=on}', 'cs_italic', True),
380-
('w:r/w:rPr/w:dstrike{w:val=off}', 'double_strike', False),
381-
('w:r/w:rPr/w:emboss{w:val=1}', 'emboss', True),
382-
('w:r/w:rPr/w:vanish{w:val=0}', 'hidden', False),
383-
('w:r/w:rPr/w:i{w:val=true}', 'italic', True),
384-
('w:r/w:rPr/w:imprint{w:val=false}', 'imprint', False),
385-
('w:r/w:rPr/w:oMath{w:val=on}', 'math', True),
386-
('w:r/w:rPr/w:noProof{w:val=off}', 'no_proof', False),
387-
('w:r/w:rPr/w:outline{w:val=1}', 'outline', True),
388-
('w:r/w:rPr/w:rtl{w:val=0}', 'rtl', False),
389-
('w:r/w:rPr/w:shadow{w:val=true}', 'shadow', True),
390-
('w:r/w:rPr/w:smallCaps{w:val=false}', 'small_caps', False),
391-
('w:r/w:rPr/w:snapToGrid{w:val=on}', 'snap_to_grid', True),
392-
('w:r/w:rPr/w:specVanish{w:val=off}', 'spec_vanish', False),
393-
('w:r/w:rPr/w:strike{w:val=1}', 'strike', True),
394-
('w:r/w:rPr/w:webHidden{w:val=0}', 'web_hidden', False),
395-
])
396-
def bool_prop_get_fixture(self, request):
397-
r_cxml, bool_prop_name, expected_value = request.param
398-
run = Run(element(r_cxml), None)
399-
return run, bool_prop_name, expected_value
400-
401-
@pytest.fixture(params=[
402-
# nothing to True, False, and None ---------------------------
403-
('w:r', 'all_caps', True,
404-
'w:r/w:rPr/w:caps'),
405-
('w:r', 'bold', False,
406-
'w:r/w:rPr/w:b{w:val=0}'),
407-
('w:r', 'italic', None,
408-
'w:r/w:rPr'),
409-
# default to True, False, and None ---------------------------
410-
('w:r/w:rPr/w:cs', 'complex_script', True,
411-
'w:r/w:rPr/w:cs'),
412-
('w:r/w:rPr/w:bCs', 'cs_bold', False,
413-
'w:r/w:rPr/w:bCs{w:val=0}'),
414-
('w:r/w:rPr/w:iCs', 'cs_italic', None,
415-
'w:r/w:rPr'),
416-
# True to True, False, and None ------------------------------
417-
('w:r/w:rPr/w:dstrike{w:val=1}', 'double_strike', True,
418-
'w:r/w:rPr/w:dstrike'),
419-
('w:r/w:rPr/w:emboss{w:val=on}', 'emboss', False,
420-
'w:r/w:rPr/w:emboss{w:val=0}'),
421-
('w:r/w:rPr/w:vanish{w:val=1}', 'hidden', None,
422-
'w:r/w:rPr'),
423-
# False to True, False, and None -----------------------------
424-
('w:r/w:rPr/w:i{w:val=false}', 'italic', True,
425-
'w:r/w:rPr/w:i'),
426-
('w:r/w:rPr/w:imprint{w:val=0}', 'imprint', False,
427-
'w:r/w:rPr/w:imprint{w:val=0}'),
428-
('w:r/w:rPr/w:oMath{w:val=off}', 'math', None,
429-
'w:r/w:rPr'),
430-
# random mix -------------------------------------------------
431-
('w:r/w:rPr/w:noProof{w:val=1}', 'no_proof', False,
432-
'w:r/w:rPr/w:noProof{w:val=0}'),
433-
('w:r/w:rPr', 'outline', True,
434-
'w:r/w:rPr/w:outline'),
435-
('w:r/w:rPr/w:rtl{w:val=true}', 'rtl', False,
436-
'w:r/w:rPr/w:rtl{w:val=0}'),
437-
('w:r/w:rPr/w:shadow{w:val=on}', 'shadow', True,
438-
'w:r/w:rPr/w:shadow'),
439-
('w:r/w:rPr/w:smallCaps', 'small_caps', False,
440-
'w:r/w:rPr/w:smallCaps{w:val=0}'),
441-
('w:r/w:rPr/w:snapToGrid', 'snap_to_grid', True,
442-
'w:r/w:rPr/w:snapToGrid'),
443-
('w:r/w:rPr/w:specVanish', 'spec_vanish', None,
444-
'w:r/w:rPr'),
445-
('w:r/w:rPr/w:strike{w:val=foo}', 'strike', True,
446-
'w:r/w:rPr/w:strike'),
447-
('w:r/w:rPr/w:webHidden', 'web_hidden', False,
448-
'w:r/w:rPr/w:webHidden{w:val=0}'),
449-
])
450-
def bool_prop_set_fixture(self, request):
451-
initial_r_cxml, bool_prop_name, value, expected_cxml = request.param
452-
run = Run(element(initial_r_cxml), None)
453-
expected_xml = xml(expected_cxml)
454-
return run, bool_prop_name, value, expected_xml
455-
456-
@pytest.fixture(params=[
457-
('w:r', 'w:r'),
458-
('w:r/w:t"foo"', 'w:r'),
459-
('w:r/w:br', 'w:r'),
460-
('w:r/w:rPr', 'w:r/w:rPr'),
461-
('w:r/(w:rPr, w:t"foo")', 'w:r/w:rPr'),
462-
('w:r/(w:rPr/(w:b, w:i), w:t"foo", w:cr, w:t"bar")',
463-
'w:r/w:rPr/(w:b, w:i)'),
464-
])
465-
def clear_fixture(self, request):
466-
initial_r_cxml, expected_cxml = request.param
467-
run = Run(element(initial_r_cxml), None)
468-
expected_xml = xml(expected_cxml)
469-
return run, expected_xml
470-
471-
@pytest.fixture(params=[
472-
('w:r', None),
473-
('w:r/w:rPr/w:rStyle{w:val=Foobar}', 'Foobar'),
474-
])
475-
def style_get_fixture(self, request):
476-
r_cxml, expected_style = request.param
477-
run = Run(element(r_cxml), None)
478-
return run, expected_style
479-
480-
@pytest.fixture(params=[
481-
('w:r', None,
482-
'w:r/w:rPr'),
483-
('w:r', 'Foo',
484-
'w:r/w:rPr/w:rStyle{w:val=Foo}'),
485-
('w:r/w:rPr/w:rStyle{w:val=Foo}', None,
486-
'w:r/w:rPr'),
487-
('w:r/w:rPr/w:rStyle{w:val=Foo}', 'Bar',
488-
'w:r/w:rPr/w:rStyle{w:val=Bar}'),
489-
])
490-
def style_set_fixture(self, request):
491-
initial_r_cxml, new_style, expected_cxml = request.param
492-
run = Run(element(initial_r_cxml), None)
493-
expected_xml = xml(expected_cxml)
494-
return run, new_style, expected_xml
495-
496-
@pytest.fixture(params=[
497-
('w:r', ''),
498-
('w:r/w:t"foobar"', 'foobar'),
499-
('w:r/(w:t"abc", w:tab, w:t"def", w:cr)', 'abc\tdef\n'),
500-
('w:r/(w:br{w:type=page}, w:t"abc", w:t"def", w:tab)', '\nabcdef\t'),
501-
])
502-
def text_get_fixture(self, request):
503-
r_cxml, expected_text = request.param
504-
run = Run(element(r_cxml), None)
505-
return run, expected_text
506-
507-
@pytest.fixture(params=[
508-
('abc def', 'w:r/w:t"abc def"'),
509-
('abc\tdef', 'w:r/(w:t"abc", w:tab, w:t"def")'),
510-
('abc\ndef', 'w:r/(w:t"abc", w:br, w:t"def")'),
511-
('abc\rdef', 'w:r/(w:t"abc", w:br, w:t"def")'),
512-
])
513-
def text_set_fixture(self, request):
514-
new_text, expected_cxml = request.param
515-
initial_r_cxml = 'w:r/w:t"should get deleted"'
516-
run = Run(element(initial_r_cxml), None)
517-
expected_xml = xml(expected_cxml)
518-
return run, new_text, expected_xml
519-
520-
@pytest.fixture(params=[
521-
('w:r', None),
522-
('w:r/w:rPr/w:u', None),
523-
('w:r/w:rPr/w:u{w:val=single}', True),
524-
('w:r/w:rPr/w:u{w:val=none}', False),
525-
('w:r/w:rPr/w:u{w:val=double}', WD_UNDERLINE.DOUBLE),
526-
('w:r/w:rPr/w:u{w:val=wave}', WD_UNDERLINE.WAVY),
527-
])
528-
def underline_get_fixture(self, request):
529-
r_cxml, expected_underline = request.param
530-
run = Run(element(r_cxml), None)
531-
return run, expected_underline
532-
533-
@pytest.fixture(params=[
534-
('w:r', True, 'w:r/w:rPr/w:u{w:val=single}'),
535-
('w:r', False, 'w:r/w:rPr/w:u{w:val=none}'),
536-
('w:r', None, 'w:r/w:rPr'),
537-
('w:r', WD_UNDERLINE.SINGLE, 'w:r/w:rPr/w:u{w:val=single}'),
538-
('w:r', WD_UNDERLINE.THICK, 'w:r/w:rPr/w:u{w:val=thick}'),
539-
('w:r/w:rPr/w:u{w:val=single}', True,
540-
'w:r/w:rPr/w:u{w:val=single}'),
541-
('w:r/w:rPr/w:u{w:val=single}', False,
542-
'w:r/w:rPr/w:u{w:val=none}'),
543-
('w:r/w:rPr/w:u{w:val=single}', None,
544-
'w:r/w:rPr'),
545-
('w:r/w:rPr/w:u{w:val=single}', WD_UNDERLINE.SINGLE,
546-
'w:r/w:rPr/w:u{w:val=single}'),
547-
('w:r/w:rPr/w:u{w:val=single}', WD_UNDERLINE.DOTTED,
548-
'w:r/w:rPr/w:u{w:val=dotted}'),
549-
])
550-
def underline_set_fixture(self, request):
551-
initial_r_cxml, new_underline, expected_cxml = request.param
552-
run = Run(element(initial_r_cxml), None)
553-
expected_xml = xml(expected_cxml)
554-
return run, new_underline, expected_xml
555-
556-
@pytest.fixture(params=['foobar', 42, 'single'])
557-
def underline_raise_fixture(self, request):
558-
invalid_underline_setting = request.param
559-
run = Run(element('w:r/w:rPr'), None)
560-
return run, invalid_underline_setting
561-
562-
# fixture components ---------------------------------------------
563-
564-
@pytest.fixture
565-
def inline_shapes_(self, request, picture_):
566-
inline_shapes_ = instance_mock(request, InlineShapes)
567-
inline_shapes_.add_picture.return_value = picture_
568-
return inline_shapes_
569-
570-
@pytest.fixture
571-
def paragraph_(self, request):
572-
return instance_mock(request, Paragraph)
573-
574-
@pytest.fixture
575-
def picture_(self, request):
576-
return instance_mock(request, InlineShape)
577-
578-
@pytest.fixture
579-
def Text_(self, request):
580-
return class_mock(request, 'docx.text.Text')

tests/text/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)