|
| 1 | +"""Step implementations for hyperlink-related features.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from behave import given, then |
| 6 | +from behave.runner import Context |
| 7 | + |
| 8 | +from docx import Document |
| 9 | + |
| 10 | +from helpers import test_docx |
| 11 | + |
| 12 | +# given =================================================== |
| 13 | + |
| 14 | + |
| 15 | +@given("a hyperlink") |
| 16 | +def given_a_hyperlink(context: Context): |
| 17 | + document = Document(test_docx("par-hyperlinks")) |
| 18 | + context.hyperlink = document.paragraphs[1].hyperlinks[0] |
| 19 | + |
| 20 | + |
| 21 | +@given("a hyperlink having {zero_or_more} rendered page breaks") |
| 22 | +def given_a_hyperlink_having_rendered_page_breaks(context: Context, zero_or_more: str): |
| 23 | + paragraph_idx = { |
| 24 | + "no": 1, |
| 25 | + "one": 2, |
| 26 | + }[zero_or_more] |
| 27 | + document = Document(test_docx("par-hyperlinks")) |
| 28 | + paragraph = document.paragraphs[paragraph_idx] |
| 29 | + context.hyperlink = paragraph.hyperlinks[0] |
| 30 | + |
| 31 | + |
| 32 | +@given("a hyperlink having {one_or_more} runs") |
| 33 | +def given_a_hyperlink_having_one_or_more_runs(context: Context, one_or_more: str): |
| 34 | + paragraph_idx, hyperlink_idx = { |
| 35 | + "one": (1, 0), |
| 36 | + "two": (2, 1), |
| 37 | + }[one_or_more] |
| 38 | + document = Document(test_docx("par-hyperlinks")) |
| 39 | + paragraph = document.paragraphs[paragraph_idx] |
| 40 | + context.hyperlink = paragraph.hyperlinks[hyperlink_idx] |
| 41 | + |
| 42 | + |
| 43 | +# then ===================================================== |
| 44 | + |
| 45 | + |
| 46 | +@then("hyperlink.address is the URL of the hyperlink") |
| 47 | +def then_hyperlink_address_is_the_URL_of_the_hyperlink(context: Context): |
| 48 | + actual_value = context.hyperlink.address |
| 49 | + expected_value = "http://yahoo.com/" |
| 50 | + assert ( |
| 51 | + actual_value == expected_value |
| 52 | + ), f"expected: {expected_value}, got: {actual_value}" |
| 53 | + |
| 54 | + |
| 55 | +@then("hyperlink.contains_page_break is {value}") |
| 56 | +def then_hyperlink_contains_page_break_is_value(context: Context, value: str): |
| 57 | + actual_value = context.hyperlink.contains_page_break |
| 58 | + expected_value = {"True":
B41A
True, "False": False}[value] |
| 59 | + assert ( |
| 60 | + actual_value == expected_value |
| 61 | + ), f"expected: {expected_value}, got: {actual_value}" |
| 62 | + |
| 63 | + |
| 64 | +@then("hyperlink.runs contains only Run instances") |
| 65 | +def then_hyperlink_runs_contains_only_Run_instances(context: Context): |
| 66 | + actual_value = [type(item).__name__ for item in context.hyperlink.runs] |
| 67 | + expected_value = ["Run" for _ in context.hyperlink.runs] |
| 68 | + assert ( |
| 69 | + actual_value == expected_value |
| 70 | + ), f"expected: {expected_value}, got: {actual_value}" |
| 71 | + |
| 72 | + |
| 73 | +@then("hyperlink.runs has length {value}") |
| 74 | +def then_hyperlink_runs_has_length(context: Context, value: str): |
| 75 | + actual_value = len(context.hyperlink.runs) |
| 76 | + expected_value = int(value) |
| 77 | + assert ( |
| 78 | + actual_value == expected_value |
| 79 | + ), f"expected: {expected_value}, got: {actual_value}" |
| 80 | + |
| 81 | + |
| 82 | +@then("hyperlink.text is the visible text of the hyperlink") |
| 83 | +def then_hyperlink_text_is_the_visible_text_of_the_hyperlink(context: Context): |
| 84 | + actual_value = context.hyperlink.text |
| 85 | + expected_value = "awesome hyperlink" |
| 86 | + assert ( |
| 87 | + actual_value == expected_value |
| 88 | + ), f"expected: {expected_value}, got: {actual_value}" |
0 commit comments