|
11 | 11 | import pytest |
12 | 12 |
|
13 | 13 | from docx.enum.style import WD_STYLE_TYPE |
| 14 | +from docx.oxml.parts.styles import CT_Style, CT_Styles |
14 | 15 | from docx.styles.style import BaseStyle |
15 | 16 | from docx.styles.styles import Styles |
16 | 17 |
|
@@ -54,6 +55,23 @@ def it_raises_on_style_not_found(self, get_raises_fixture): |
54 | 55 | with pytest.raises(KeyError): |
55 | 56 | styles[key] |
56 | 57 |
|
| 58 | + def it_can_add_a_new_style(self, add_fixture): |
| 59 | + styles, name, style_type, builtin = add_fixture[:4] |
| 60 | + name_, StyleFactory_, style_elm_, style_ = add_fixture[4:] |
| 61 | + |
| 62 | + style = styles.add_style(name, style_type, builtin) |
| 63 | + |
| 64 | + styles._element.add_style_of_type.assert_called_once_with( |
| 65 | + name_, style_type, builtin |
| 66 | + ) |
| 67 | + StyleFactory_.assert_called_once_with(style_elm_) |
| 68 | + assert style is style_ |
| 69 | + |
| 70 | + def it_raises_when_style_name_already_used(self, add_raises_fixture): |
| 71 | + styles, name = add_raises_fixture |
| 72 | + with pytest.raises(ValueError): |
| 73 | + styles.add_style(name, None) |
| 74 | + |
57 | 75 | def it_can_get_the_default_style_for_a_type(self, default_fixture): |
58 | 76 | styles, style_type, StyleFactory_ = default_fixture[:3] |
59 | 77 | StyleFactory_calls, style_ = default_fixture[3:] |
@@ -119,6 +137,28 @@ def it_raises_on_style_type_mismatch(self, id_style_raises_fixture): |
119 | 137 |
|
120 | 138 | # fixture -------------------------------------------------------- |
121 | 139 |
|
| 140 | + @pytest.fixture(params=[ |
| 141 | + ('Foo Bar', 'Foo Bar', WD_STYLE_TYPE.CHARACTER, False), |
| 142 | + ('Heading 1', 'heading 1', WD_STYLE_TYPE.PARAGRAPH, True), |
| 143 | + ]) |
| 144 | + def add_fixture(self, request, styles_elm_, _getitem_, style_elm_, |
| 145 | + StyleFactory_, style_): |
| 146 | + name, name_, style_type, builtin = request.param |
| 147 | + styles = Styles(styles_elm_) |
| 148 | + _getitem_.return_value = None |
| 149 | + styles_elm_.add_style_of_type.return_value = style_elm_ |
| 150 | + StyleFactory_.return_value = style_ |
| 151 | + return ( |
| 152 | + styles, name, style_type, builtin, name_, StyleFactory_, |
| 153 | + style_elm_, style_ |
| 154 | + ) |
| 155 | + |
| 156 | + @pytest.fixture |
| 157 | + def add_raises_fixture(self, _getitem_): |
| 158 | + styles = Styles(element('w:styles/w:style/w:name{w:val=heading 1}')) |
| 159 | + name = 'Heading 1' |
| 160 | + return styles, name |
| 161 | + |
122 | 162 | @pytest.fixture(params=[ |
123 | 163 | ('w:styles', |
124 | 164 | False, WD_STYLE_TYPE.CHARACTER), |
@@ -319,3 +359,11 @@ def style_(self, request): |
319 | 359 | @pytest.fixture |
320 | 360 | def StyleFactory_(self, request): |
321 | 361 | return function_mock(request, 'docx.styles.styles.StyleFactory') |
| 362 | + |
| 363 | + @pytest.fixture |
| 364 | + def style_elm_(self, request): |
| 365 | + return instance_mock(request, CT_Style) |
| 366 | + |
| 367 | + @pytest.fixture |
| 368 | + def styles_elm_(self, request): |
| 369 | + return instance_mock(request, CT_Styles) |
0 commit comments