Skip to content

Commit d360409

Browse files
committed
xfail: acceptance test for Comment mutations
1 parent 432dd15 commit d360409

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

features/cmt-mutations.feature

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Feature: Comment mutations
2+
In order to add and modify the content of a comment
3+
As a developer using python-docx
4+
I need mutation methods on Comment objects
5+
6+
7+
@wip
8+
Scenario: Comments.add_comment()
9+
Given a Comments object with 0 comments
10+
When I assign comment = comments.add_comment()
11+
Then comment.comment_id == 0
12+
And len(comment.paragraphs) == 1
13+
And comment.paragraphs[0].style.name == "CommentText"
14+
And len(comments) == 1
15+
And comments.get(0) == comment
16+
17+
18+
@wip
19+
Scenario: Comments.add_comment() specifying author and initials
20+
Given a Comments object with 0 comments
21+
When I assign comment = comments.add_comment(author="John Doe", initials="JD")
22+
Then comment.author == "John Doe"
23+
And comment.initials == "JD"
24+
25+
26+
@wip
27+
Scenario: Comment.add_paragraph() specifying text and style
28+
Given a default Comment object
29+
When I assign paragraph = comment.add_paragraph(text, style)
30+
Then len(comment.paragraphs) == 2
31+
And paragraph.text == text
32+
And paragraph.style == style
33+
And comment.paragraphs[-1] == paragraph
34+
35+
36+
@wip
37+
Scenario: Comment.add_paragraph() not specifying text or style
38+
Given a default Comment object
39+
When I assign paragraph = comment.add_paragraph()
40+
Then len(comment.paragraphs) == 2
41+
And paragraph.text == ""
42+
And paragraph.style == "CommentText"
43+
And comment.paragraphs[-1] == paragraph
44+
45+
46+
@wip
47+
Scenario: Add image to comment
48+
Given a default Comment object
49+
When I assign paragraph = comment.add_paragraph()
50+
And I assign run = paragraph.add_run()
51+
And I call run.add_picture()
52+
Then run.iter_inner_content() yields a single Picture drawing
53+
54+
55+
@wip
56+
Scenario: update Comment.author
57+
Given a Comment object
58+
When I assign "Jane Smith" to comment.author
59+
Then comment.author == "Jane Smith"
60+
61+
62+
@wip
63+
Scenario: update Comment.initials
64+
Given a Comment object
65+
When I assign "JS" to comment.initials
66+
Then comment.initials == "JS"

features/steps/comments.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def given_a_comments_object_with_count_comments(context: Context, count: str):
3030
context.comments = Document(test_docx(testfile_name)).comments
3131

3232

33+
@given("a default Comment object")
34+
def given_a_default_comment_object(context: Context):
35+
context.comment = Document(test_docx("comments-rich-para")).comments.add_comment()
36+
37+
3338
@given("a document having a comments part")
3439
def given_a_document_having_a_comments_part(context: Context):
3540
context.document = Document(test_docx("comments-rich-para"))
@@ -43,11 +48,48 @@ def given_a_document_having_no_comments_part(context: Context):
4348
# when =====================================================
4449

4550

51+
@when('I assign "{author}" to comment.author')
52+
def when_I_assign_author_to_comment_author(context: Context, author: str):
53+
context.comment.author = author
54+
55+
56+
@when("I assign comment = comments.add_comment()")
57+
def when_I_assign_comment_eq_add_comment(context: Context):
58+
context.comment = context.comments.add_comment()
59+
60+
61+
@when('I assign comment = comments.add_comment(author="John Doe", initials="JD")')
62+
def when_I_assign_comment_eq_comments_add_comment_with_author_and_initials(context: Context):
63+
context.comment = context.comments.add_comment(author="John Doe", initials="JD")
64+
65+
66+
@when('I assign "{initials}" to comment.initials')
67+
def when_I_assign_initials(context: Context, initials: str):
68+
context.comment.initials = initials
69+
70+
4671
@when("I assign para_text = comment.paragraphs[0].text")
4772
def when_I_assign_para_text(context: Context):
4873
context.para_text = context.comment.paragraphs[0].text
4974

5075

76+
@when("I assign paragraph = comment.add_paragraph()")
77+
def when_I_assign_default_add_paragraph(context: Context):
78+
context.paragraph = context.comment.add_paragraph()
79+
80+
81+
@when("I assign paragraph = comment.add_paragraph(text, style)")
82+
def when_I_assign_add_paragraph_with_text_and_style(context: Context):
83+
context.para_text = text = "Comment text"
84+
context.para_style = style = "Normal"
85+
context.paragraph = context.comment.add_paragraph(text, style)
86+
87+
88+
@when("I assign run = paragraph.add_run()")
89+
def when_I_assign_paragraph_add_run(context: Context):
90+
context.run = context.paragraph.add_run()
91+
92+
5193
@when("I call comments.get(2)")
5294
def when_I_call_comments_get_2(context: Context):
5395
context.comment = context.comments.get(2)
@@ -62,6 +104,17 @@ def then_comment_author_is_the_author_of_the_comment(context: Context):
62104
assert actual == "Steve Canny", f"expected author 'Steve Canny', got '{actual}'"
63105

64106

107+
@then('comment.author == "{author}"')
108+
def then_comment_author_eq_author(context: Context, author: str):
109+
actual = context.comment.author
110+
assert actual == author, f"expected author '{author}', got '{actual}'"
111+
112+
113+
@then("comment.comment_id == 0")
114+
def then_comment_id_is_0(context: Context):
115+
assert context.comment.comment_id == 0
116+
117+
65118
@then("comment.comment_id is the comment identifier")
66119
def then_comment_comment_id_is_the_comment_identifier(context: Context):
67120
assert context.comment.comment_id == 0
@@ -73,11 +126,42 @@ def then_comment_initials_is_the_initials_of_the_comment_author(context: Context
73126
assert initials == "SJC", f"expected initials 'SJC', got '{initials}'"
74127

75128

129+
@then('comment.initials == "{initials}"')
130+
def then_comment_initials_eq_initials(context: Context, initials: str):
131+
actual = context.comment.initials
132+
assert actual == initials, f"expected initials '{initials}', got '{actual}'"
133+
134+
135+
@then("comment.paragraphs[{idx}] == paragraph")
136+
def then_comment_paragraphs_idx_eq_paragraph(context: Context, idx: str):
137+
actual = context.comment.paragraphs[int(idx)]._p
138+
expected = context.paragraph._p
139+
assert actual == expected, "paragraphs do not compare equal"
140+
141+
142+
@then('comment.paragraphs[{idx}].style.name == "{style}"')
143+
def then_comment_paragraphs_idx_style_name_eq_style(context: Context, idx: str, style: str):
144+
actual = context.comment.paragraphs[int(idx)]._p.style
145+
expected = style
146+
assert actual == expected, f"expected style name '{expected}', got '{actual}'"
147+
148+
76149
@then("comment.timestamp is the date and time the comment was authored")
77150
def then_comment_timestamp_is_the_date_and_time_the_comment_was_authored(context: Context):
78151
assert context.comment.timestamp == dt.datetime(2025, 6, 7, 11, 20, 0, tzinfo=dt.timezone.utc)
79152

80153

154+
@then("comments.get({id}) == comment")
155+
def then_comments_get_comment_id_eq_comment(context: Context, id: str):
156+
comment_id = int(id)
157+
comment = context.comments.get(comment_id)
158+
159+
assert type(comment) is Comment, f"expected a Comment object, got {type(comment)}"
160+
assert comment.comment_id == comment_id, (
161+
f"expected comment_id '{comment_id}', got '{comment.comment_id}'"
162+
)
163+
164+
81165
@then("document.comments is a Comments object")
82166
def then_document_comments_is_a_Comments_object(context: Context):
83167
document = context.document
@@ -109,6 +193,13 @@ def then_iterating_comments_yields_count_comments(context: Context, count: str):
109193
assert len(remaining) == int(count) - 1, "iterating comments did not yield the expected count"
110194

111195

196+
@then("len(comment.paragraphs) == {count}")
197+
def then_len_comment_paragraphs_eq_count(context: Context, count: str):
198+
actual = len(context.comment.paragraphs)
199+
expected = int(count)
200+
assert actual == expected, f"expected len(comment.paragraphs) of {expected}, got {actual}"
201+
202+
112203
@then("len(comments) == {count}")
113204
def then_len_comments_eq_count(context: Context, count: str):
114205
actual = len(context.comments)
@@ -123,6 +214,46 @@ def then_para_text_is_the_text_of_the_first_paragraph_in_the_comment(context: Co
123214
assert actual == expected, f"expected para_text '{expected}', got '{actual}'"
124215

125216

217+
@then("paragraph.style == style")
218+
def then_paragraph_style_eq_known_style(context: Context):
219+
actual = context.paragraph.style.name
220+
expected = context.para_style
221+
assert actual == expected, f"expected paragraph.style '{expected}', got '{actual}'"
222+
223+
224+
@then('paragraph.style == "{style}"')
225+
def then_paragraph_style_eq_style(context: Context, style: str):
226+
actual = context.paragraph._p.style
227+
expected = style
228+
assert actual == expected, f"expected paragraph.style '{expected}', got '{actual}'"
229+
230+
231+
@then("paragraph.text == text")
232+
def then_paragraph_text_eq_known_text(context: Context):
233+
actual = context.paragraph.text
234+
expected = context.para_text
235+
assert actual == expected, f"expected paragraph.text '{expected}', got '{actual}'"
236+
237+
238+
@then('paragraph.text == ""')
239+
def then_paragraph_text_eq_text(context: Context):
240+
actual = context.paragraph.text
241+
expected = ""
242+
assert actual == expected, f"expected paragraph.text '{expected}', got '{actual}'"
243+
244+
245+
@then("run.iter_inner_content() yields a single Picture drawing")
246+
def then_run_iter_inner_content_yields_a_single_picture_drawing(context: Context):
247+
inner_content = list(context.run.iter_inner_content())
248+
249+
assert len(inner_content) == 1, (
250+
f"expected a single inner content element, got {len(inner_content)}"
251+
)
252+
inner_content_item = inner_content[0]
253+
assert isinstance(inner_content_item, Drawing)
254+
assert inner_content_item.has_picture
255+
256+
126257
@then("the result is a Comment object with id 2")
127258
def then_the_result_is_a_comment_object_with_id_2(context: Context):
128259
comment = context.comment
49 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)