Skip to content

Commit 6629346

Browse files
author
Steve Canny
committed
acpt: add scenarios for ParaFormat line spacing
1 parent d16452e commit 6629346

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

features/steps/text.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from behave import given, then, when
1212

1313
from docx import Document
14-
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK, WD_UNDERLINE
14+
from docx.enum.text import (
15+
WD_ALIGN_PARAGRAPH, WD_BREAK, WD_LINE_SPACING, WD_UNDERLINE
16+
)
1517
from docx.oxml import parse_xml
1618
from docx.oxml.ns import nsdecls, qn
1719
from docx.shared import Pt
@@ -66,6 +68,17 @@ def given_a_font_of_size(context, size):
6668
context.font = document.styles[style_name].font
6769

6870

71+
@given('a paragraph format having {setting} line spacing')
72+
def given_a_paragraph_format_having_setting_line_spacing(context, setting):
73+
style_name = {
74+
'inherited': 'Normal',
75+
'14 pt': 'Base',
76+
'double': 'Citation',
77+
}[setting]
78+
document = Document(test_docx('sty-known-styles'))
79+
context.paragraph_format = document.styles[style_name].paragraph_format
80+
81+
6982
@given('a paragraph format having {setting} space {side}')
7083
def given_a_paragraph_format_having_setting_spacing(context, setting, side):
7184
style_name = 'Normal' if setting == 'inherited' else 'Base'
@@ -238,6 +251,31 @@ def when_I_assign_value_to_font_underline(context, value_key):
238251
font.underline = value
239252

240253

254+
@when('I assign {value_key} to paragraph_format.line_spacing')
255+
def when_I_assign_value_to_paragraph_format_line_spacing(context, value_key):
256+
value = {
257+
'Pt(14)': Pt(14),
258+
'2': 2,
259+
}.get(value_key)
260+
value = float(value_key) if value is None else value
261+
context.paragraph_format.line_spacing = value
262+
263+
264+
@when('I assign {value_key} to paragraph_format.line_spacing_rule')
265+
def when_I_assign_value_to_paragraph_format_line_rule(context, value_key):
266+
value = {
267+
'None': None,
268+
'WD_LINE_SPACING.EXACTLY': WD_LINE_SPACING.EXACTLY,
269+
'WD_LINE_SPACING.MULTIPLE': WD_LINE_SPACING.MULTIPLE,
270+
'WD_LINE_SPACING.SINGLE': WD_LINE_SPACING.SINGLE,
271+
'WD_LINE_SPACING.DOUBLE': WD_LINE_SPACING.DOUBLE,
272+
'WD_LINE_SPACING.AT_LEAST': WD_LINE_SPACING.AT_LEAST,
273+
'WD_LINE_SPACING.ONE_POINT_FIVE': WD_LINE_SPACING.ONE_POINT_FIVE,
274+
}[value_key]
275+
paragraph_format = context.paragraph_format
276+
paragraph_format.line_spacing_rule = value
277+
278+
241279
@when('I assign {value_key} to font.{sub_super}script')
242280
def when_I_assign_value_to_font_sub_super(context, value_key, sub_super):
243281
font = context.font
@@ -381,6 +419,36 @@ def then_paragraph_format_alignment_is_value(context, value_key):
381419
assert paragraph_format.alignment == value
382420

383421

422+
@then('paragraph_format.line_spacing is {value}')
423+
def then_paragraph_format_line_spacing_is_value(context, value):
424+
value = (
425+
None if value == 'None' else
426+
float(value) if '.' in value else
427+
int(value)
428+
)
429+
paragraph_format = context.paragraph_format
430+
431+
if value is None or isinstance(value, int):
432+
assert paragraph_format.line_spacing == value
433+
else:
434+
assert abs(paragraph_format.line_spacing - value) < 0.001
435+
436+
437+
@then('paragraph_format.line_spacing_rule is {value_key}')
438+
def then_paragraph_format_line_spacing_rule_is_value(context, value_key):
439+
value = {
440+
'None': None,
441+
'WD_LINE_SPACING.EXACTLY': WD_LINE_SPACING.EXACTLY,
442+
'WD_LINE_SPACING.MULTIPLE': WD_LINE_SPACING.MULTIPLE,
443+
'WD_LINE_SPACING.SINGLE': WD_LINE_SPACING.SINGLE,
444+
'WD_LINE_SPACING.DOUBLE': WD_LINE_SPACING.DOUBLE,
445+
'WD_LINE_SPACING.AT_LEAST': WD_LINE_SPACING.AT_LEAST,
446+
'WD_LINE_SPACING.ONE_POINT_FIVE': WD_LINE_SPACING.ONE_POINT_FIVE,
447+
}[value_key]
448+
paragraph_format = context.paragraph_format
449+
assert paragraph_format.line_spacing_rule == value
450+
451+
384452
@then('paragraph_format.space_{side} is {value}')
385453
def then_paragraph_format_space_side_is_value(context, side, value):
386454
expected_value = None if value == 'None' else int(value)

features/txt-parfmt-props.feature

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,47 @@ Feature: Get or set paragraph formatting properties
5252
| after | inherited | Pt(12) | 152400 |
5353
| after | 42 pt | Pt(18) | 228600 |
5454
| after | 42 pt | None | None |
55+
56+
57+
@wip
58+
Scenario Outline: Get line spacing
59+
Given a paragraph format having <setting> line spacing
60+
Then paragraph_format.line_spacing is <value>
61+
Then paragraph_format.line_spacing_rule is <rule-value>
62+
63+
Examples: paragraph_format.line_spacing values
64+
| setting | value | rule-value |
65+
| inherited | None | None |
66+
| 14 pt | 177800 | WD_LINE_SPACING.EXACTLY |
67+
| double | 2.0 | WD_LINE_SPACING.DOUBLE |
68+
69+
70+
@wip
71+
Scenario Outline: Set line spacing
72+
Given a paragraph format having <setting> line spacing
73+
When I assign <new-value> to paragraph_format.line_spacing
74+
Then paragraph_format.line_spacing is <value>
75+
Then paragraph_format.line_spacing_rule is <rule-value>
76+
77+
Examples: paragraph_format.line_spacing assignment results
78+
| setting | new-value | value | rule-value |
79+
| inherited | Pt(14) | 177800 | WD_LINE_SPACING.EXACTLY |
80+
| 14 pt | 2 | 2.0 | WD_LINE_SPACING.DOUBLE |
81+
| double | 1.75 | 1.75 | WD_LINE_SPACING.MULTIPLE |
82+
| inherited | 1.0 | 1.0 | WD_LINE_SPACING.SINGLE |
83+
| 14 pt | 1.5 | 1.5 | WD_LINE_SPACING.ONE_POINT_FIVE |
84+
85+
86+
@wip
87+
Scenario Outline: Set line spacing rule
88+
Given a paragraph format having <setting> line spacing
89+
When I assign <new-value> to paragraph_format.line_spacing_rule
90+
Then paragraph_format.line_spacing is <value>
91+
Then paragraph_format.line_spacing_rule is <rule-value>
92+
93+
Examples: paragraph_format.line_spacing_rule assignment results
94+
| setting | new-value | value | rule-value |
95+
| 14 pt | WD_LINE_SPACING.DOUBLE | 2.0 | WD_LINE_SPACING.DOUBLE |
96+
| double | WD_LINE_SPACING.SINGLE | 1.0 | WD_LINE_SPACING.SINGLE |
97+
| 14 pt | WD_LINE_SPACING.AT_LEAST | 177800 | WD_LINE_SPACING.AT_LEAST |
98+
| 14 pt | None | 1.1666 | WD_LINE_SPACING.MULTIPLE |

0 commit comments

Comments
 (0)