|
| 1 | +"""Test suite for the docx.oxml.text.hyperlink module.""" |
| 2 | + |
| 3 | +from typing import cast |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from docx.oxml.text.hyperlink import CT_Hyperlink |
| 8 | +from docx.oxml.text.run import CT_R |
| 9 | + |
| 10 | +from ...unitutil.cxml import element |
| 11 | + |
| 12 | + |
| 13 | +class DescribeCT_Hyperlink: |
| 14 | + """Unit-test suite for the CT_Hyperlink (<w:hyperlink>) element.""" |
| 15 | + |
| 16 | + def it_has_a_relationship_that_contains_the_hyperlink_address(self): |
| 17 | + cxml = 'w:hyperlink{r:id=rId6}/w:r/w:t"post"' |
| 18 | + hyperlink = cast(CT_Hyperlink, element(cxml)) |
| 19 | + |
| 20 | + rId = hyperlink.rId |
| 21 | + |
| 22 | + assert rId == "rId6" |
| 23 | + |
| 24 | + @pytest.mark.parametrize( |
| 25 | + ("cxml", "expected_value"), |
| 26 | + [ |
| 27 | + # -- default (when omitted) is True, somewhat surprisingly -- |
| 28 | + ("w:hyperlink{r:id=rId6}", True), |
| 29 | + ("w:hyperlink{r:id=rId6,w:history=0}", False), |
| 30 | + ("w:hyperlink{r:id=rId6,w:history=1}", True), |
| 31 | + ], |
| 32 | + ) |
| 33 | + def it_knows_whether_it_has_been_clicked_on_aka_visited( |
| 34 | + self, cxml: str, expected_value: bool |
| 35 | + ): |
| 36 | + hyperlink = cast(CT_Hyperlink, element(cxml)) |
| 37 | + assert hyperlink.history is expected_value |
| 38 | + |
| 39 | + def it_has_zero_or_more_runs_containing_the_hyperlink_text(self): |
| 40 | + cxml = 'w:hyperlink{r:id=rId6,w:history=1}/(w:r/w:t"blog",w:r/w:t" post")' |
| 41 | + hyperlink = cast(CT_Hyperlink, element(cxml)) |
| 42 | + |
| 43 | + rs = hyperlink.r_lst |
| 44 | + |
| 45 | + assert [type(r) for r in rs] == [CT_R, CT_R] |
| 46 | + assert rs[0].text == "blog" |
| 47 | + assert rs[1].text == " post" |
0 commit comments