|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +"""Step implementations for header and footer-related features""" |
| 4 | + |
| 5 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 6 | + |
| 7 | +from behave import given, then, when |
| 8 | + |
| 9 | +from docx import Document |
| 10 | + |
| 11 | +from helpers import test_docx |
| 12 | + |
| 13 | + |
| 14 | +# given ==================================================== |
| 15 | + |
| 16 | +@given("a _Footer object {with_or_no} footer definition as footer") |
| 17 | +def given_a_Footer_object_with_or_no_footer_definition(context, with_or_no): |
| 18 | + section_idx = {"with a": 0, "with no": 1}[with_or_no] |
| 19 | + context.sections = Document(test_docx("hdr-header-footer")).sections |
| 20 | + context.footer = context.sections[section_idx].footer |
| 21 | + |
| 22 | + |
| 23 | +@given("a _Header object {with_or_no} header definition as header") |
| 24 | +def given_a_Header_object_with_or_no_header_definition(context, with_or_no): |
| 25 | + section_idx = {"with a": 0, "with no": 1}[with_or_no] |
| 26 | + context.sections = Document(test_docx("hdr-header-footer")).sections |
| 27 | + context.header = context.sections[section_idx].header |
| 28 | + |
| 29 | + |
| 30 | +@given("the next _Footer object with no footer definition as footer_2") |
| 31 | +def given_the_next_Footer_object_with_no_footer_definition(context): |
| 32 | + context.footer_2 = context.sections[1].footer |
| 33 | + |
| 34 | + |
| 35 | +@given("the next _Header object with no header definition as header_2") |
| 36 | +def given_the_next_Header_object_with_no_header_definition(context): |
| 37 | + context.header_2 = context.sections[1].header |
| 38 | + |
| 39 | + |
| 40 | +# when ===================================================== |
| 41 | + |
| 42 | +@when("I assign {value} to header.is_linked_to_previous") |
| 43 | +def when_I_assign_value_to_header_is_linked_to_previous(context, value): |
| 44 | + context.header.is_linked_to_previous = eval(value) |
| 45 | + |
| 46 | + |
| 47 | +@when("I assign {value} to footer.is_linked_to_previous") |
| 48 | +def when_I_assign_value_to_footer_is_linked_to_previous(context, value): |
| 49 | + context.footer.is_linked_to_previous = eval(value) |
| 50 | + |
| 51 | + |
| 52 | +# then ===================================================== |
| 53 | + |
| 54 | +@then("footer.is_linked_to_previous is {value}") |
| 55 | +def then_footer_is_linked_to_previous_is_value(context, value): |
| 56 | + actual = context.footer.is_linked_to_previous |
| 57 | + expected = eval(value) |
| 58 | + assert actual == expected, "footer.is_linked_to_previous is %s" % actual |
| 59 | + |
| 60 | + |
| 61 | +@then("footer_2.paragraphs[0].text == footer.paragraphs[0].text") |
| 62 | +def then_footer_2_text_eq_footer_text(context): |
| 63 | + actual = context.footer_2.paragraphs[0].text |
| 64 | + expected = context.footer.paragraphs[0].text |
| 65 | + assert actual == expected, "footer_2.paragraphs[0].text == %s" % actual |
| 66 | + |
| 67 | + |
| 68 | +@then("footer_2.is_linked_to_previous is {value}") |
| 69 | +def then_footer_2_is_linked_to_previous_is_value(context, value): |
| 70 | + actual = context.footer_2.is_linked_to_previous |
| 71 | + expected = eval(value) |
| 72 | + assert actual == expected, "footer_2.is_linked_to_previous is %s" % actual |
| 73 | + |
| 74 | + |
| 75 | +@then("header.is_linked_to_previous is {value}") |
| 76 | +def then_header_is_linked_to_previous_is_value(context, value): |
| 77 | + actual = context.header.is_linked_to_previous |
| 78 | + expected = eval(value) |
| 79 | + assert actual == expected, "header.is_linked_to_previous is %s" % actual |
| 80 | + |
| 81 | + |
| 82 | +@then("header_2.is_linked_to_previous is {value}") |
| 83 | +def then_header_2_is_linked_to_previous_is_value(context, value): |
| 84 | + actual = context.header_2.is_linked_to_previous |
| 85 | + expected = eval(value) |
| 86 | + assert actual == expected, "header_2.is_linked_to_previous is %s" % actual |
| 87 | + |
| 88 | + |
| 89 | +@then("header_2.paragraphs[0].text == header.paragraphs[0].text") |
| 90 | +def then_header_2_text_eq_header_text(context): |
| 91 | + actual = context.header_2.paragraphs[0].text |
| 92 | + expected = context.header.paragraphs[0].text |
| 93 | + assert actual == expected, "header_2.paragraphs[0].text == %s" % actual |
0 commit comments