22
33from __future__ import annotations
44
5- from typing import cast
5+ from typing import List , cast
66
77import pytest
88
9+ from docx import types as t
910from docx .enum .style import WD_STYLE_TYPE
1011from docx .enum .text import WD_BREAK , WD_UNDERLINE
1112from docx .oxml .text .run import CT_R
1213from docx .parts .document import DocumentPart
14+ from docx .parts .story import StoryPart
1315from docx .shape import InlineShape
1416from docx .text .font import Font
1517from docx .text .run import Run
1921
2022
2123class DescribeRun (object ):
24+ """Unit-test suite for `docx.text.run.Run`."""
25+
2226 def it_knows_its_bool_prop_states (self , bool_prop_get_fixture ):
2327 run , prop_name , expected_state = bool_prop_get_fixture
2428 assert getattr (run , prop_name ) == expected_state
@@ -45,6 +49,36 @@ def it_knows_whether_it_contains_a_page_break(
4549
4650 assert run .contains_page_break == expected_value
4751
52+ @pytest .mark .parametrize (
53+ ("r_cxml" , "expected" ),
54+ [
55+ # -- no content produces an empty iterator --
56+ ("w:r" , []),
57+ # -- contiguous text content is condensed into a single str --
58+ ('w:r/(w:t"foo",w:cr,w:t"bar")' , ["str" ]),
59+ # -- page-breaks are a form of inner-content --
60+ (
61+ 'w:r/(w:t"abc",w:br,w:lastRenderedPageBreak,w:noBreakHyphen,w:t"def")' ,
62+ ["str" , "RenderedPageBreak" , "str" ],
63+ ),
64+ # -- as are drawings --
65+ (
66+ 'w:r/(w:t"abc", w:lastRenderedPageBreak, w:drawing)' ,
67+ ["str" , "RenderedPageBreak" , "Drawing" ],
68+ ),
69+ ],
70+ )
71+ def it_can_iterate_its_inner_content_items (
72+ self , r_cxml : str , expected : List [str ], fake_parent : t .StoryChild
73+ ):
74+ r = cast (CT_R , element (r_cxml ))
75+ run = Run (r , fake_parent )
76+
77+ inner_content = run .iter_inner_content ()
78+
79+ actual = [type (item ).__name__ for item in inner_content ]
80+ assert actual == expected , f"expected: { expected } , got: { actual } "
81+
4882 def it_knows_its_character_style (self , style_get_fixture ):
4983 run , style_id_ , style_ = style_get_fixture
5084 style = run .style
@@ -244,6 +278,15 @@ def clear_fixture(self, request):
244278 expected_xml = xml (expected_cxml )
245279 return run , expected_xml
246280
281+ @pytest .fixture
282+ def fake_parent (self ) -> t .StoryChild :
283+ class StoryChild :
284+ @property
285+ def part (self ) -> StoryPart :
286+ raise NotImplementedError
287+
288+ return StoryChild ()
289+
247290 @pytest .fixture
248291 def font_fixture (self , Font_ , font_ ):
249292 run = Run (element ("w:r" ), None )
0 commit comments