Skip to content

Commit ff0bd91

Browse files
author
Steve Canny
committed
acpt: clean up picture-related test steps
* rename helpers.test_file_path to test_file * move picture-related assertions to steps/shape.py * refactor given_a_run()
1 parent 7df5323 commit ff0bd91

File tree

6 files changed

+45
-46
lines changed

6 files changed

+45
-46
lines changed

features/run-add-content.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ Feature: Add content to a run
33
As a developer using python-docx
44
I need a way to add each of the run content elements to a run
55

6-
76
Scenario: Add a tab
87
Given a run
98
When I add a tab
109
Then the tab appears at the end of the run
1110

12-
1311
Scenario: Assign mixed text to text property
1412
Given a run
1513
When I assign mixed text to the text property

features/steps/api.py

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from docx.shared import Inches
1010
from docx.table import Table
1111

12-
from helpers import test_file_path
12+
from helpers import test_file
1313

1414

1515
# when ====================================================
@@ -70,7 +70,7 @@ def when_add_paragraph_without_specifying_text_or_style(context):
7070
def when_add_picture_specifying_width_and_height(context):
7171
document = context.document
7272
context.picture = document.add_picture(
73-
test_file_path('monty-truth.png'),
73+
test_file('monty-truth.png'),
7474
width=Inches(1.75), height=Inches(2.5)
7575
)
7676

@@ -79,22 +79,22 @@ def when_add_picture_specifying_width_and_height(context):
7979
def when_add_picture_specifying_height(context):
8080
document = context.document
8181
context.picture = document.add_picture(
82-
test_file_path('monty-truth.png'), height=Inches(1.5)
82+
test_file('monty-truth.png'), height=Inches(1.5)
8383
)
8484

8585

8686
@when('I add a picture specifying a width of 1.5 inches')
8787
def when_add_picture_specifying_width(context):
8888
document = context.document
8989
context.picture = document.add_picture(
90-
test_file_path('monty-truth.png'), width=Inches(1.5)
90+
test_file('monty-truth.png'), width=Inches(1.5)
9191
)
9292

9393

9494
@when('I add a picture specifying only the image file')
9595
def when_add_picture_specifying_only_image_file(context):
9696
document = context.document
97-
context.picture = document.add_picture(test_file_path('monty-truth.png'))
97+
context.picture = document.add_picture(test_file('monty-truth.png'))
9898

9999

100100
# then =====================================================
@@ -149,37 +149,6 @@ def then_last_p_is_empty_paragraph_added(context):
149149
assert p.text == ''
150150

151151

152-
@then('the picture has its native width and height')
153-
def then_picture_has_native_width_and_height(context):
154-
picture = context.picture
155-
assert picture.width == 1905000, 'got %d' % picture.width
156-
assert picture.height == 2717800, 'got %d' % picture.height
157-
158-
159-
@then('the picture height is 2.14 inches')
160-
def then_picture_height_is_value_2(context):
161-
picture = context.picture
162-
assert picture.height == 1956816, 'got %d' % picture.height
163-
164-
165-
@then('the picture height is 2.5 inches')
166-
def then_picture_height_is_value(context):
167-
picture = context.picture
168-
assert picture.height == 2286000, 'got %d' % picture.height
169-
170-
171-
@then('the picture width is 1.05 inches')
172-
def then_picture_width_is_value_2(context):
173-
picture = context.picture
174-
assert picture.width == 961402, 'got %d' % picture.width
175-
176-
177-
@then('the picture width is 1.75 inches')
178-
def then_picture_width_is_value(context):
179-
picture = context.picture
180-
assert picture.width == 1600200, 'got %d' % picture.width
181-
182-
183152
@then('the style of the last paragraph is \'{style}\'')
184153
def then_style_of_last_paragraph_is_style(context, style):
185154
document = context.document

features/steps/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_docx(name):
2626
return absjoin(thisdir, 'test_files', '%s.docx' % name)
2727

2828

29-
def test_file_path(name):
29+
def test_file(name):
3030
"""
3131
Return the absolute path to file with *name* in test_files directory
3232
"""

features/steps/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
from docx.image.image import Image
1212

13-
from helpers import test_file_path
13+
from helpers import test_file
1414

1515

1616
# given ===================================================
1717

1818
@given('the image file \'{filename}\'')
1919
def given_image_filename(context, filename):
20-
context.image_path = test_file_path(filename)
20+
context.image_path = test_file(filename)
2121

2222

2323
# when ====================================================

features/steps/shape.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from docx.parts.document import InlineShape, InlineShapes
1616
from docx.shared import Inches
1717

18-
from helpers import test_docx, test_file_path
18+
from helpers import test_docx, test_file
1919

2020

2121
# given ===================================================
@@ -59,7 +59,7 @@ def given_inline_shape_known_to_be_shape_of_type(context, shp_of_type):
5959
def when_add_inline_picture_from_file_like_object(context):
6060
document = context.document
6161
run = document.add_paragraph().add_run()
62-
with open(test_file_path('monty-truth.png'), 'rb') as f:
62+
with open(test_file('monty-truth.png'), 'rb') as f:
6363
context.inline_shape = document.inline_shapes.add_picture(f, run)
6464

6565

@@ -68,7 +68,7 @@ def when_add_inline_picture_to_document(context):
6868
document = context.document
6969
run = document.add_paragraph().add_run()
7070
context.inline_shape = (document.inline_shapes.add_picture(
71-
test_file_path('monty-truth.png'), run
71+
test_file('monty-truth.png'), run
7272
))
7373

7474

@@ -155,3 +155,34 @@ def then_len_of_inline_shape_collection_is_5(context):
155155
inline_shapes = context.document.inline_shapes
156156
shape_count = len(inline_shapes)
157157
assert shape_count == 5, 'got %s' % shape_count
158+
159+
160+
@then('the picture has its native width and height')
161+
def then_picture_has_native_width_and_height(context):
162+
picture = context.picture
163+
assert picture.width == 1905000, 'got %d' % picture.width
164+
assert picture.height == 2717800, 'got %d' % picture.height
165+
166+
167+
@then('the picture height is 2.14 inches')
168+
def then_picture_height_is_value_2(context):
169+
picture = context.picture
170+
assert picture.height == 1956816, 'got %d' % picture.height
171+
172+
173+
@then('the picture height is 2.5 inches')
174+
def then_picture_height_is_value(context):
175+
picture = context.picture
176+
assert picture.height == 2286000, 'got %d' % picture.height
177+
178+
179+
@then('the picture width is 1.05 inches')
180+
def then_picture_width_is_value_2(context):
181+
picture = context.picture
182+
assert picture.width == 961402, 'got %d' % picture.width
183+
184+
185+
@then('the picture width is 1.75 inches')
186+
def then_picture_width_is_value(context):
187+
picture = context.picture
188+
assert picture.width == 1600200, 'got %d' % picture.width

features/steps/text.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121

2222
@given('a run')
2323
def given_a_run(context):
24-
p = Document().add_paragraph()
25-
context.run = p.add_run()
24+
document = Document()
25+
run = document.add_paragraph().add_run()
26+
context.run = run
2627

2728

2829
@given('a run having {bool_prop_name} set on')

0 commit comments

Comments
 (0)