Skip to content

Commit b8b7367

Browse files
author
Steve Canny
committed
acpt: add scenario for Styles.latent_styles
1 parent e7930e4 commit b8b7367

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

docx/styles/latent.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Latent style-related objects.
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..shared import ElementProxy
12+
13+
14+
class LatentStyles(ElementProxy):
15+
"""
16+
Provides access to the default behaviors for latent styles in this
17+
document and to the collection of |_LatentStyle| objects that define
18+
overrides of those defaults for a particular named latent style.
19+
"""
20+
21+
__slots__ = ()
22+
23+
24+
class _LatentStyle(ElementProxy):
25+
"""
26+
Proxy for an `w:lsdException` element, which specifies display behaviors
27+
for a built-in style when no definition for that style is stored yet in
28+
the `styles.xml` part. The values in this element override the defaults
29+
specified in the parent `w:latentStyles` element.
30+
"""
31+
32+
__slots__ = ()

features/steps/styles.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from docx import Document
1010
from docx.enum.style import WD_STYLE_TYPE
11+
from docx.styles.latent import _LatentStyle, LatentStyles
1112
from docx.styles.styles import Styles
1213
from docx.styles.style import BaseStyle
1314
from docx.text.paragraph import ParagraphFormat
@@ -56,6 +57,12 @@ def given_a_document_having_no_styles_part(context):
5657
context.document = Document(docx_path)
5758

5859

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+
5966
@given('a style based on {base_style}')
6067
def given_a_style_based_on_setting(context, base_style):
6168
style_name = {
@@ -141,6 +148,12 @@ def given_a_style_of_type(context, style_type):
141148
context.style = document.styles[name]
142149

143150

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+
144157
# when =====================================================
145158

146159
@when('I assign a new name to the style')
@@ -209,6 +222,13 @@ def when_I_delete_a_style(context):
209222

210223
# then =====================================================
211224

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+
212232
@then('I can access a style by its UI name')
213233
def then_I_can_access_a_style_by_its_UI_name(context):
214234
styles = context.document.styles
@@ -237,6 +257,18 @@ def then_I_can_iterate_over_its_styles(context):
237257
assert all(isinstance(s, BaseStyle) for s in styles)
238258

239259

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+
240272
@then('len(styles) is {style_count_str}')
241273
def then_len_styles_is_style_count(context, style_count_str):
242274
assert len(context.document.styles) == int(style_count_str)
@@ -340,6 +372,14 @@ def then_style_unhide_when_used_is_value(context, value):
340372
assert style.unhide_when_used is expected_value
341373

342374

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+
343383
@then('styles[\'{name}\'] is a style')
344384
def then_styles_name_is_a_style(context, name):
345385
styles = context.document.styles
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Feature: Access latent styles for a document
2+
In order to operate on the latent styles for a document
3+
As a developer using python-docx
4+
I need access to the latent styles collection
5+
6+
7+
@wip
8+
Scenario: Access latent styles collection
9+
Given the style collection of a document
10+
Then styles.latent_styles is the LatentStyles object for the document
11+
And len(latent_styles) is 137
12+
13+
14+
@wip
15+
Scenario: Access latent style in collection
16+
Given a latent style collection
17+
Then I can iterate over the latent styles
18+
And I can access a latent style by name

0 commit comments

Comments
 (0)