diff options
Diffstat (limited to 'tests/docx')
| -rw-r--r-- | tests/docx/body_xml_tests.py | 382 | ||||
| -rw-r--r-- | tests/docx/comments_xml_tests.py | 15 | ||||
| -rw-r--r-- | tests/docx/content_types_xml_tests.py | 15 | ||||
| -rw-r--r-- | tests/docx/document_xml_tests.py | 18 | ||||
| -rw-r--r-- | tests/docx/docx_tests.py | 88 | ||||
| -rw-r--r-- | tests/docx/files_tests.py | 25 | ||||
| -rw-r--r-- | tests/docx/notes_xml_tests.py | 14 | ||||
| -rw-r--r-- | tests/docx/numbering_xml_tests.py | 27 | ||||
| -rw-r--r-- | tests/docx/office_xml_tests.py | 8 | ||||
| -rw-r--r-- | tests/docx/relationships_xml_tests.py | 12 | ||||
| -rw-r--r-- | tests/docx/style_map_tests.py | 26 | ||||
| -rw-r--r-- | tests/docx/styles_xml_tests.py | 33 | ||||
| -rw-r--r-- | tests/docx/uris_tests.py | 9 | ||||
| -rw-r--r-- | tests/docx/xmlparser_tests.py | 54 |
14 files changed, 254 insertions, 472 deletions
diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index d7f5509..eade1e6 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -3,10 +3,9 @@ import io import sys -from precisely import assert_that, is_sequence -from nose.tools import istest, assert_equal -from nose_parameterized import parameterized, param import funk +from precisely import assert_that, is_sequence +import pytest from mammoth import documents, results from mammoth.docx.xmlparser import element as xml_element, text as xml_text @@ -23,27 +22,25 @@ from .document_matchers import ( is_table, is_row, ) +from ..testing import assert_equal if sys.version_info >= (3, ): unichr = chr -@istest -def text_from_text_element_is_read(): +def test_text_from_text_element_is_read(): element = _text_element("Hello!") assert_equal(documents.Text("Hello!"), _read_and_get_document_xml_element(element)) -@istest -def can_read_text_within_run(): +def test_can_read_text_within_run(): element = _run_element_with_text("Hello!") assert_equal( documents.run([documents.Text("Hello!")]), _read_and_get_document_xml_element(element) ) -@istest -def can_read_text_within_paragraph(): +def test_can_read_text_within_paragraph(): element = _paragraph_element_with_text("Hello!") assert_equal( documents.paragraph([documents.run([documents.Text("Hello!")])]), @@ -51,15 +48,12 @@ def can_read_text_within_paragraph(): ) -@istest class ParagraphTests(object): - @istest - def paragraph_has_no_style_if_it_has_no_properties(self): + def test_paragraph_has_no_style_if_it_has_no_properties(self): element = xml_element("w:p") assert_equal(None, _read_and_get_document_xml_element(element).style_id) - @istest - def paragraph_has_style_id_and_name_read_from_paragraph_properties_if_present(self): + def test_paragraph_has_style_id_and_name_read_from_paragraph_properties_if_present(self): style_xml = xml_element("w:pStyle", {"w:val": "Heading1"}) properties_xml = xml_element("w:pPr", {}, [style_xml]) paragraph_xml = xml_element("w:p", {}, [properties_xml]) @@ -72,8 +66,7 @@ class ParagraphTests(object): assert_equal("Heading1", paragraph.style_id) assert_equal("Heading 1", paragraph.style_name) - @istest - def warning_is_emitted_when_paragraph_style_cannot_be_found(self): + def test_warning_is_emitted_when_paragraph_style_cannot_be_found(self): style_xml = xml_element("w:pStyle", {"w:val": "Heading1"}) properties_xml = xml_element("w:pPr", {}, [style_xml]) paragraph_xml = xml_element("w:p", {}, [properties_xml]) @@ -84,27 +77,23 @@ class ParagraphTests(object): assert_equal(None, paragraph.style_name) assert_equal([results.warning("Paragraph style with ID Heading1 was referenced but not defined in the document")], result.messages) - @istest - def paragraph_has_no_justification_if_it_has_no_justificiation_properties(self): + def test_paragraph_has_no_justification_if_it_has_no_justificiation_properties(self): paragraph_xml = xml_element("w:p") paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal(None, paragraph.alignment) - @istest - def paragraph_has_justification_read_from_paragraph_properties_if_present(self): + def test_paragraph_has_justification_read_from_paragraph_properties_if_present(self): justification_xml = xml_element("w:jc", {"w:val": "center"}) properties_xml = xml_element("w:pPr", {}, [justification_xml]) paragraph_xml = xml_element("w:p", {}, [properties_xml]) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal("center", paragraph.alignment) - @istest - def paragraph_has_no_numbering_if_it_has_no_numbering_properties(self): + def test_paragraph_has_no_numbering_if_it_has_no_numbering_properties(self): element = xml_element("w:p") assert_equal(None, _read_and_get_document_xml_element(element).numbering) - @istest - def paragraph_has_numbering_properties_from_paragraph_properties_if_present(self): + def test_paragraph_has_numbering_properties_from_paragraph_properties_if_present(self): paragraph_xml = self._paragraph_with_numbering_properties([ xml_element("w:ilvl", {"w:val": "1"}), xml_element("w:numId", {"w:val": "42"}), @@ -116,8 +105,7 @@ class ParagraphTests(object): assert_equal("1", paragraph.numbering.level_index) assert_equal(True, paragraph.numbering.is_ordered) - @istest - def numbering_on_paragraph_style_takes_precedence_over_numpr(self): + def test_numbering_on_paragraph_style_takes_precedence_over_numpr(self): numbering_properties_xml = xml_element("w:numPr", {}, [ xml_element("w:ilvl", {"w:val": "1"}), xml_element("w:numId", {"w:val": "42"}), @@ -136,8 +124,7 @@ class ParagraphTests(object): assert_equal(True, paragraph.numbering.is_ordered) - @istest - def numbering_properties_are_ignored_if_lvl_is_missing(self): + def test_numbering_properties_are_ignored_if_lvl_is_missing(self): paragraph_xml = self._paragraph_with_numbering_properties([ xml_element("w:numId", {"w:val": "42"}), ]) @@ -147,8 +134,7 @@ class ParagraphTests(object): assert_equal(None, paragraph.numbering) - @istest - def numbering_properties_are_ignored_if_num_id_is_missing(self): + def test_numbering_properties_are_ignored_if_num_id_is_missing(self): paragraph_xml = self._paragraph_with_numbering_properties([ xml_element("w:ilvl", {"w:val": "1"}), ]) @@ -164,46 +150,38 @@ class ParagraphTests(object): return xml_element("w:p", {}, [properties_xml]) -@istest class ParagraphIndentTests(object): - @istest - def when_w_start_is_set_then_start_indent_is_read_from_w_start(self): + def test_when_w_start_is_set_then_start_indent_is_read_from_w_start(self): paragraph_xml = self._paragraph_with_indent({"w:start": "720", "w:left": "40"}) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal("720", paragraph.indent.start) - @istest - def when_w_start_is_not_set_then_start_indent_is_read_from_w_left(self): + def test_when_w_start_is_not_set_then_start_indent_is_read_from_w_left(self): paragraph_xml = self._paragraph_with_indent({"w:left": "720"}) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal("720", paragraph.indent.start) - @istest - def when_w_end_is_set_then_end_indent_is_read_from_w_end(self): + def test_when_w_end_is_set_then_end_indent_is_read_from_w_end(self): paragraph_xml = self._paragraph_with_indent({"w:end": "720", "w:right": "40"}) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal("720", paragraph.indent.end) - @istest - def when_w_end_is_not_set_then_end_indent_is_read_from_w_right(self): + def test_when_w_end_is_not_set_then_end_indent_is_read_from_w_right(self): paragraph_xml = self._paragraph_with_indent({"w:right": "720"}) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal("720", paragraph.indent.end) - @istest - def paragraph_has_indent_first_line_read_from_paragraph_properties_if_present(self): + def test_paragraph_has_indent_first_line_read_from_paragraph_properties_if_present(self): paragraph_xml = self._paragraph_with_indent({"w:firstLine": "720"}) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal("720", paragraph.indent.first_line) - @istest - def paragraph_has_indent_hanging_read_from_paragraph_properties_if_present(self): + def test_paragraph_has_indent_hanging_read_from_paragraph_properties_if_present(self): paragraph_xml = self._paragraph_with_indent({"w:hanging": "720"}) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal("720", paragraph.indent.hanging) - @istest - def when_indent_attributes_arent_set_then_indents_are_none(self): + def test_when_indent_attributes_arent_set_then_indents_are_none(self): paragraph_xml = self._paragraph_with_indent({}) paragraph = _read_and_get_document_xml_element(paragraph_xml) assert_equal(None, paragraph.indent.start) @@ -217,15 +195,12 @@ class ParagraphIndentTests(object): return xml_element("w:p", {}, [properties_xml]) -@istest class RunTests(object): - @istest - def run_has_no_style_if_it_has_no_properties(self): + def test_run_has_no_style_if_it_has_no_properties(self): element = xml_element("w:r") assert_equal(None, _read_and_get_document_xml_element(element).style_id) - @istest - def run_has_style_id_and_name_read_from_run_properties_if_present(self): + def test_run_has_style_id_and_name_read_from_run_properties_if_present(self): style_xml = xml_element("w:rStyle", {"w:val": "Heading1Char"}) styles = Styles.create( @@ -236,8 +211,7 @@ class RunTests(object): assert_equal("Heading1Char", run.style_id) assert_equal("Heading 1 Char", run.style_name) - @istest - def warning_is_emitted_when_run_style_cannot_be_found(self): + def test_warning_is_emitted_when_run_style_cannot_be_found(self): style_xml = xml_element("w:rStyle", {"w:val": "Heading1Char"}) properties_xml = xml_element("w:rPr", {}, [style_xml]) run_xml = xml_element("w:r", {}, [properties_xml]) @@ -249,139 +223,118 @@ class RunTests(object): assert_equal([results.warning("Run style with ID Heading1Char was referenced but not defined in the document")], result.messages) - @istest - def run_is_not_bold_if_bold_element_is_not_present(self): + def test_run_is_not_bold_if_bold_element_is_not_present(self): run = self._read_run_with_properties([]) assert_equal(False, run.is_bold) - @istest - def run_is_bold_if_bold_element_is_present(self): + def test_run_is_bold_if_bold_element_is_present(self): run = self._read_run_with_properties([xml_element("w:b")]) assert_equal(True, run.is_bold) - @istest - def run_is_not_italic_if_italic_element_is_not_present(self): + def test_run_is_not_italic_if_italic_element_is_not_present(self): run = self._read_run_with_properties([]) assert_equal(False, run.is_italic) - @istest - def run_is_italic_if_italic_element_is_present(self): + def test_run_is_italic_if_italic_element_is_present(self): run = self._read_run_with_properties([xml_element("w:i")]) assert_equal(True, run.is_italic) - @istest - def run_is_not_underlined_if_underline_element_is_not_present(self): + def test_run_is_not_underlined_if_underline_element_is_not_present(self): run = self._read_run_with_properties([]) assert_equal(False, run.is_underline) - @istest - def run_is_not_underlined_if_underline_element_is_present_without_val_attribute(self): + def test_run_is_not_underlined_if_underline_element_is_present_without_val_attribute(self): run = self._read_run_with_properties([xml_element("w:u")]) assert_equal(False, run.is_underline) - @istest - def run_is_not_underlined_if_underline_element_is_present_and_val_is_false(self): + def test_run_is_not_underlined_if_underline_element_is_present_and_val_is_false(self): run = self._read_run_with_properties([xml_element("w:u", {"w:val": "false"})]) assert_equal(False, run.is_underline) - @istest - def run_is_not_underlined_if_underline_element_is_present_and_val_is_0(self): + def test_run_is_not_underlined_if_underline_element_is_present_and_val_is_0(self): run = self._read_run_with_properties([xml_element("w:u", {"w:val": "0"})]) assert_equal(False, run.is_underline) - @istest - def run_is_not_underlined_if_underline_element_is_present_and_val_is_none(self): + def test_run_is_not_underlined_if_underline_element_is_present_and_val_is_none(self): run = self._read_run_with_properties([xml_element("w:u", {"w:val": "none"})]) assert_equal(False, run.is_underline) - @istest - def run_is_underlined_if_underline_element_is_present_and_val_is_not_none_or_falsy(self): + def test_run_is_underlined_if_underline_element_is_present_and_val_is_not_none_or_falsy(self): run = self._read_run_with_properties([xml_element("w:u", {"w:val": "single"})]) assert_equal(True, run.is_underline) - @istest - def run_is_not_struckthrough_if_strikethrough_element_is_not_present(self): + def test_run_is_not_struckthrough_if_strikethrough_element_is_not_present(self): run = self._read_run_with_properties([]) assert_equal(False, run.is_strikethrough) - @istest - def run_is_struckthrough_if_strikethrough_element_is_present(self): + def test_run_is_struckthrough_if_strikethrough_element_is_present(self): run = self._read_run_with_properties([xml_element("w:strike")]) assert_equal(True, run.is_strikethrough) - @istest - def run_is_not_small_caps_if_small_caps_element_is_not_present(self): + def test_run_is_not_small_caps_if_small_caps_element_is_not_present(self): run = self._read_run_with_properties([]) assert_equal(False, run.is_small_caps) - @istest - def run_is_small_caps_if_small_caps_element_is_present(self): + def test_run_is_small_caps_if_small_caps_element_is_present(self): run = self._read_run_with_properties([xml_element("w:smallCaps")]) assert_equal(True, run.is_small_caps) - run_boolean_property_test = lambda func: parameterized([ - param(attr_name="is_bold", tag_name="w:b"), - param(attr_name="is_underline", tag_name="w:u"), - param(attr_name="is_italic", tag_name="w:i"), - param(attr_name="is_strikethrough", tag_name="w:strike"), - param(attr_name="is_all_caps", tag_name="w:caps"), - param(attr_name="is_small_caps", tag_name="w:smallCaps"), - ])(istest(func)) - - @run_boolean_property_test - def run_boolean_property_is_false_if_element_is_present_and_val_is_false(self, attr_name, tag_name): + run_boolean_property_test_params = [ + ("is_bold", "w:b"), + ("is_underline", "w:u"), + ("is_italic", "w:i"), + ("is_strikethrough", "w:strike"), + ("is_all_caps", "w:caps"), + ("is_small_caps", "w:smallCaps"), + ] + + @pytest.mark.parametrize("attr_name, tag_name", run_boolean_property_test_params) + def test_run_boolean_property_is_false_if_element_is_present_and_val_is_false(self, attr_name, tag_name): run = self._read_run_with_properties([xml_element(tag_name, {"w:val": "false"})]) assert_equal(False, getattr(run, attr_name)) - @run_boolean_property_test - def run_boolean_property_is_false_if_element_is_present_and_val_is_0(self, attr_name, tag_name): + @pytest.mark.parametrize("attr_name, tag_name", run_boolean_property_test_params) + def test_run_boolean_property_is_false_if_element_is_present_and_val_is_0(self, attr_name, tag_name): run = self._read_run_with_properties([xml_element(tag_name, {"w:val": "0"})]) assert_equal(False, getattr(run, attr_name)) - @run_boolean_property_test - def run_boolean_property_is_true_if_element_is_present_and_val_is_true(self, attr_name, tag_name): + @pytest.mark.parametrize("attr_name, tag_name", run_boolean_property_test_params) + def test_run_boolean_property_is_true_if_element_is_present_and_val_is_true(self, attr_name, tag_name): run = self._read_run_with_properties([xml_element(tag_name, {"w:val": "true"})]) assert_equal(True, getattr(run, attr_name)) - @run_boolean_property_test - def run_boolean_property_is_true_if_element_is_present_and_val_is_1(self, attr_name, tag_name): + @pytest.mark.parametrize("attr_name, tag_name", run_boolean_property_test_params) + def test_run_boolean_property_is_true_if_element_is_present_and_val_is_1(self, attr_name, tag_name): run = self._read_run_with_properties([xml_element(tag_name, {"w:val": "1"})]) assert_equal(True, getattr(run, attr_name)) - @istest - def run_has_baseline_vertical_alignment_if_vertical_alignment_element_is_not_present(self): + def test_run_has_baseline_vertical_alignment_if_vertical_alignment_element_is_not_present(self): run = self._read_run_with_properties([]) assert_equal(documents.VerticalAlignment.baseline, run.vertical_alignment) - @istest - def run_has_vertical_alignment_read_from_vertical_alignment_element(self): + def test_run_has_vertical_alignment_read_from_vertical_alignment_element(self): run = self._read_run_with_properties([xml_element("w:vertAlign", {"w:val": "superscript"})]) assert_equal(documents.VerticalAlignment.superscript, run.vertical_alignment) - @istest - def run_has_none_font_by_default(self): + def test_run_has_none_font_by_default(self): run = self._read_run_with_properties([]) assert_equal(None, run.font) - @istest - def run_has_font_read_from_properties(self): + def test_run_has_font_read_from_properties(self): font_xml = xml_element("w:rFonts", {"w:ascii": "Arial"}) run = self._read_run_with_properties([font_xml]) assert_equal("Arial", run.font) - @istest - def run_has_none_font_size_by_default(self): + def test_run_has_none_font_size_by_default(self): run = self._read_run_with_properties([]) assert_equal(None, run.font_size) - @istest - def run_has_font_size_read_from_properties(self): + def test_run_has_font_size_read_from_properties(self): font_size_xml = xml_element("w:sz", {"w:val": "28"}) run = self._read_run_with_properties([font_size_xml]) assert_equal(14, run.font_size) - @istest - def run_with_invalid_w_sz_has_none_font_size(self): + def test_run_with_invalid_w_sz_has_none_font_size(self): font_size_xml = xml_element("w:sz", {"w:val": "28a"}) run = self._read_run_with_properties([font_size_xml]) assert_equal(None, run.font_size) @@ -392,7 +345,6 @@ class RunTests(object): return _read_and_get_document_xml_element(run_xml, styles=styles) -@istest class ComplexFieldTests(object): _URI = "http://example.com" _BEGIN_COMPLEX_FIELD = xml_element("w:r", {}, [ @@ -417,8 +369,7 @@ class ComplexFieldTests(object): def _is_empty_hyperlinked_run(self): return self._is_hyperlinked_run(children=[]) - @istest - def runs_in_a_complex_field_for_hyperlinks_without_switch_are_read_as_external_hyperlinks(self): + def test_runs_in_a_complex_field_for_hyperlinks_without_switch_are_read_as_external_hyperlinks(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, @@ -440,8 +391,7 @@ class ComplexFieldTests(object): is_empty_run, ))) - @istest - def runs_in_a_complex_field_for_hyperlinks_with_l_switch_are_read_as_internal_hyperlinks(self): + def test_runs_in_a_complex_field_for_hyperlinks_with_l_switch_are_read_as_internal_hyperlinks(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, xml_element("w:instrText", {}, [ @@ -465,8 +415,7 @@ class ComplexFieldTests(object): is_empty_run, ))) - @istest - def runs_after_a_complex_field_for_hyperlinks_are_not_read_as_hyperlinks(self): + def test_runs_after_a_complex_field_for_hyperlinks_are_not_read_as_hyperlinks(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, @@ -485,8 +434,7 @@ class ComplexFieldTests(object): )), ))) - @istest - def can_handle_split_instr_text_elements(self): + def test_can_handle_split_instr_text_elements(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, xml_element("w:instrText", {}, [ @@ -513,8 +461,7 @@ class ComplexFieldTests(object): is_empty_run, ))) - @istest - def hyperlink_is_not_ended_by_end_of_nested_complex_field(self): + def test_hyperlink_is_not_ended_by_end_of_nested_complex_field(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, @@ -545,8 +492,7 @@ class ComplexFieldTests(object): is_empty_run, ))) - @istest - def complex_field_nested_within_a_hyperlink_complex_field_is_wrapped_with_the_hyperlink(self): + def test_complex_field_nested_within_a_hyperlink_complex_field_is_wrapped_with_the_hyperlink(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, @@ -577,8 +523,7 @@ class ComplexFieldTests(object): is_empty_run, ))) - @istest - def field_without_separate_fld_char_is_ignored(self): + def test_field_without_separate_fld_char_is_ignored(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, @@ -605,43 +550,37 @@ class ComplexFieldTests(object): ))) -@istest -def can_read_tab_element(): +def test_can_read_tab_element(): element = xml_element("w:tab") tab = _read_and_get_document_xml_element(element) assert_equal(documents.tab(), tab) -@istest -def no_break_hyphen_element_is_read_as_non_breaking_hyphen_character(): +def test_no_break_hyphen_element_is_read_as_non_breaking_hyphen_character(): element = xml_element("w:noBreakHyphen") tab = _read_and_get_document_xml_element(element) assert_equal(documents.text(unichr(0x2011)), tab) -@istest -def soft_hyphen_element_is_read_as_soft_hyphen_character(): +def test_soft_hyphen_element_is_read_as_soft_hyphen_character(): element = xml_element("w:softHyphen") tab = _read_and_get_document_xml_element(element) assert_equal(documents.text(u"\u00ad"), tab) -@istest -def w_sym_with_supported_font_and_supported_code_point_in_ascii_range_is_converted_to_text(): +def test_w_sym_with_supported_font_and_supported_code_point_in_ascii_range_is_converted_to_text(): element = xml_element("w:sym", {"w:font": "Wingdings", "w:char": "28"}) text = _read_and_get_document_xml_element(element) assert_equal(documents.text(u"🕿"), text) -@istest -def w_sym_with_supported_font_and_supported_code_point_in_private_use_area_is_converted_to_text(): +def test_w_sym_with_supported_font_and_supported_code_point_in_private_use_area_is_converted_to_text(): element = xml_element("w:sym", {"w:font": "Wingdings", "w:char": "F028"}) text = _read_and_get_document_xml_element(element) assert_equal(documents.text(u"🕿"), text) -@istest -def w_sym_with_unsupported_font_and_code_point_produces_empty_result_with_warning(): +def test_w_sym_with_unsupported_font_and_code_point_produces_empty_result_with_warning(): element = xml_element("w:sym", {"w:font": "Dingwings", "w:char": "28"}) result = _read_document_xml_element(element) @@ -651,10 +590,8 @@ def w_sym_with_unsupported_font_and_code_point_produces_empty_result_with_warnin assert_equal(None, result.value) -@istest class TableTests(object): - @istest - def word_table_is_read_as_document_table_element(self): + def test_word_table_is_read_as_document_table_element(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:tc", {}, [ @@ -672,13 +609,11 @@ class TableTests(object): ]) assert_equal(expected_result, table) - @istest - def table_has_no_style_if_it_has_no_properties(self): + def test_table_has_no_style_if_it_has_no_properties(self): element = xml_element("w:tbl") assert_equal(None, _read_and_get_document_xml_element(element).style_id) - @istest - def table_has_style_id_and_name_read_from_paragraph_properties_if_present(self): + def test_table_has_style_id_and_name_read_from_paragraph_properties_if_present(self): style_xml = xml_element("w:tblStyle", {"w:val": "TableNormal"}) properties_xml = xml_element("w:tblPr", {}, [style_xml]) table_xml = xml_element("w:tbl", {}, [properties_xml]) @@ -691,8 +626,7 @@ class TableTests(object): assert_equal("TableNormal", paragraph.style_id) assert_equal("Normal Table", paragraph.style_name) - @istest - def warning_is_emitted_when_table_style_cannot_be_found(self): + def test_warning_is_emitted_when_table_style_cannot_be_found(self): style_xml = xml_element("w:tblStyle", {"w:val": "TableNormal"}) properties_xml = xml_element("w:tblPr", {}, [style_xml]) table_xml = xml_element("w:tbl", {}, [properties_xml]) @@ -703,8 +637,7 @@ class TableTests(object): assert_equal(None, table.style_name) assert_equal([results.warning("Table style with ID TableNormal was referenced but not defined in the document")], result.messages) - @istest - def tbl_header_marks_table_row_as_header(self): + def test_tbl_header_marks_table_row_as_header(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:trPr", {}, [ @@ -722,8 +655,7 @@ class TableTests(object): )) - @istest - def gridspan_is_read_as_colspan_for_table_cell(self): + def test_gridspan_is_read_as_colspan_for_table_cell(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:tc", {}, [ @@ -745,8 +677,7 @@ class TableTests(object): assert_equal(expected_result, table) - @istest - def vmerge_is_read_as_rowspan_for_table_cell(self): + def test_vmerge_is_read_as_rowspan_for_table_cell(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc()), w_tr(w_tc(properties=[w_vmerge("restart")])), @@ -765,8 +696,7 @@ class TableTests(object): assert_equal(expected_result, result) - @istest - def vmerge_without_val_is_treated_as_continue(self): + def test_vmerge_without_val_is_treated_as_continue(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(properties=[w_vmerge(None)])), @@ -779,8 +709,7 @@ class TableTests(object): assert_equal(expected_result, result) - @istest - def vmerge_accounts_for_cells_spanning_columns(self): + def test_vmerge_accounts_for_cells_spanning_columns(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc(), w_tc(), w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(properties=[w_gridspan("2")]), w_tc(properties=[w_vmerge("continue")])), @@ -797,8 +726,7 @@ class TableTests(object): assert_equal(expected_result, result) - @istest - def no_vertical_cell_merging_if_merged_cells_do_not_line_up(self): + def test_no_vertical_cell_merging_if_merged_cells_do_not_line_up(self): element = xml_element("w:tbl", {}, [ w_tr(w_tc(properties=[w_gridspan("2")]), w_tc(properties=[w_vmerge("restart")])), w_tr(w_tc(), w_tc(properties=[w_vmerge("continue")])), @@ -811,42 +739,35 @@ class TableTests(object): assert_equal(expected_result, result) - @istest - def warning_if_non_row_in_table(self): + def test_warning_if_non_row_in_table(self): element = xml_element("w:tbl", {}, [xml_element("w:p")]) result = _read_document_xml_element(element) expected_warning = results.warning("unexpected non-row element in table, cell merging may be incorrect") assert_equal([expected_warning], result.messages) - @istest - def warning_if_non_cell_in_table_row(self): + def test_warning_if_non_cell_in_table_row(self): element = xml_element("w:tbl", {}, [w_tr(xml_element("w:p"))]) result = _read_document_xml_element(element) expected_warning = results.warning("unexpected non-cell element in table row, cell merging may be incorrect") assert_equal([expected_warning], result.messages) -@istest -def children_of_w_ins_are_converted_normally(): +def test_children_of_w_ins_are_converted_normally(): _assert_children_are_converted_normally("w:ins") -@istest -def children_of_w_object_are_converted_normally(): +def test_children_of_w_object_are_converted_normally(): _assert_children_are_converted_normally("w:object") -@istest -def children_of_w_smart_tag_are_converted_normally(): +def test_children_of_w_smart_tag_are_converted_normally(): _assert_children_are_converted_normally("w:smartTag") -@istest -def children_of_v_group_are_converted_normally(): +def test_children_of_v_group_are_converted_normally(): _assert_children_are_converted_normally("v:group") -@istest -def children_of_v_rect_are_converted_normally(): +def test_children_of_v_rect_are_converted_normally(): _assert_children_are_converted_normally("v:rect") @@ -862,10 +783,8 @@ def _assert_children_are_converted_normally(tag_name): ) -@istest class HyperlinkTests(object): - @istest - def hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id(self): + def test_hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id(self): relationships = Relationships([ _hyperlink_relationship("r42", "http://example.com"), ]) @@ -876,8 +795,7 @@ class HyperlinkTests(object): _read_and_get_document_xml_element(element, relationships=relationships) ) - @istest - def hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id_and_an_anchor(self): + def test_hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id_and_an_anchor(self): relationships = Relationships([ _hyperlink_relationship("r42", "http://example.com/"), ]) @@ -888,8 +806,7 @@ class HyperlinkTests(object): _read_and_get_document_xml_element(element, relationships=relationships) ) - @istest - def existing_fragment_is_replaced_when_anchor_is_set_on_external_link(self): + def test_existing_fragment_is_replaced_when_anchor_is_set_on_external_link(self): relationships = Relationships([ _hyperlink_relationship("r42", "http://example.com/#previous"), ]) @@ -900,8 +817,7 @@ class HyperlinkTests(object): _read_and_get_document_xml_element(element, relationships=relationships) ) - @istest - def hyperlink_is_read_as_internal_hyperlink_if_it_has_an_anchor_attribute(self): + def test_hyperlink_is_read_as_internal_hyperlink_if_it_has_an_anchor_attribute(self): run_element = xml_element("w:r") element = xml_element("w:hyperlink", {"w:anchor": "start"}, [run_element]) assert_equal( @@ -909,8 +825,7 @@ class HyperlinkTests(object): _read_and_get_document_xml_element(element) ) - @istest - def hyperlink_is_ignored_if_it_does_not_have_a_relationship_id_nor_anchor(self): + def test_hyperlink_is_ignored_if_it_does_not_have_a_relationship_id_nor_anchor(self): run_element = xml_element("w:r") element = xml_element("w:hyperlink", {}, [run_element]) assert_equal( @@ -918,8 +833,7 @@ class HyperlinkTests(object): _read_and_get_document_xml_element(element) ) - @istest - def target_frame_is_read(self): + def test_target_frame_is_read(self): element = xml_element("w:hyperlink", { "w:anchor": "start", "w:tgtFrame": "_blank", @@ -929,8 +843,7 @@ class HyperlinkTests(object): is_hyperlink(target_frame="_blank"), ) - @istest - def empty_target_frame_is_ignored(self): + def test_empty_target_frame_is_ignored(self): element = xml_element("w:hyperlink", { "w:anchor": "start", "w:tgtFrame": "", @@ -941,15 +854,12 @@ class HyperlinkTests(object): ) -@istest class BookmarkTests(object): - @istest - def go_back_bookmark_is_ignored(self): + def test_go_back_bookmark_is_ignored(self): element = xml_element("w:bookmarkStart", {"w:name": "_GoBack"}) assert_equal(None, _read_and_get_document_xml_element(element)) - @istest - def bookmark_start_is_read_if_name_is_not_go_back(self): + def test_bookmark_start_is_read_if_name_is_not_go_back(self): element = xml_element("w:bookmarkStart", {"w:name": "start"}) assert_equal( documents.bookmark("start"), @@ -957,34 +867,28 @@ class BookmarkTests(object): ) -@istest class BreakTests(object): - @istest - def br_without_explicit_type_is_read_as_line_break(self): + def test_br_without_explicit_type_is_read_as_line_break(self): break_element = xml_element("w:br", {}, []) result = _read_document_xml_element(break_element) assert_equal(documents.line_break, result.value) - @istest - def br_with_text_wrapping_type_is_read_as_line_break(self): + def test_br_with_text_wrapping_type_is_read_as_line_break(self): break_element = xml_element("w:br", {"w:type": "textWrapping"}, []) result = _read_document_xml_element(break_element) assert_equal(documents.line_break, result.value) - @istest - def br_with_page_type_is_read_as_page_break(self): + def test_br_with_page_type_is_read_as_page_break(self): break_element = xml_element("w:br", {"w:type": "page"}, []) result = _read_document_xml_element(break_element) assert_equal(documents.page_break, result.value) - @istest - def br_with_column_type_is_read_as_column_break(self): + def test_br_with_column_type_is_read_as_column_break(self): break_element = xml_element("w:br", {"w:type": "column"}, []) result = _read_document_xml_element(break_element) assert_equal(documents.column_break, result.value) - @istest - def warning_on_break_types_that_arent_recognised(self): + def test_warning_on_break_types_that_arent_recognised(self): break_element = xml_element("w:br", {"w:type": "unknownBreakType"}, []) result = _read_document_xml_element(break_element) expected_warning = results.warning("Unsupported break type: unknownBreakType") @@ -992,7 +896,6 @@ class BreakTests(object): assert_equal(None, result.value) -@istest class ImageTests(object): IMAGE_BYTES = b"Not an image at all!" IMAGE_RELATIONSHIP_ID = "rId5" @@ -1016,8 +919,7 @@ class ImageTests(object): docx_file=docx_file, ) - @istest - def can_read_imagedata_elements_with_rid_attribute(self): + def test_can_read_imagedata_elements_with_rid_attribute(self): imagedata_element = xml_element("v:imagedata", { "r:id": self.IMAGE_RELATIONSHIP_ID, "o:title": "It's a hat" @@ -1031,8 +933,7 @@ class ImageTests(object): with image.open() as image_file: assert_equal(self.IMAGE_BYTES, image_file.read()) - @istest - def when_imagedata_element_has_no_relationship_id_then_it_is_ignored_with_warning(self): + def test_when_imagedata_element_has_no_relationship_id_then_it_is_ignored_with_warning(self): imagedata_element = xml_element("v:imagedata") result = _read_document_xml_element(imagedata_element) @@ -1041,8 +942,7 @@ class ImageTests(object): assert_equal(None, result.value) - @istest - def can_read_inline_pictures(self): + def test_can_read_inline_pictures(self): drawing_element = _create_inline_image( blip=_embedded_blip(self.IMAGE_RELATIONSHIP_ID), description="It's a hat", @@ -1057,8 +957,7 @@ class ImageTests(object): assert_equal(self.IMAGE_BYTES, image_file.read()) - @istest - def alt_text_title_is_used_if_alt_text_description_is_missing(self): + def test_alt_text_title_is_used_if_alt_text_description_is_missing(self): drawing_element = _create_inline_image( blip=_embedded_blip(self.IMAGE_RELATIONSHIP_ID), title="It's a hat", @@ -1073,8 +972,7 @@ class ImageTests(object): assert_equal(self.IMAGE_BYTES, image_file.read()) - @istest - def alt_text_title_is_used_if_alt_text_description_is_blank(self): + def test_alt_text_title_is_used_if_alt_text_description_is_blank(self): drawing_element = _create_inline_image( blip=_embedded_blip(self.IMAGE_RELATIONSHIP_ID), description=" ", @@ -1090,8 +988,7 @@ class ImageTests(object): assert_equal(self.IMAGE_BYTES, image_file.read()) - @istest - def alt_text_description_is_preferred_to_alt_text_title(self): + def test_alt_text_description_is_preferred_to_alt_text_title(self): drawing_element = _create_inline_image( blip=_embedded_blip(self.IMAGE_RELATIONSHIP_ID), description="It's a hat", @@ -1106,8 +1003,7 @@ class ImageTests(object): with image.open() as image_file: assert_equal(self.IMAGE_BYTES, image_file.read()) - @istest - def can_read_anchored_pictures(self): + def test_can_read_anchored_pictures(self): drawing_element = _create_anchored_image( blip=_embedded_blip(self.IMAGE_RELATIONSHIP_ID), description="It's a hat", @@ -1121,10 +1017,7 @@ class ImageTests(object): with image.open() as image_file: assert_equal(self.IMAGE_BYTES, image_file.read()) - - @istest - @funk.with_mocks - def can_read_linked_pictures(self, mocks): + def test_can_read_linked_pictures(self, mocks): drawing_element = _create_inline_image( blip=_linked_blip("rId5"), description="It's a hat", @@ -1153,8 +1046,7 @@ class ImageTests(object): with image.open() as image_file: assert_equal(self.IMAGE_BYTES, image_file.read()) - @istest - def warning_if_blip_has_no_image_file(self): + def test_warning_if_blip_has_no_image_file(self): drawing = _create_inline_image( blip=xml_element("a:blip"), description="It's a hat", @@ -1165,9 +1057,7 @@ class ImageTests(object): assert_equal([results.warning("Could not find image file for a:blip element")], result.messages) assert_equal(None, result.value) - @istest - @funk.with_mocks - def warning_if_unsupported_image_type(self, mocks): + def test_warning_if_unsupported_image_type(self, mocks): drawing_element = _create_inline_image( blip=_embedded_blip("rId5"), description="It's a hat", @@ -1193,8 +1083,7 @@ class ImageTests(object): expected_warning = results.warning("Image of type image/x-emf is unlikely to display in web browsers") assert_equal([expected_warning], result.messages) - @istest - def no_elements_created_if_image_cannot_be_found_in_w_drawing(self): + def test_no_elements_created_if_image_cannot_be_found_in_w_drawing(self): drawing = xml_element("w:drawing") result = _read_document_xml_element(drawing) @@ -1202,8 +1091,7 @@ class ImageTests(object): assert_equal([], result.messages) assert_equal(None, result.value) - @istest - def no_elements_created_if_image_cannot_be_found_in_wp_inline(self): + def test_no_elements_created_if_image_cannot_be_found_in_wp_inline(self): drawing = xml_element("wp:inline") result = _read_document_xml_element(drawing) @@ -1212,47 +1100,40 @@ class ImageTests(object): assert_equal(None, result.value) -@istest -def footnote_reference_has_id_read(): +def test_footnote_reference_has_id_read(): footnote_xml = xml_element("w:footnoteReference", {"w:id": "4"}) footnote = _read_and_get_document_xml_element(footnote_xml) assert_equal("4", footnote.note_id) -@istest -def comment_reference_has_id_read(): +def test_comment_reference_has_id_read(): comment_reference_xml = xml_element("w:commentReference", {"w:id": "4"}) comment_reference = _read_and_get_document_xml_element(comment_reference_xml) assert_equal(documents.CommentReference("4"), comment_reference) -@istest -def ignored_elements_are_ignored_without_message(): +def test_ignored_elements_are_ignored_without_message(): element = xml_element("w:bookmarkEnd") result = _read_document_xml_element(element) assert_equal(None, result.value) assert_equal([], result.messages) -@istest -def unrecognised_elements_emit_warning(): +def test_unrecognised_elements_emit_warning(): element = xml_element("w:huh", {}, []) result = _read_document_xml_element(element) expected_warning = results.warning("An unrecognised element was ignored: w:huh") assert_equal([expected_warning], result.messages) -@istest -def unrecognised_elements_are_ignored(): +def test_unrecognised_elements_are_ignored(): element = xml_element("w:huh", {}, []) assert_equal(None, _read_document_xml_element(element).value) -@istest -def unrecognised_children_are_ignored(): +def test_unrecognised_children_are_ignored(): element = xml_element("w:r", {}, [_text_element("Hello!"), xml_element("w:huh", {}, [])]) assert_equal( documents.run([documents.Text("Hello!")]), _read_document_xml_element(element).value ) -@istest -def text_boxes_have_content_appended_after_containing_paragraph(): +def test_text_boxes_have_content_appended_after_containing_paragraph(): text_box = xml_element("w:pict", {}, [ xml_element("v:shape", {}, [ xml_element("v:textbox", {}, [ @@ -1268,8 +1149,7 @@ def text_boxes_have_content_appended_after_containing_paragraph(): result = _read_and_get_document_xml_elements(paragraph) assert_equal(result[1].style_id, "textbox-content") -@istest -def alternate_content_is_read_using_fallback(): +def test_alternate_content_is_read_using_fallback(): element = xml_element("mc:AlternateContent", {}, [ xml_element("mc:Choice", {"Requires": "wps"}, [ _paragraph_with_style_id("first") @@ -1281,8 +1161,7 @@ def alternate_content_is_read_using_fallback(): result = _read_and_get_document_xml_element(element) assert_equal("second", result.style_id) -@istest -def sdt_is_read_using_sdt_content(): +def test_sdt_is_read_using_sdt_content(): element = xml_element("w:sdt", {}, [ xml_element("w:sdtContent", {}, [ xml_element("w:t", {}, [xml_text("Blackdown")]), @@ -1291,8 +1170,7 @@ def sdt_is_read_using_sdt_content(): result = _read_and_get_document_xml_element(element) assert_equal(documents.text("Blackdown"), result) -@istest -def text_nodes_are_ignored_when_reading_children(): +def test_text_nodes_are_ignored_when_reading_children(): element = xml_element("w:r", {}, [xml_text("[text]")]) assert_equal( documents.run([]), diff --git a/tests/docx/comments_xml_tests.py b/tests/docx/comments_xml_tests.py index 1d5dd8d..010185f 100644 --- a/tests/docx/comments_xml_tests.py +++ b/tests/docx/comments_xml_tests.py @@ -1,13 +1,11 @@ -from nose.tools import istest, assert_equal - from mammoth import documents from mammoth.docx.xmlparser import element as xml_element from mammoth.docx.comments_xml import read_comments_xml_element from mammoth.docx import body_xml +from ..testing import assert_equal -@istest -def id_and_body_of_comment_is_read(): +def test_id_and_body_of_comment_is_read(): body = [xml_element("w:p")] comments = read_comments_xml_element(xml_element("w:comments", {}, [ xml_element("w:comment", {"w:id": "1"}, body), @@ -17,8 +15,7 @@ def id_and_body_of_comment_is_read(): assert_equal("1", comments.value[0].comment_id) -@istest -def when_optional_attributes_of_comment_are_missing_then_they_are_read_as_none(): +def test_when_optional_attributes_of_comment_are_missing_then_they_are_read_as_none(): comments = read_comments_xml_element(xml_element("w:comments", {}, [ xml_element("w:comment", {"w:id": "1"}, []), ]), body_reader=body_xml.reader()) @@ -27,8 +24,7 @@ def when_optional_attributes_of_comment_are_missing_then_they_are_read_as_none() assert_equal(None, comment.author_initials) -@istest -def when_optional_attributes_of_comment_are_blank_then_they_are_read_as_none(): +def test_when_optional_attributes_of_comment_are_blank_then_they_are_read_as_none(): comments = read_comments_xml_element(xml_element("w:comments", {}, [ xml_element("w:comment", {"w:id": "1", "w:author": " ", "w:initials": " "}, []), ]), body_reader=body_xml.reader()) @@ -37,8 +33,7 @@ def when_optional_attributes_of_comment_are_blank_then_they_are_read_as_none(): assert_equal(None, comment.author_initials) -@istest -def when_optional_attributes_of_comment_are_not_blank_then_they_are_read(): +def test_when_optional_attributes_of_comment_are_not_blank_then_they_are_read(): comments = read_comments_xml_element(xml_element("w:comments", {}, [ xml_element("w:comment", {"w:id": "1", "w:author": "The Piemaker", "w:initials": "TP"}, []), ]), body_reader=body_xml.reader()) diff --git a/tests/docx/content_types_xml_tests.py b/tests/docx/content_types_xml_tests.py index 8b104d9..14920b4 100644 --- a/tests/docx/content_types_xml_tests.py +++ b/tests/docx/content_types_xml_tests.py @@ -1,11 +1,9 @@ -from nose.tools import istest, assert_equal - from mammoth.docx.xmlparser import element as xml_element from mammoth.docx.content_types_xml import read_content_types_xml_element +from ..testing import assert_equal -@istest -def content_type_is_based_on_default_for_extension_if_there_is_no_override(): +def test_content_type_is_based_on_default_for_extension_if_there_is_no_override(): element = xml_element("content-types:Types", {}, [ xml_element("content-types:Default", { "Extension": "png", @@ -19,8 +17,7 @@ def content_type_is_based_on_default_for_extension_if_there_is_no_override(): ) -@istest -def content_type_is_based_on_override_if_present(): +def test_content_type_is_based_on_override_if_present(): element = xml_element("content-types:Types", {}, [ xml_element("content-types:Default", { "Extension": "png", @@ -38,8 +35,7 @@ def content_type_is_based_on_override_if_present(): ) -@istest -def fallback_content_types_have_common_image_types(): +def test_fallback_content_types_have_common_image_types(): element = xml_element("content-types:Types", {}, []) content_types = read_content_types_xml_element(element) assert_equal( @@ -72,8 +68,7 @@ def fallback_content_types_have_common_image_types(): ) -@istest -def fallback_content_types_are_case_insensitive(): +def test_fallback_content_types_are_case_insensitive(): element = xml_element("content-types:Types", {}, []) content_types = read_content_types_xml_element(element) assert_equal( diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py index fa63775..e93392c 100644 --- a/tests/docx/document_xml_tests.py +++ b/tests/docx/document_xml_tests.py @@ -1,33 +1,29 @@ -from nose.tools import istest, assert_equal - from mammoth import documents from mammoth.docx.xmlparser import element as xml_element, text as xml_text from mammoth.docx.document_xml import read_document_xml_element from mammoth.docx import body_xml +from ..testing import assert_equal -@istest class ReadXmlElementTests(object): - @istest - def can_read_text_within_document(self): + def test_can_read_text_within_document(self): element = _document_element_with_text("Hello!") assert_equal( documents.document([documents.paragraph([documents.run([documents.Text("Hello!")])])]), _read_and_get_document_xml_element(element) ) - - @istest - def footnotes_of_document_are_read(self): + + def test_footnotes_of_document_are_read(self): notes = [documents.note("footnote", "4", [documents.paragraph([])])] - + body_xml = xml_element("w:body") document_xml = xml_element("w:document", {}, [body_xml]) - + document = _read_and_get_document_xml_element(document_xml, notes=notes) footnote = document.notes.find_note("footnote", "4") assert_equal("4", footnote.note_id) assert isinstance(footnote.body[0], documents.Paragraph) - + def _read_and_get_document_xml_element(*args, **kwargs): body_reader = body_xml.reader() diff --git a/tests/docx/docx_tests.py b/tests/docx/docx_tests.py index 830f334..ee6b928 100644 --- a/tests/docx/docx_tests.py +++ b/tests/docx/docx_tests.py @@ -2,17 +2,13 @@ import io import textwrap import zipfile -from nose.tools import istest, assert_equal - from mammoth import docx, documents, zips -from ..testing import assert_raises, test_path +from ..testing import assert_equal, assert_raises, generate_test_path -@istest class ReadTests(object): - @istest - def can_read_document_with_single_paragraph_with_single_run_of_text(self): - with open(test_path("single-paragraph.docx"), "rb") as fileobj: + def test_can_read_document_with_single_paragraph_with_single_run_of_text(self): + with open(generate_test_path("single-paragraph.docx"), "rb") as fileobj: result = docx.read(fileobj=fileobj) expected_document = documents.document([ documents.paragraph([ @@ -29,8 +25,7 @@ _relationship_namespaces = { } -@istest -def main_document_is_found_using_package_relationships(): +def test_main_document_is_found_using_package_relationships(): fileobj = _create_zip({ "word/document2.xml": textwrap.dedent("""\ <?xml version="1.0" encoding="utf-8" ?> @@ -62,8 +57,7 @@ def main_document_is_found_using_package_relationships(): assert_equal(expected_document, result.value) -@istest -def error_is_raised_when_main_document_part_does_not_exist(): +def test_error_is_raised_when_main_document_part_does_not_exist(): fileobj = _create_zip({ "_rels/.rels": textwrap.dedent("""\ <?xml version="1.0" encoding="utf-8"?> @@ -78,9 +72,8 @@ def error_is_raised_when_main_document_part_does_not_exist(): str(error), ) -class TestPartPaths(object): - @istest - def main_document_part_is_found_using_package_relationships(self): +class PartPathsTests(object): + def test_main_document_part_is_found_using_package_relationships(self): fileobj = _create_zip({ "word/document2.xml": " ", "_rels/.rels": textwrap.dedent("""\ @@ -92,55 +85,44 @@ class TestPartPaths(object): }) part_paths = self._find_part_paths(fileobj) assert_equal("word/document2.xml", part_paths.main_document) - - @istest - def when_relationship_for_main_document_cannot_be_found_then_fallback_is_used(self): + + def test_when_relationship_for_main_document_cannot_be_found_then_fallback_is_used(self): fileobj = _create_zip({ "word/document.xml": " ", }) part_paths = self._find_part_paths(fileobj) assert_equal("word/document.xml", part_paths.main_document) - - @istest - def comments_part_is_found_using_main_document_relationships(self): + + def test_comments_part_is_found_using_main_document_relationships(self): self._assert_path_is_found_using_main_document_relationships("comments") - - @istest - def when_relationship_for_comments_cannot_be_found_then_fallback_is_used(self): + + def test_when_relationship_for_comments_cannot_be_found_then_fallback_is_used(self): self._assert_when_relationship_for_part_cannot_be_found_then_fallback_is_used("comments") - - @istest - def endnotes_part_is_found_using_main_document_relationships(self): + + def test_endnotes_part_is_found_using_main_document_relationships(self): self._assert_path_is_found_using_main_document_relationships("endnotes") - - @istest - def when_relationship_for_endnotes_cannot_be_found_then_fallback_is_used(self): + + def test_when_relationship_for_endnotes_cannot_be_found_then_fallback_is_used(self): self._assert_when_relationship_for_part_cannot_be_found_then_fallback_is_used("endnotes") - - @istest - def footnotes_part_is_found_using_main_document_relationships(self): + + def test_footnotes_part_is_found_using_main_document_relationships(self): self._assert_path_is_found_using_main_document_relationships("footnotes") - - @istest - def when_relationship_for_footnotes_cannot_be_found_then_fallback_is_used(self): + + def test_when_relationship_for_footnotes_cannot_be_found_then_fallback_is_used(self): self._assert_when_relationship_for_part_cannot_be_found_then_fallback_is_used("footnotes") - - @istest - def numbering_part_is_found_using_main_document_relationships(self): + + def test_numbering_part_is_found_using_main_document_relationships(self): self._assert_path_is_found_using_main_document_relationships("numbering") - - @istest - def when_relationship_for_numbering_cannot_be_found_then_fallback_is_used(self): + + def test_when_relationship_for_numbering_cannot_be_found_then_fallback_is_used(self): self._assert_when_relationship_for_part_cannot_be_found_then_fallback_is_used("numbering") - - @istest - def styles_part_is_found_using_main_document_relationships(self): + + def test_styles_part_is_found_using_main_document_relationships(self): self._assert_path_is_found_using_main_document_relationships("styles") - - @istest - def when_relationship_for_styles_cannot_be_found_then_fallback_is_used(self): + + def test_when_relationship_for_styles_cannot_be_found_then_fallback_is_used(self): self._assert_when_relationship_for_part_cannot_be_found_then_fallback_is_used("styles") - + def _assert_path_is_found_using_main_document_relationships(self, name): fileobj = _create_zip({ "_rels/.rels": textwrap.dedent("""\ @@ -160,7 +142,7 @@ class TestPartPaths(object): }) part_paths = self._find_part_paths(fileobj) assert_equal("word/target-path.xml", getattr(part_paths, name)) - + def _assert_when_relationship_for_part_cannot_be_found_then_fallback_is_used(self, name): fileobj = _create_zip({ "_rels/.rels": textwrap.dedent("""\ @@ -173,21 +155,21 @@ class TestPartPaths(object): }) part_paths = self._find_part_paths(fileobj) assert_equal("word/{0}.xml".format(name), getattr(part_paths, name)) - - + + def _find_part_paths(self, fileobj): return docx._find_part_paths(zips.open_zip(fileobj, "r")) def _create_zip(files): fileobj = io.BytesIO() - + zip_file = zipfile.ZipFile(fileobj, "w") try: for name, contents in files.items(): zip_file.writestr(name, contents) finally: zip_file.close() - + fileobj.seek(0) return fileobj diff --git a/tests/docx/files_tests.py b/tests/docx/files_tests.py index f6c0120..6845cc2 100644 --- a/tests/docx/files_tests.py +++ b/tests/docx/files_tests.py @@ -1,12 +1,9 @@ -from nose.tools import istest, assert_equal - from mammoth.docx.files import Files, InvalidFileReferenceError -from ..testing import test_path, assert_raises +from ..testing import generate_test_path, assert_equal, assert_raises -@istest -def can_open_files_with_file_uri(): - path = test_path("tiny-picture.png") +def test_can_open_files_with_file_uri(): + path = generate_test_path("tiny-picture.png") files = Files(None) with files.open("file:///" + path) as image_file: contents = image_file.read() @@ -15,18 +12,16 @@ def can_open_files_with_file_uri(): assert_equal(source_file.read(), contents) -@istest -def can_open_files_with_relative_uri(): - files = Files(test_path("")) +def test_can_open_files_with_relative_uri(): + files = Files(generate_test_path("")) with files.open("tiny-picture.png") as image_file: contents = image_file.read() assert_equal(bytes, type(contents)) - with open(test_path("tiny-picture.png"), "rb") as source_file: + with open(generate_test_path("tiny-picture.png"), "rb") as source_file: assert_equal(source_file.read(), contents) -@istest -def given_base_is_not_set_when_opening_relative_uri_then_error_is_raised(): +def test_given_base_is_not_set_when_opening_relative_uri_then_error_is_raised(): files = Files(None) error = assert_raises(InvalidFileReferenceError, lambda: files.open("not-a-real-file.png")) expected_message = ( @@ -35,8 +30,7 @@ def given_base_is_not_set_when_opening_relative_uri_then_error_is_raised(): assert_equal(expected_message, str(error)) -@istest -def error_is_raised_if_relative_uri_cannot_be_opened(): +def test_error_is_raised_if_relative_uri_cannot_be_opened(): files = Files("/tmp") error = assert_raises(InvalidFileReferenceError, lambda: files.open("not-a-real-file.png")) expected_message = ( @@ -46,8 +40,7 @@ def error_is_raised_if_relative_uri_cannot_be_opened(): assert_equal(expected_message, str(error)) -@istest -def error_is_raised_if_file_uri_cannot_be_opened(): +def test_error_is_raised_if_file_uri_cannot_be_opened(): files = Files("/tmp") error = assert_raises(InvalidFileReferenceError, lambda: files.open("file:///not-a-real-file.png")) expected_message = "could not open external image: 'file:///not-a-real-file.png' (document directory: '/tmp')\n" diff --git a/tests/docx/notes_xml_tests.py b/tests/docx/notes_xml_tests.py index 56a7681..086e6d8 100644 --- a/tests/docx/notes_xml_tests.py +++ b/tests/docx/notes_xml_tests.py @@ -1,13 +1,11 @@ -from nose.tools import istest, assert_equal - from mammoth import documents from mammoth.docx.xmlparser import element as xml_element from mammoth.docx.notes_xml import read_footnotes_xml_element from mammoth.docx import body_xml +from ..testing import assert_equal -@istest -def id_and_body_of_footnote_are_read(): +def test_id_and_body_of_footnote_are_read(): footnote_body = [xml_element("w:p")] footnotes = read_footnotes_xml_element(xml_element("w:footnotes", {}, [ xml_element("w:footnote", {"w:id": "1"}, footnote_body), @@ -17,13 +15,11 @@ def id_and_body_of_footnote_are_read(): assert_equal("1", footnotes.value[0].note_id) -@istest -def continuation_separator_is_ignored(): +def test_continuation_separator_is_ignored(): _assert_footnote_type_is_ignored("continuationSeparator") -@istest -def separator_is_ignored(): +def test_separator_is_ignored(): _assert_footnote_type_is_ignored("separator") @@ -33,4 +29,4 @@ def _assert_footnote_type_is_ignored(footnote_type): xml_element("w:footnote", {"w:id": "1", "w:type": footnote_type}, footnote_body), ]), body_reader=None) assert_equal(0, len(footnotes.value)) - + diff --git a/tests/docx/numbering_xml_tests.py b/tests/docx/numbering_xml_tests.py index 6c1d843..b6959a6 100644 --- a/tests/docx/numbering_xml_tests.py +++ b/tests/docx/numbering_xml_tests.py @@ -1,12 +1,10 @@ -from nose.tools import istest, assert_equal - from mammoth.docx.xmlparser import element as xml_element from mammoth.docx.numbering_xml import read_numbering_xml_element from mammoth.docx.styles_xml import NumberingStyle, Styles +from ..testing import assert_equal -@istest -def find_level_returns_none_if_num_with_id_cannot_be_found(): +def test_find_level_returns_none_if_num_with_id_cannot_be_found(): numbering = _read_numbering_xml_element(xml_element("w:numbering")) assert_equal(None, numbering.find_level("47", "0")) @@ -26,32 +24,27 @@ _sample_numbering_xml = xml_element("w:numbering", {}, [ ]) -@istest -def level_includes_level_index(): +def test_level_includes_level_index(): numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal("0", numbering.find_level("47", "0").level_index) assert_equal("1", numbering.find_level("47", "1").level_index) -@istest -def list_is_not_ordered_if_formatted_as_bullet(): +def test_list_is_not_ordered_if_formatted_as_bullet(): numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal(False, numbering.find_level("47", "0").is_ordered) -@istest -def list_is_ordered_if_formatted_as_decimal(): +def test_list_is_ordered_if_formatted_as_decimal(): numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal(True, numbering.find_level("47", "1").is_ordered) -@istest -def find_level_returns_none_if_level_cannot_be_found(): +def test_find_level_returns_none_if_level_cannot_be_found(): numbering = _read_numbering_xml_element(_sample_numbering_xml) assert_equal(None, numbering.find_level("47", "2")) -@istest -def num_referencing_non_existent_abstract_num_is_ignored(): +def test_num_referencing_non_existent_abstract_num_is_ignored(): element = xml_element("w:numbering", {}, [ xml_element("w:num", {"w:numId": "47"}, [ xml_element("w:abstractNumId", {"w:val": "42"}) @@ -63,8 +56,7 @@ def num_referencing_non_existent_abstract_num_is_ignored(): assert_equal(None, numbering.find_level("47", "0")) -@istest -def when_abstract_num_has_num_style_link_then_style_is_used_to_find_num(): +def test_when_abstract_num_has_num_style_link_then_style_is_used_to_find_num(): numbering = _read_numbering_xml_element( xml_element("w:numbering", {}, [ xml_element("w:abstractNum", {"w:abstractNumId": "100"}, [ @@ -88,8 +80,7 @@ def when_abstract_num_has_num_style_link_then_style_is_used_to_find_num(): # See: 17.9.23 pStyle (Paragraph Style's Associated Numbering Level) in ECMA-376, 4th Edition -@istest -def numbering_level_can_be_found_by_paragraph_style_id(): +def test_numbering_level_can_be_found_by_paragraph_style_id(): numbering = _read_numbering_xml_element( xml_element("w:numbering", {}, [ xml_element("w:abstractNum", {"w:abstractNumId": "42"}, [ diff --git a/tests/docx/office_xml_tests.py b/tests/docx/office_xml_tests.py index 5b8b1e1..f87fa75 100644 --- a/tests/docx/office_xml_tests.py +++ b/tests/docx/office_xml_tests.py @@ -2,13 +2,11 @@ from __future__ import unicode_literals import io -from nose.tools import istest, assert_equal - from mammoth.docx import xmlparser as xml, office_xml +from ..testing import assert_equal -@istest -def alternate_content_is_replaced_by_contents_of_fallback(): +def test_alternate_content_is_replaced_by_contents_of_fallback(): xml_string = ( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + '<numbering xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">' + @@ -21,6 +19,6 @@ def alternate_content_is_replaced_by_contents_of_fallback(): '</mc:Fallback>' + '</mc:AlternateContent>' + '</numbering>') - + result = office_xml.read(io.StringIO(xml_string)) assert_equal([xml.element("fallback")], result.children) diff --git a/tests/docx/relationships_xml_tests.py b/tests/docx/relationships_xml_tests.py index 5e52142..2c5b6ce 100644 --- a/tests/docx/relationships_xml_tests.py +++ b/tests/docx/relationships_xml_tests.py @@ -1,11 +1,9 @@ -from nose.tools import istest, assert_equal - from mammoth.docx.xmlparser import element as xml_element from mammoth.docx.relationships_xml import read_relationships_xml_element +from ..testing import assert_equal -@istest -def relationship_targets_can_be_found_by_id(): +def test_relationship_targets_can_be_found_by_id(): element = xml_element("relationships:Relationships", {}, [ xml_element("relationships:Relationship", { "Id": "rId8", @@ -25,8 +23,7 @@ def relationship_targets_can_be_found_by_id(): ) -@istest -def relationship_targets_can_be_found_by_type(): +def test_relationship_targets_can_be_found_by_type(): element = xml_element("relationships:Relationships", {}, [ xml_element("relationships:Relationship", { "Id": "rId2", @@ -51,8 +48,7 @@ def relationship_targets_can_be_found_by_type(): ) -@istest -def when_there_are_no_relationships_of_requested_type_then_empty_list_is_returned(): +def test_when_there_are_no_relationships_of_requested_type_then_empty_list_is_returned(): element = xml_element("relationships:Relationships", {}, []) relationships = read_relationships_xml_element(element) assert_equal( diff --git a/tests/docx/style_map_tests.py b/tests/docx/style_map_tests.py index b774b66..81349fc 100644 --- a/tests/docx/style_map_tests.py +++ b/tests/docx/style_map_tests.py @@ -1,56 +1,48 @@ import io from zipfile import ZipFile -from nose.tools import istest, assert_equal - from mammoth.docx.style_map import write_style_map, read_style_map from mammoth.zips import open_zip from mammoth.docx import xmlparser as xml +from ..testing import assert_equal -@istest -def reading_embedded_style_map_on_document_without_embedded_style_map_returns_none(): +def test_reading_embedded_style_map_on_document_without_embedded_style_map_returns_none(): fileobj = _normal_docx() assert_equal(None, read_style_map(fileobj)) -@istest -def writing_style_map_preserves_unrelated_files(): +def test_writing_style_map_preserves_unrelated_files(): fileobj = _normal_docx() write_style_map(fileobj, "p => h1") with open_zip(fileobj, "r") as zip_file: assert_equal("placeholder", zip_file.read_str("placeholder")) -@istest -def embedded_style_map_can_be_read_after_being_written(): +def test_embedded_style_map_can_be_read_after_being_written(): fileobj = _normal_docx() write_style_map(fileobj, "p => h1") assert_equal("p => h1", read_style_map(fileobj)) -@istest -def embedded_style_map_is_written_to_separate_file(): +def test_embedded_style_map_is_written_to_separate_file(): fileobj = _normal_docx() write_style_map(fileobj, "p => h1") with open_zip(fileobj, "r") as zip_file: assert_equal("p => h1", zip_file.read_str("mammoth/style-map")) -@istest -def embedded_style_map_is_referenced_in_relationships(): +def test_embedded_style_map_is_referenced_in_relationships(): fileobj = _normal_docx() write_style_map(fileobj, "p => h1") assert_equal(expected_relationships_xml, _read_relationships_xml(fileobj)) - -@istest -def embedded_style_map_has_override_content_type_in_content_types_xml(): + +def test_embedded_style_map_has_override_content_type_in_content_types_xml(): fileobj = _normal_docx() write_style_map(fileobj, "p => h1") assert_equal(expected_content_types_xml, _read_content_types_xml(fileobj)) -@istest -def can_overwrite_existing_style_map(): +def test_can_overwrite_existing_style_map(): fileobj = _normal_docx() write_style_map(fileobj, "p => h1") write_style_map(fileobj, "p => h2") diff --git a/tests/docx/styles_xml_tests.py b/tests/docx/styles_xml_tests.py index 580d91d..165d86c 100644 --- a/tests/docx/styles_xml_tests.py +++ b/tests/docx/styles_xml_tests.py @@ -1,18 +1,15 @@ -from nose.tools import istest, assert_equal - from mammoth.docx.xmlparser import element as xml_element from mammoth.docx.styles_xml import read_styles_xml_element +from ..testing import assert_equal -@istest -def paragraph_style_is_null_if_no_style_with_that_id_exists(): +def test_paragraph_style_is_null_if_no_style_with_that_id_exists(): element = xml_element("w:styles") styles = read_styles_xml_element(element) assert_equal(None, styles.find_paragraph_style_by_id("Heading1")) -@istest -def paragraph_style_can_be_found_by_id(): +def test_paragraph_style_can_be_found_by_id(): element = xml_element("w:styles", {}, [ _paragraph_style_element("Heading1", "Heading 1"), ]) @@ -23,8 +20,7 @@ def paragraph_style_can_be_found_by_id(): ) -@istest -def character_style_can_be_found_by_id(): +def test_character_style_can_be_found_by_id(): element = xml_element("w:styles", {}, [ _character_style_element("Heading1Char", "Heading 1 Char"), ]) @@ -35,8 +31,7 @@ def character_style_can_be_found_by_id(): ) -@istest -def table_style_can_be_found_by_id(): +def test_table_style_can_be_found_by_id(): element = xml_element("w:styles", {}, [ _table_style_element("TableNormal", "Normal Table"), ]) @@ -47,8 +42,7 @@ def table_style_can_be_found_by_id(): ) -@istest -def paragraph_and_character_styles_are_distinct(): +def test_paragraph_and_character_styles_are_distinct(): element = xml_element("w:styles", {}, [ _paragraph_style_element("Heading1", "Heading 1"), _character_style_element("Heading1Char", "Heading 1 Char"), @@ -58,8 +52,7 @@ def paragraph_and_character_styles_are_distinct(): assert_equal(None, styles.find_paragraph_style_by_id("Heading1Char")) -@istest -def styles_include_names(): +def test_styles_include_names(): element = xml_element("w:styles", {}, [ _paragraph_style_element("Heading1", "Heading 1"), ]) @@ -70,8 +63,7 @@ def styles_include_names(): ) -@istest -def style_name_is_none_if_name_element_does_not_exist(): +def test_style_name_is_none_if_name_element_does_not_exist(): element = xml_element("w:styles", {}, [ _style_without_name_element("paragraph", "Heading1"), _style_without_name_element("character", "Heading1Char") @@ -81,15 +73,13 @@ def style_name_is_none_if_name_element_does_not_exist(): assert_equal(None, styles.find_character_style_by_id("Heading1Char").name) -@istest -def numbering_style_is_none_if_no_style_with_that_id_exists(): +def test_numbering_style_is_none_if_no_style_with_that_id_exists(): element = xml_element("w:styles", {}, []) styles = read_styles_xml_element(element) assert_equal(None, styles.find_numbering_style_by_id("List1")) -@istest -def numbering_style_has_none_num_id_if_style_has_no_paragraph_properties(): +def test_numbering_style_has_none_num_id_if_style_has_no_paragraph_properties(): element = xml_element("w:styles", {}, [ xml_element("w:style", {"w:type": "numbering", "w:styleId": "List1"}), ]) @@ -97,8 +87,7 @@ def numbering_style_has_none_num_id_if_style_has_no_paragraph_properties(): assert_equal(None, styles.find_numbering_style_by_id("List1").num_id) -@istest -def numbering_style_has_num_id_read_from_paragraph_properties(): +def test_numbering_style_has_num_id_read_from_paragraph_properties(): element = xml_element("w:styles", {}, [ xml_element("w:style", {"w:type": "numbering", "w:styleId": "List1"}, [ xml_element("w:pPr", {}, [ diff --git a/tests/docx/uris_tests.py b/tests/docx/uris_tests.py index 6b9c264..298a40a 100644 --- a/tests/docx/uris_tests.py +++ b/tests/docx/uris_tests.py @@ -1,18 +1,15 @@ -from nose.tools import istest, assert_equal - from mammoth.docx.uris import uri_to_zip_entry_name +from ..testing import assert_equal -@istest -def when_path_does_not_have_leading_slash_then_path_is_resolved_relative_to_base(): +def test_when_path_does_not_have_leading_slash_then_path_is_resolved_relative_to_base(): assert_equal( "one/two/three/four", uri_to_zip_entry_name("one/two", "three/four"), ) -@istest -def when_path_has_leading_slash_then_base_is_ignored(): +def test_when_path_has_leading_slash_then_base_is_ignored(): assert_equal( "three/four", uri_to_zip_entry_name("one/two", "/three/four"), diff --git a/tests/docx/xmlparser_tests.py b/tests/docx/xmlparser_tests.py index 65a2f25..8c960eb 100644 --- a/tests/docx/xmlparser_tests.py +++ b/tests/docx/xmlparser_tests.py @@ -1,89 +1,73 @@ import io -from nose.tools import istest, assert_equal - from mammoth.docx.xmlparser import parse_xml, element as xml_element, text as xml_text +from ..testing import assert_equal -@istest -def can_parse_self_closing_element(): +def test_can_parse_self_closing_element(): xml = _parse_xml_string(b"<body/>") assert_equal(xml_element("body", {}, []), xml) -@istest -def can_parse_empty_element_with_separate_closing_tag(): +def test_can_parse_empty_element_with_separate_closing_tag(): xml = _parse_xml_string(b"<body></body>") assert_equal(xml_element("body", {}, []), xml) -@istest -def can_parse_attributes_of_tag(): +def test_can_parse_attributes_of_tag(): xml = _parse_xml_string(b"<body name='bob'></body>") assert_equal(xml_element("body", {"name": "bob"}, []), xml) -@istest -def can_parse_text_element(): +def test_can_parse_text_element(): xml = _parse_xml_string(b"<body>Hello!</body>") assert_equal(xml_element("body", {}, [xml_text("Hello!")]), xml) -@istest -def can_parse_text_element_before_new_tag(): +def test_can_parse_text_element_before_new_tag(): xml = _parse_xml_string(b"<body>Hello!<br/></body>") assert_equal(xml_element("body", {}, [xml_text("Hello!"), xml_element("br", {}, [])]), xml) -@istest -def can_parse_element_with_children(): +def test_can_parse_element_with_children(): xml = _parse_xml_string(b"<body><a/><b/></body>") assert_equal([xml_element("a", {}, []), xml_element("b", {}, [])], xml.children) -@istest -def unmapped_namespaces_uris_are_included_in_braces_as_prefix(): +def test_unmapped_namespaces_uris_are_included_in_braces_as_prefix(): xml = _parse_xml_string(b'<w:body xmlns:w="word"/>') assert_equal("{word}body", xml.name) -@istest -def mapped_namespaces_uris_are_translated_using_namespace_map(): +def test_mapped_namespaces_uris_are_translated_using_namespace_map(): xml = _parse_xml_string(b'<w:body xmlns:w="word"/>', [("x", "word")]) assert_equal("x:body", xml.name) -@istest -def namespace_of_attributes_is_mapped_to_prefix(): +def test_namespace_of_attributes_is_mapped_to_prefix(): xml = _parse_xml_string(b'<w:body xmlns:w="word" w:val="Hello!"/>', [("x", "word")]) assert_equal("Hello!", xml.attributes["x:val"]) -@istest -def whitespace_between_xml_declaration_and_root_tag_is_ignored(): +def test_whitespace_between_xml_declaration_and_root_tag_is_ignored(): xml = _parse_xml_string(b'<?xml version="1.0" ?>\n<body/>') assert_equal("body", xml.name) - -@istest + class FindChildTests(object): - @istest - def returns_none_if_no_children(self): + def test_returns_none_if_no_children(self): xml = xml_element("a") assert_equal(None, xml.find_child("b")) - - @istest - def returns_none_if_no_matching_children(self): + + def test_returns_none_if_no_matching_children(self): xml = xml_element("a", {}, [xml_element("c")]) assert_equal(None, xml.find_child("b")) - - @istest - def returns_first_matching_child(self): + + def test_returns_first_matching_child(self): xml = xml_element("a", {}, [xml_element("b", {"id": 1}), xml_element("b", {"id": 2})]) assert_equal(1, xml.find_child("b").attributes["id"]) - - @istest - def ignores_text_nodes(self): + + def test_ignores_text_nodes(self): xml = xml_element("a", {}, [xml_text("Hello!")]) assert_equal(None, xml.find_child("b")) |
