🌐 AI搜索 & 代理 主页
Skip to content

Commit 5cb32d7

Browse files
committed
xfail: acceptance test for Document.comments
1 parent d9da49b commit 5cb32d7

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

features/doc-comments.feature

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Feature: Document.comments
2+
In order to operate on comments added to a document
3+
As a developer using python-docx
4+
I need access to the comments collection for the document
5+
And I need methods allowing access to the comments in the collection
6+
7+
8+
@wip
9+
Scenario Outline: Access document comments
10+
Given a document having <a-or-no> comments part
11+
Then document.comments is a Comments object
12+
13+
Examples: having a comments part or not
14+
| a-or-no |
15+
| a |
16+
| no |
17+
18+
19+
@wip
20+
Scenario Outline: Comments.__len__()
21+
Given a Comments object with <count> comments
22+
Then len(comments) == <count>
23+
24+
Examples: len(comments) values
25+
| count |
26+
| 0 |
27+
| 4 |
28+
29+
30+
@wip
31+
Scenario: Comments.__iter__()
32+
Given a Comments object with 4 comments
33+
Then iterating comments yields 4 Comment objects
34+
35+
36+
@wip
37+
Scenario: Comments.get()
38+
Given a Comments object with 4 comments
39+
When I call comments.get(2)
40+
Then the result is a Comment object with id 2

features/steps/comments.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Step implementations for document comments-related features."""
2+
3+
from behave import given, then, when
4+
from behave.runner import Context
5+
6+
from docx import Document
7+
from docx.comments import Comment, Comments
8+
9+
from helpers import test_docx
10+
11+
# given ====================================================
12+
13+
14+
@given("a Comments object with {count} comments")
15+
def given_a_comments_object_with_count_comments(context: Context, count: str):
16+
testfile_name = {"0": "doc-default", "4": "comments-rich-para"}[count]
17+
context.comments = Document(test_docx(testfile_name)).comments
18+
19+
20+
@given("a document having a comments part")
21+
def given_a_document_having_a_comments_part(context: Context):
22+
context.document = Document(test_docx("comments-rich-para"))
23+
24+
25+
@given("a document having no comments part")
26+
def given_a_document_having_no_comments_part(context: Context):
27+
context.document = Document(test_docx("doc-default"))
28+
29+
30+
# when =====================================================
31+
32+
33+
@when("I call comments.get(2)")
34+
def when_I_call_comments_get_2(context: Context):
35+
context.comment = context.comments.get(2)
36+
37+
38+
# then =====================================================
39+
40+
41+
@then("document.comments is a Comments object")
42+
def then_document_comments_is_a_Comments_object(context: Context):
43+
document = context.document
44+
assert type(document.comments) is Comments
45+
46+
47+
@then("iterating comments yields {count} Comment objects")
48+
def then_iterating_comments_yields_count_comments(context: Context, count: str):
49+
comment_iter = iter(context.comments)
50+
51+
comment = next(comment_iter)
52+
assert type(comment) is Comment, f"expected a Comment object, got {type(comment)}"
53+
54+
remaining = list(comment_iter)
55+
assert len(remaining) == int(count) - 1, "iterating comments did not yield the expected count"
56+
57+
58+
@then("len(comments) == {count}")
59+
def then_len_comments_eq_count(context: Context, count: str):
60+
actual = len(context.comments)
61+
expected = int(count)
62+
assert actual == expected, f"expected len(comments) of {expected}, got {actual}"
63+
64+
65+
@then("the result is a Comment object with id 2")
66+
def then_the_result_is_a_comment_object_with_id_2(context: Context):
67+
comment = context.comment
68+
assert type(comment) is Comment, f"expected a Comment object, got {type(comment)}"
69+
assert comment.comment_id == 2, f"expected comment_id `2`, got '{comment.comment_id}'"
19.5 KB
Binary file not shown.

src/docx/comments.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Collection providing access to comments added to this document."""
2+
3+
from __future__ import annotations
4+
5+
from docx.blkcntnr import BlockItemContainer
6+
7+
8+
class Comments:
9+
"""Collection containing the comments added to this document."""
10+
11+
12+
class Comment(BlockItemContainer):
13+
"""Proxy for a single comment in the document.
14+
15+
Provides methods to access comment metadata such as author, initials, and date.
16+
17+
A comment is also a block-item container, similar to a table cell, so it can contain both
18+
paragraphs and tables and its paragraphs can contain rich text, hyperlinks and images,
19+
although the common case is that a comment contains a single paragraph of plain text like a
20+
sentence or phrase.
21+
22+
Note that certain content like tables may not be displayed in the Word comment sidebar due to
23+
space limitations. Such "over-sized" content can still be viewed in the review pane.
24+
"""

0 commit comments

Comments
 (0)