1# encoding: utf-8
2
3"""Step implementations for header and footer-related features"""
4
5from __future__ import absolute_import, division, print_function, unicode_literals
6
7from behave import given, then, when
8
9from docx import Document
10
11from helpers import test_docx, test_file
12
13
14# given ====================================================
15
16@given("a _Footer object {with_or_no} footer definition as footer")
17def 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")
24def 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("a _Run object from a footer as run")
31def given_a_Run_object_from_a_footer_as_run(context):
32    footer = Document(test_docx("hdr-header-footer")).sections[0].footer
33    context.run = footer.paragraphs[0].add_run()
34
35
36@given("a _Run object from a header as run")
37def given_a_Run_object_from_a_header_as_run(context):
38    header = Document(test_docx("hdr-header-footer")).sections[0].header
39    context.run = header.paragraphs[0].add_run()
40
41
42@given("the next _Footer object with no footer definition as footer_2")
43def given_the_next_Footer_object_with_no_footer_definition(context):
44    context.footer_2 = context.sections[1].footer
45
46
47@given("the next _Header object with no header definition as header_2")
48def given_the_next_Header_object_with_no_header_definition(context):
49    context.header_2 = context.sections[1].header
50
51
52# when =====================================================
53
54@when("I assign \"Normal\" to footer.paragraphs[0].style")
55def when_I_assign_Body_Text_to_footer_style(context):
56    context.footer.paragraphs[0].style = "Normal"
57
58
59@when("I assign \"Normal\" to header.paragraphs[0].style")
60def when_I_assign_Body_Text_to_header_style(context):
61    context.header.paragraphs[0].style = "Normal"
62
63
64@when("I assign {value} to header.is_linked_to_previous")
65def when_I_assign_value_to_header_is_linked_to_previous(context, value):
66    context.header.is_linked_to_previous = eval(value)
67
68
69@when("I assign {value} to footer.is_linked_to_previous")
70def when_I_assign_value_to_footer_is_linked_to_previous(context, value):
71    context.footer.is_linked_to_previous = eval(value)
72
73
74@when("I call run.add_picture()")
75def when_I_call_run_add_picture(context):
76    context.run.add_picture(test_file("test.png"))
77
78
79# then =====================================================
80
81@then("footer.is_linked_to_previous is {value}")
82def then_footer_is_linked_to_previous_is_value(context, value):
83    actual = context.footer.is_linked_to_previous
84    expected = eval(value)
85    assert actual == expected, "footer.is_linked_to_previous is %s" % actual
86
87
88@then("footer.paragraphs[0].style.name == \"Normal\"")
89def then_footer_paragraphs_0_style_name_eq_Normal(context):
90    actual = context.footer.paragraphs[0].style.name
91    expected = "Normal"
92    assert actual == expected, "footer.paragraphs[0].style.name is %s" % actual
93
94
95@then("footer_2.is_linked_to_previous is {value}")
96def then_footer_2_is_linked_to_previous_is_value(context, value):
97    actual = context.footer_2.is_linked_to_previous
98    expected = eval(value)
99    assert actual == expected, "footer_2.is_linked_to_previous is %s" % actual
100
101
102@then("footer_2.paragraphs[0].text == footer.paragraphs[0].text")
103def then_footer_2_text_eq_footer_text(context):
104    actual = context.footer_2.paragraphs[0].text
105    expected = context.footer.paragraphs[0].text
106    assert actual == expected, "footer_2.paragraphs[0].text == %s" % actual
107
108
109@then("header.is_linked_to_previous is {value}")
110def then_header_is_linked_to_previous_is_value(context, value):
111    actual = context.header.is_linked_to_previous
112    expected = eval(value)
113    assert actual == expected, "header.is_linked_to_previous is %s" % actual
114
115
116@then("header.paragraphs[0].style.name == \"Normal\"")
117def then_header_paragraphs_0_style_name_eq_Normal(context):
118    actual = context.header.paragraphs[0].style.name
119    expected = "Normal"
120    assert actual == expected, "header.paragraphs[0].style.name is %s" % actual
121
122
123@then("header_2.is_linked_to_previous is {value}")
124def then_header_2_is_linked_to_previous_is_value(context, value):
125    actual = context.header_2.is_linked_to_previous
126    expected = eval(value)
127    assert actual == expected, "header_2.is_linked_to_previous is %s" % actual
128
129
130@then("header_2.paragraphs[0].text == header.paragraphs[0].text")
131def then_header_2_text_eq_header_text(context):
132    actual = context.header_2.paragraphs[0].text
133    expected = context.header.paragraphs[0].text
134    assert actual == expected, "header_2.paragraphs[0].text == %s" % actual
135
136
137@then("I can't detect the image but no exception is raised")
138def then_I_cant_detect_the_image_but_no_exception_is_raised(context):
139    pass
140