|
| 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}'" |
0 commit comments