|
8 | 8 |
|
9 | 9 | from docx import Document |
10 | 10 | from docx.enum.style import WD_STYLE_TYPE |
| 11 | +from docx.styles.latent import _LatentStyle, LatentStyles |
11 | 12 | from docx.styles.styles import Styles |
12 | 13 | from docx.styles.style import BaseStyle |
13 | 14 | from docx.text.paragraph import ParagraphFormat |
@@ -56,6 +57,12 @@ def given_a_document_having_no_styles_part(context): |
56 | 57 | context.document = Document(docx_path) |
57 | 58 |
|
58 | 59 |
|
| 60 | +@given('a latent style collection') |
| 61 | +def given_a_latent_style_collection(context): |
| 62 | + document = Document(test_docx('sty-known-styles')) |
| 63 | + context.latent_styles = document.styles.latent_styles |
| 64 | + |
| 65 | + |
59 | 66 | @given('a style based on {base_style}') |
60 | 67 | def given_a_style_based_on_setting(context, base_style): |
61 | 68 | style_name = { |
@@ -141,6 +148,12 @@ def given_a_style_of_type(context, style_type): |
141 | 148 | context.style = document.styles[name] |
142 | 149 |
|
143 | 150 |
|
| 151 | +@given('the style collection of a document') |
| 152 | +def given_the_style_collection_of_a_document(context): |
| 153 | + document = Document(test_docx('sty-known-styles')) |
| 154 | + context.styles = document.styles |
| 155 | + |
| 156 | + |
144 | 157 | # when ===================================================== |
145 | 158 |
|
146 | 159 | @when('I assign a new name to the style') |
@@ -209,6 +222,13 @@ def when_I_delete_a_style(context): |
209 | 222 |
|
210 | 223 | # then ===================================================== |
211 | 224 |
|
| 225 | +@then('I can access a latent style by name') |
| 226 | +def then_I_can_access_a_latent_style_by_name(context): |
| 227 | + latent_styles = context.latent_styles |
| 228 | + latent_style = latent_styles['Colorful Shading'] |
| 229 | + assert isinstance(latent_style, _LatentStyle) |
| 230 | + |
| 231 | + |
212 | 232 | @then('I can access a style by its UI name') |
213 | 233 | def then_I_can_access_a_style_by_its_UI_name(context): |
214 | 234 | styles = context.document.styles |
@@ -237,6 +257,18 @@ def then_I_can_iterate_over_its_styles(context): |
237 | 257 | assert all(isinstance(s, BaseStyle) for s in styles) |
238 | 258 |
|
239 | 259 |
|
| 260 | +@then('I can iterate over the latent styles') |
| 261 | +def then_I_can_iterate_over_the_latent_styles(context): |
| 262 | + latent_styles = [ls for ls in context.latent_styles] |
| 263 | + assert len(latent_styles) == 137 |
| 264 | + assert all(isinstance(ls, _LatentStyle) for ls in latent_styles) |
| 265 | + |
| 266 | + |
| 267 | +@then('len(latent_styles) is 137') |
| 268 | +def then_len_latent_styles_is_137(context): |
| 269 | + assert len(context.latent_styles) == 137 |
| 270 | + |
| 271 | + |
240 | 272 | @then('len(styles) is {style_count_str}') |
241 | 273 | def then_len_styles_is_style_count(context, style_count_str): |
242 | 274 | assert len(context.document.styles) == int(style_count_str) |
@@ -340,6 +372,14 @@ def then_style_unhide_when_used_is_value(context, value): |
340 | 372 | assert style.unhide_when_used is expected_value |
341 | 373 |
|
342 | 374 |
|
| 375 | +@then('styles.latent_styles is the LatentStyles object for the document') |
| 376 | +def then_styles_latent_styles_is_the_LatentStyles_object(context): |
| 377 | + styles = context.styles |
| 378 | + context.latent_styles = latent_styles = styles.latent_styles |
| 379 | + assert isinstance(latent_styles, LatentStyles) |
| 380 | + assert latent_styles.element is styles.element.latentStyles |
| 381 | + |
| 382 | + |
343 | 383 | @then('styles[\'{name}\'] is a style') |
344 | 384 | def then_styles_name_is_a_style(context, name): |
345 | 385 | styles = context.document.styles |
|
0 commit comments