Skip to content

Commit 49f935f

Browse files
author
Steve Canny
committed
reorg: extract font-related behave steps
features/steps/text.py was getting large and font is far enough along to warrant its own steps file.
1 parent d7e5c9b commit 49f935f

File tree

2 files changed

+151
-128
lines changed

2 files changed

+151
-128
lines changed

features/steps/font.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Step implementations for font-related features.
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from behave import given, then, when
12+
13+
from docx import Document
14+
from docx.enum.text import WD_UNDERLINE
15+
16+
from helpers import test_docx
17+
18+
19+
# given ===================================================
20+
21+
@given('a font having typeface name {name}')
22+
def given_a_font_having_typeface_name(context, name):
23+
document = Document(test_docx('txt-font-props'))
24+
style_name = {
25+
'not specified': 'Normal',
26+
'Avenir Black': 'Having Typeface',
27+
}[name]
28+
context.font = document.styles[style_name].font
29+
30+
31+
@given('a font having {underline_type} underline')
32+
def given_a_font_having_type_underline(context, underline_type):
33+
style_name = {
34+
'inherited': 'Normal',
35+
'no': 'None Underlined',
36+
'single': 'Underlined',
37+
'double': 'Double Underlined',
38+
}[underline_type]
39+
document = Document(test_docx('txt-font-props'))
40+
context.font = document.styles[style_name].font
41+
42+
43+
@given('a font having {vertAlign_state} vertical alignment')
44+
def given_a_font_having_vertAlign_state(context, vertAlign_state):
45+
style_name = {
46+
'inherited': 'Normal',
47+
'subscript': 'Subscript',
48+
'superscript': 'Superscript',
49+
}[vertAlign_state]
50+
document = Document(test_docx('txt-font-props'))
51+
context.font = document.styles[style_name].font
52+
53+
54+
@given('a font of size {size}')
55+
def given_a_font_of_size(context, size):
56+
document = Document(test_docx('txt-font-props'))
57+
style_name = {
58+
'unspecified': 'Normal',
59+
'14 pt': 'Having Typeface',
60+
'18 pt': 'Large Size',
61+
}[size]
62+
context.font = document.styles[style_name].font
63+
64+
65+
# when ====================================================
66+
67+
@when('I assign {value} to font.name')
68+
def when_I_assign_value_to_font_name(context, value):
69+
font = context.font
70+
value = None if value == 'None' else value
71+
font.name = value
72+
73+
74+
@when('I assign {value} to font.size')
75+
def when_I_assign_value_str_to_font_size(context, value):
76+
value = None if value == 'None' else int(value)
77+
font = context.font
78+
font.size = value
79+
80+
81+
@when('I assign {value} to font.underline')
82+
def when_I_assign_value_to_font_underline(context, value):
83+
new_value = {
84+
'True': True,
85+
'False': False,
86+
'None': None,
87+
'WD_UNDERLINE.SINGLE': WD_UNDERLINE.SINGLE,
88+
'WD_UNDERLINE.DOUBLE': WD_UNDERLINE.DOUBLE,
89+
}[value]
90+
font = context.font
91+
font.underline = new_value
92+
93+
94+
@when('I assign {value} to font.{sub_super}script')
95+
def when_I_assign_value_to_font_sub_super(context, value, sub_super):
96+
font = context.font
97+
name = {
98+
'sub': 'subscript',
99+
'super': 'superscript',
100+
}[sub_super]
101+
new_value = {
102+
'None': None,
103+
'True': True,
104+
'False': False,
105+
}[value]
106+
107+
setattr(font, name, new_value)
108+
109+
110+
# then =====================================================
111+
112+
@then('font.name is {value}')
113+
def then_font_name_is_value(context, value):
114+
font = context.font
115+
value = None if value == 'None' else value
116+
assert font.name == value
117+
118+
119+
@then('font.size is {value}')
120+
def then_font_size_is_value(context, value):
121+
value = None if value == 'None' else int(value)
122+
font = context.font
123+
assert font.size == value
124+
125+
126+
@then('font.underline is {value}')
127+
def then_font_underline_is_value(context, value):
128+
expected_value = {
129+
'None': None,
130+
'True': True,
131+
'False': False,
132+
'WD_UNDERLINE.DOUBLE': WD_UNDERLINE.DOUBLE,
133+
}[value]
134+
font = context.font
135+
assert font.underline == expected_value
136+
137+
138+
@then('font.{sub_super}script is {value}')
139+
def then_font_sub_super_is_value(context, sub_super, value):
140+
name = {
141+
'sub': 'subscript',
142+
'super': 'superscript',
143+
}[sub_super]
144+
expected_value = {
145+
'None': None,
146+
'True': True,
147+
'False': False,
148+
}[value]
149+
font = context.font
150+
actual_value = getattr(font, name)
151+
assert actual_value == expected_value

features/steps/text.py

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,6 @@
2424

2525
# given ===================================================
2626

27-
@given('a font having typeface name {name}')
28-
def given_a_font_having_typeface_name(context, name):
29-
document = Document(test_docx('txt-font-props'))
30-
style_name = {
31-
'not specified': 'Normal',
32-
'Avenir Black': 'Having Typeface',
33-
}[name]
34-
context.font = document.styles[style_name].font
35-
36-
37-
@given('a font having {underline_type} underline')
38-
def given_a_font_having_type_underline(context, underline_type):
39-
style_name = {
40-
'inherited': 'Normal',
41-
'no': 'None Underlined',
42-
'single': 'Underlined',
43-
'double': 'Double Underlined',
44-
}[underline_type]
45-
document = Document(test_docx('txt-font-props'))
46-
context.font = document.styles[style_name].font
47-
48-
49-
@given('a font having {vertAlign_state} vertical alignment')
50-
def given_a_font_having_vertAlign_state(context, vertAlign_state):
51-
style_name = {
52-
'inherited': 'Normal',
53-
'subscript': 'Subscript',
54-
'superscript': 'Superscript',
55-
}[vertAlign_state]
56-
document = Document(test_docx('txt-font-props'))
57-
context.font = document.styles[style_name].font
58-
59-
60-
@given('a font of size {size}')
61-
def given_a_font_of_size(context, size):
62-
document = Document(test_docx('txt-font-props'))
63-
style_name = {
64-
'unspecified': 'Normal',
65-
'14 pt': 'Having Typeface',
66-
'18 pt': 'Large Size',
67-
}[size]
68-
context.font = document.styles[style_name].font
69-
70-
7127
@given('a paragraph format having {prop_name} set {setting}')
7228
def given_a_paragraph_format_having_prop_set(context, prop_name, setting):
7329
style_name = {
@@ -250,33 +206,6 @@ def when_I_assign_mixed_text_to_the_text_property(context):
250206
context.run.text = 'abc\tdef\nghi\rjkl'
251207

252208

253-
@when('I assign {value} to font.name')
254-
def when_I_assign_value_to_font_name(context, value):
255-
font = context.font
256-
value = None if value == 'None' else value
257-
font.name = value
258-
259-
260-
@when('I assign {value} to font.size')
261-
def when_I_assign_value_str_to_font_size(context, value):
262-
value = None if value == 'None' else int(value)
263-
font = context.font
264-
font.size = value
265-
266-
267-
@when('I assign {value_key} to font.underline')
268-
def when_I_assign_value_to_font_underline(context, value_key):
269-
value = {
270-
'True': True,
271-
'False': False,
272-
'None': None,
273-
'WD_UNDERLINE.SINGLE': WD_UNDERLINE.SINGLE,
274-
'WD_UNDERLINE.DOUBLE': WD_UNDERLINE.DOUBLE,
275-
}[value_key]
276-
font = context.font
277-
font.underline = value
278-
279-
280209
@when('I assign {value_key} to paragraph_format.line_spacing')
281210
def when_I_assign_value_to_paragraph_format_line_spacing(context, value_key):
282211
value = {
@@ -302,22 +231,6 @@ def when_I_assign_value_to_paragraph_format_line_rule(context, value_key):
302231
paragraph_format.line_spacing_rule = value
303232

304233

305-
@when('I assign {value_key} to font.{sub_super}script')
306-
def when_I_assign_value_to_font_sub_super(context, value_key, sub_super):
307-
font = context.font
308-
name = {
309-
'sub': 'subscript',
310-
'super': 'superscript',
311-
}[sub_super]
312-
value = {
313-
'None': None,
314-
'True': True,
315-
'False': False,
316-
}[value_key]
317-
318-
setattr(font, name, value)
319-
320-
321234
@when('I assign {value_key} to paragraph_format.alignment')
322235
def when_I_assign_value_to_paragraph_format_alignment(context, value_key):
323236
value = {
@@ -392,47 +305,6 @@ def when_I_set_the_run_underline_to_value(context, underline_value):
392305

393306
# then =====================================================
394307

395-
@then('font.name is {value}')
396-
def then_font_name_is_value(context, value):
397-
font = context.font
398-
value = None if value == 'None' else value
399-
assert font.name == value
400-
401-
402-
@then('font.size is {value}')
403-
def then_font_size_is_value(context, value):
404-
value = None if value == 'None' else int(value)
405-
font = context.font
406-
assert font.size == value
407-
408-
409-
@then('font.underline is {value_key}')
410-
def then_font_underline_is_value(context, value_key):
411-
value = {
412-
'None': None,
413-
'True': True,
414-
'False': False,
415-
'WD_UNDERLINE.DOUBLE': WD_UNDERLINE.DOUBLE,
416-
}[value_key]
417-
font = context.font
418-
assert font.underline == value
419-
420-
421-
@then('font.{sub_super}script is {value_key}')
422-
def then_font_sub_super_is_value(context, sub_super, value_key):
423-
name = {
424-
'sub': 'subscript',
425-
'super': 'superscript',
426-
}[sub_super]
427-
expected_value = {
428-
'None': None,
429-
'True': True,
430-
'False': False,
431-
}[value_key]
432-
font = context.font
433-
actual_value = getattr(font, name)
434-
assert actual_value == expected_value
435-
436308

437309
@then('it is a column break')
438310
def then_type_is_column_break(context):

0 commit comments

Comments
 (0)