diff options
Diffstat (limited to 'tests')
33 files changed, 571 insertions, 1006 deletions
diff --git a/tests/cli_tests.py b/tests/cli_tests.py index 4728c6b..7a2ed23 100644 --- a/tests/cli_tests.py +++ b/tests/cli_tests.py @@ -1,28 +1,26 @@ import os import base64 -from nose.tools import istest, assert_equal import spur import tempman -from .testing import test_path +from .testing import assert_equal, generate_test_path _local = spur.LocalShell() -@istest -def html_is_printed_to_stdout_if_output_file_is_not_set(): - docx_path = test_path("single-paragraph.docx") + +def test_html_is_printed_to_stdout_if_output_file_is_not_set(): + docx_path = generate_test_path("single-paragraph.docx") result = _local.run(["mammoth", docx_path]) assert_equal(b"", result.stderr_output) assert_equal(b"<p>Walking on imported air</p>", result.output) -@istest -def html_is_written_to_file_if_output_file_is_set(): +def test_html_is_written_to_file_if_output_file_is_set(): with tempman.create_temp_dir() as temp_dir: output_path = os.path.join(temp_dir.path, "output.html") - docx_path = test_path("single-paragraph.docx") + docx_path = generate_test_path("single-paragraph.docx") result = _local.run(["mammoth", docx_path, output_path]) assert_equal(b"", result.stderr_output) assert_equal(b"", result.output) @@ -32,34 +30,32 @@ def html_is_written_to_file_if_output_file_is_set(): _image_base_64 = b"iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" -@istest -def inline_images_are_included_in_output_if_writing_to_single_file(): - docx_path = test_path("tiny-picture.docx") + +def test_inline_images_are_included_in_output_if_writing_to_single_file(): + docx_path = generate_test_path("tiny-picture.docx") result = _local.run(["mammoth", docx_path]) assert_equal(b"""<p><img src="data:image/png;base64,""" + _image_base_64 + b"""" /></p>""", result.output) -@istest -def images_are_written_to_separate_files_if_output_dir_is_set(): +def test_images_are_written_to_separate_files_if_output_dir_is_set(): with tempman.create_temp_dir() as temp_dir: output_path = os.path.join(temp_dir.path, "tiny-picture.html") image_path = os.path.join(temp_dir.path, "1.png") - - docx_path = test_path("tiny-picture.docx") + + docx_path = generate_test_path("tiny-picture.docx") result = _local.run(["mammoth", docx_path, "--output-dir", temp_dir.path]) assert_equal(b"", result.stderr_output) assert_equal(b"", result.output) with open(output_path) as output_file: assert_equal("""<p><img src="1.png" /></p>""", output_file.read()) - + with open(image_path, "rb") as image_file: assert_equal(_image_base_64, base64.b64encode(image_file.read())) - -@istest -def style_map_is_used_if_set(): + +def test_style_map_is_used_if_set(): with tempman.create_temp_dir() as temp_dir: - docx_path = test_path("single-paragraph.docx") + docx_path = generate_test_path("single-paragraph.docx") style_map_path = os.path.join(temp_dir.path, "style-map") with open(style_map_path, "w") as style_map_file: style_map_file.write("p => span:fresh") @@ -68,9 +64,8 @@ def style_map_is_used_if_set(): assert_equal(b"<span>Walking on imported air</span>", result.output) -@istest -def output_format_markdown_option_generates_markdown_output(): - docx_path = test_path("single-paragraph.docx") +def test_output_format_markdown_option_generates_markdown_output(): + docx_path = generate_test_path("single-paragraph.docx") result = _local.run(["mammoth", docx_path, "--output-format=markdown"]) assert_equal(b"", result.stderr_output) assert_equal(b"Walking on imported air\n\n", result.output) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..a342abb --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,9 @@ +import funk +import pytest + + +@pytest.fixture(name="mocks") +def _fixture_mocks(): + mocks = funk.Mocks() + yield mocks + mocks.verify() diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index c4e667f..8f02048 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -4,24 +4,21 @@ from __future__ import unicode_literals import io -from nose.tools import istest, assert_equal - from mammoth import documents, results, html from mammoth.conversion import convert_document_element_to_html, _comment_author_label from mammoth.docx.xmlparser import parse_xml from mammoth.styles.parser import read_style_mapping +from .testing import assert_equal -@istest -def plain_paragraph_is_converted_to_plain_paragraph(): +def test_plain_paragraph_is_converted_to_plain_paragraph(): result = convert_document_element_to_html( documents.paragraph(children=[_run_with_text("Hello")]) ) assert_equal('<p>Hello</p>', result.value) -@istest -def multiple_paragraphs_are_converted_to_multiple_paragraphs(): +def test_multiple_paragraphs_are_converted_to_multiple_paragraphs(): result = convert_document_element_to_html( documents.document([ documents.paragraph(children=[_run_with_text("Hello")]), @@ -31,16 +28,14 @@ def multiple_paragraphs_are_converted_to_multiple_paragraphs(): assert_equal('<p>Hello</p><p>there</p>', result.value) -@istest -def empty_paragraphs_are_ignored(): +def test_empty_paragraphs_are_ignored(): result = convert_document_element_to_html( documents.paragraph(children=[_run_with_text("")]) ) assert_equal('', result.value) -@istest -def style_mappings_using_style_ids_can_be_used_to_map_paragraphs(): +def test_style_mappings_using_style_ids_can_be_used_to_map_paragraphs(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") @@ -52,8 +47,7 @@ def style_mappings_using_style_ids_can_be_used_to_map_paragraphs(): assert_equal('<p class="tip">Tip</p>', result.value) -@istest -def style_mappings_using_style_names_can_be_used_to_map_paragraphs(): +def test_style_mappings_using_style_names_can_be_used_to_map_paragraphs(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", style_name="Tips Paragraph", children=[ _run_with_text("Tip") @@ -65,8 +59,7 @@ def style_mappings_using_style_names_can_be_used_to_map_paragraphs(): assert_equal('<p class="tip">Tip</p>', result.value) -@istest -def style_names_in_style_mappings_are_case_insensitive(): +def test_style_names_in_style_mappings_are_case_insensitive(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", style_name="Tips Paragraph", children=[ _run_with_text("Tip") @@ -78,8 +71,7 @@ def style_names_in_style_mappings_are_case_insensitive(): assert_equal('<p class="tip">Tip</p>', result.value) -@istest -def default_paragraph_style_is_used_if_no_matching_style_is_found(): +def test_default_paragraph_style_is_used_if_no_matching_style_is_found(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") @@ -88,8 +80,7 @@ def default_paragraph_style_is_used_if_no_matching_style_is_found(): assert_equal('<p>Tip</p>', result.value) -@istest -def default_paragraph_style_is_specified_by_mapping_plain_paragraphs(): +def test_default_paragraph_style_is_specified_by_mapping_plain_paragraphs(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") @@ -101,8 +92,7 @@ def default_paragraph_style_is_specified_by_mapping_plain_paragraphs(): assert_equal('<p class="tip">Tip</p>', result.value) -@istest -def warning_is_emitted_if_paragraph_style_is_unrecognised(): +def test_warning_is_emitted_if_paragraph_style_is_unrecognised(): result = convert_document_element_to_html( documents.paragraph( style_id="Heading1", @@ -113,16 +103,14 @@ def warning_is_emitted_if_paragraph_style_is_unrecognised(): assert_equal([results.warning("Unrecognised paragraph style: Heading 1 (Style ID: Heading1)")], result.messages) -@istest -def no_warning_if_there_is_no_style_for_plain_paragraphs(): +def test_no_warning_if_there_is_no_style_for_plain_paragraphs(): result = convert_document_element_to_html( documents.paragraph(children=[_run_with_text("Tip")]), ) assert_equal([], result.messages) -@istest -def bulleted_paragraphs_are_converted_using_matching_styles(): +def test_bulleted_paragraphs_are_converted_using_matching_styles(): result = convert_document_element_to_html( documents.paragraph(children=[ _run_with_text("Hello") @@ -134,8 +122,7 @@ def bulleted_paragraphs_are_converted_using_matching_styles(): assert_equal('<ul><li>Hello</li></ul>', result.value) -@istest -def bulleted_styles_dont_match_plain_paragraph(): +def test_bulleted_styles_dont_match_plain_paragraph(): result = convert_document_element_to_html( documents.paragraph(children=[ _run_with_text("Hello") @@ -147,16 +134,14 @@ def bulleted_styles_dont_match_plain_paragraph(): assert_equal('<p>Hello</p>', result.value) -@istest -def bold_runs_are_wrapped_in_strong_tags_by_default(): +def test_bold_runs_are_wrapped_in_strong_tags_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_bold=True), ) assert_equal("<strong>Hello</strong>", result.value) -@istest -def bold_runs_can_be_configured_with_style_mapping(): +def test_bold_runs_can_be_configured_with_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_bold=True), style_map=[_style_mapping("b => em")] @@ -164,16 +149,14 @@ def bold_runs_can_be_configured_with_style_mapping(): assert_equal("<em>Hello</em>", result.value) -@istest -def italic_runs_are_wrapped_in_emphasis_tags_by_default(): +def test_italic_runs_are_wrapped_in_emphasis_tags_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_italic=True), ) assert_equal("<em>Hello</em>", result.value) -@istest -def italic_runs_can_be_configured_with_style_mapping(): +def test_italic_runs_can_be_configured_with_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_italic=True), style_map=[_style_mapping("i => strong")] @@ -181,16 +164,14 @@ def italic_runs_can_be_configured_with_style_mapping(): assert_equal("<strong>Hello</strong>", result.value) -@istest -def underline_runs_are_ignored_by_default(): +def test_underline_runs_are_ignored_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_underline=True), ) assert_equal("Hello", result.value) -@istest -def underline_runs_can_be_mapped_using_style_mapping(): +def test_underline_runs_can_be_mapped_using_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_underline=True), style_map=[ @@ -200,8 +181,7 @@ def underline_runs_can_be_mapped_using_style_mapping(): assert_equal("<em>Hello</em>", result.value) -@istest -def style_mapping_for_underline_runs_does_not_close_parent_elements(): +def test_style_mapping_for_underline_runs_does_not_close_parent_elements(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_underline=True, is_bold=True), style_map=[ @@ -211,16 +191,14 @@ def style_mapping_for_underline_runs_does_not_close_parent_elements(): assert_equal("<strong><em>Hello</em></strong>", result.value) -@istest -def strikethrough_runs_are_wrapped_in_s_elements_by_default(): +def test_strikethrough_runs_are_wrapped_in_s_elements_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_strikethrough=True), ) assert_equal("<s>Hello</s>", result.value) -@istest -def strikethrough_runs_can_be_configured_with_style_mapping(): +def test_strikethrough_runs_can_be_configured_with_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_strikethrough=True), style_map=[ @@ -230,16 +208,14 @@ def strikethrough_runs_can_be_configured_with_style_mapping(): assert_equal("<del>Hello</del>", result.value) -@istest -def sml_caps_runs_are_ignored_by_default(): +def test_sml_caps_runs_are_ignored_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_all_caps=True), ) assert_equal("Hello", result.value) -@istest -def all_caps_runs_can_be_mapped_using_style_mapping(): +def test_all_caps_runs_can_be_mapped_using_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_all_caps=True), style_map=[ @@ -249,16 +225,14 @@ def all_caps_runs_can_be_mapped_using_style_mapping(): assert_equal("<span>Hello</span>", result.value) -@istest -def small_caps_runs_are_ignored_by_default(): +def test_small_caps_runs_are_ignored_by_default(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_small_caps=True), ) assert_equal("Hello", result.value) -@istest -def small_caps_runs_can_be_mapped_using_style_mapping(): +def test_small_caps_runs_can_be_mapped_using_style_mapping(): result = convert_document_element_to_html( documents.run(children=[documents.text("Hello")], is_small_caps=True), style_map=[ @@ -268,8 +242,7 @@ def small_caps_runs_can_be_mapped_using_style_mapping(): assert_equal("<span>Hello</span>", result.value) -@istest -def superscript_runs_are_wrapped_in_sup_tags(): +def test_superscript_runs_are_wrapped_in_sup_tags(): result = convert_document_element_to_html( documents.run( children=[documents.text("Hello")], @@ -279,8 +252,7 @@ def superscript_runs_are_wrapped_in_sup_tags(): assert_equal("<sup>Hello</sup>", result.value) -@istest -def subscript_runs_are_wrapped_in_sub_tags(): +def test_subscript_runs_are_wrapped_in_sub_tags(): result = convert_document_element_to_html( documents.run( children=[documents.text("Hello")], @@ -290,8 +262,7 @@ def subscript_runs_are_wrapped_in_sub_tags(): assert_equal("<sub>Hello</sub>", result.value) -@istest -def runs_are_converted_by_satisfying_matching_paths(): +def test_runs_are_converted_by_satisfying_matching_paths(): result = convert_document_element_to_html( documents.run(style_id="TipsRun", children=[documents.Text("Tip")]), style_map=[ @@ -301,16 +272,14 @@ def runs_are_converted_by_satisfying_matching_paths(): assert_equal('<span class="tip">Tip</span>', result.value) -@istest -def docx_hyperlink_with_href_is_converted_to_anchor_tag(): +def test_docx_hyperlink_with_href_is_converted_to_anchor_tag(): result = convert_document_element_to_html( documents.hyperlink(href="http://example.com", children=[documents.Text("Hello")]), ) assert_equal('<a href="http://example.com">Hello</a>', result.value) -@istest -def docx_hyperlinks_can_be_collapsed(): +def test_docx_hyperlinks_can_be_collapsed(): result = convert_document_element_to_html( documents.document(children=[ documents.hyperlink(href="http://example.com", children=[documents.Text("Hello ")]), @@ -320,8 +289,7 @@ def docx_hyperlinks_can_be_collapsed(): assert_equal('<a href="http://example.com">Hello world</a>', result.value) -@istest -def docx_hyperlink_with_internal_anchor_reference_is_converted_to_anchor_tag(): +def test_docx_hyperlink_with_internal_anchor_reference_is_converted_to_anchor_tag(): result = convert_document_element_to_html( documents.hyperlink(anchor="start", children=[documents.Text("Hello")]), id_prefix="doc-42-", @@ -329,8 +297,7 @@ def docx_hyperlink_with_internal_anchor_reference_is_converted_to_anchor_tag(): assert_equal('<a href="#doc-42-start">Hello</a>', result.value) -@istest -def hyperlink_target_frame_is_used_as_anchor_target(): +def test_hyperlink_target_frame_is_used_as_anchor_target(): result = convert_document_element_to_html( documents.hyperlink( anchor="start", @@ -341,8 +308,7 @@ def hyperlink_target_frame_is_used_as_anchor_target(): assert_equal('<a href="#start" target="_blank">Hello</a>', result.value) -@istest -def bookmarks_are_converted_to_anchors_with_ids(): +def test_bookmarks_are_converted_to_anchors_with_ids(): result = convert_document_element_to_html( documents.bookmark(name="start"), id_prefix="doc-42-", @@ -350,14 +316,12 @@ def bookmarks_are_converted_to_anchors_with_ids(): assert_equal('<a id="doc-42-start"></a>', result.value) -@istest -def docx_tab_is_converted_to_tab_in_html(): +def test_docx_tab_is_converted_to_tab_in_html(): result = convert_document_element_to_html(documents.tab()) assert_equal('\t', result.value) -@istest -def docx_table_is_converted_to_table_in_html(): +def test_docx_table_is_converted_to_table_in_html(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("Top left")]), @@ -377,8 +341,7 @@ def docx_table_is_converted_to_table_in_html(): assert_equal(expected_html, result.value) -@istest -def table_style_mappings_can_be_used_to_map_tables(): +def test_table_style_mappings_can_be_used_to_map_tables(): table = documents.table([], style_name="Normal Table") result = convert_document_element_to_html( table, @@ -390,8 +353,7 @@ def table_style_mappings_can_be_used_to_map_tables(): assert_equal(expected_html, result.value) -@istest -def header_rows_are_wrapped_in_thead(): +def test_header_rows_are_wrapped_in_thead(): table = documents.table([ documents.table_row([documents.table_cell([])], is_header=True), documents.table_row([documents.table_cell([])], is_header=True), @@ -406,8 +368,7 @@ def header_rows_are_wrapped_in_thead(): assert_equal(expected_html, result.value) -@istest -def tbody_is_omitted_if_all_rows_are_headers(): +def test_tbody_is_omitted_if_all_rows_are_headers(): table = documents.table([ documents.table_row([documents.table_cell([])], is_header=True), ]) @@ -419,8 +380,7 @@ def tbody_is_omitted_if_all_rows_are_headers(): assert_equal(expected_html, result.value) -@istest -def unexpected_table_children_do_not_cause_error(): +def test_unexpected_table_children_do_not_cause_error(): table = documents.table([ documents.tab(), ]) @@ -429,8 +389,7 @@ def unexpected_table_children_do_not_cause_error(): assert_equal(expected_html, result.value) -@istest -def empty_cells_are_preserved_in_table(): +def test_empty_cells_are_preserved_in_table(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("")]), @@ -445,8 +404,7 @@ def empty_cells_are_preserved_in_table(): assert_equal(expected_html, result.value) -@istest -def empty_rows_are_preserved_in_table(): +def test_empty_rows_are_preserved_in_table(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("Row 1")]), @@ -461,8 +419,7 @@ def empty_rows_are_preserved_in_table(): assert_equal(expected_html, result.value) -@istest -def table_cells_are_written_with_colspan_if_not_equal_to_one(): +def test_table_cells_are_written_with_colspan_if_not_equal_to_one(): table = documents.table([ documents.table_row([ documents.table_cell([_paragraph_with_text("Top left")], colspan=2), @@ -477,8 +434,7 @@ def table_cells_are_written_with_colspan_if_not_equal_to_one(): assert_equal(expected_html, result.value) -@istest -def table_cells_are_written_with_rowspan_if_not_equal_to_one(): +def test_table_cells_are_written_with_rowspan_if_not_equal_to_one(): table = documents.table([ documents.table_row([ documents.table_cell([], rowspan=2), @@ -492,20 +448,17 @@ def table_cells_are_written_with_rowspan_if_not_equal_to_one(): assert_equal(expected_html, result.value) -@istest -def line_break_is_converted_to_br(): +def test_line_break_is_converted_to_br(): result = convert_document_element_to_html(documents.line_break) assert_equal("<br />", result.value) -@istest -def breaks_that_are_not_line_breaks_are_ignored(): +def test_breaks_that_are_not_line_breaks_are_ignored(): result = convert_document_element_to_html(documents.page_break) assert_equal("", result.value) -@istest -def breaks_can_be_mapped_using_style_mappings(): +def test_breaks_can_be_mapped_using_style_mappings(): result = convert_document_element_to_html( documents.page_break, style_map=[ @@ -515,23 +468,20 @@ def breaks_can_be_mapped_using_style_mappings(): assert_equal("<hr />", result.value) -@istest -def images_are_converted_to_img_tags_with_data_uri(): +def test_images_are_converted_to_img_tags_with_data_uri(): image = documents.image(alt_text=None, content_type="image/png", open=lambda: io.BytesIO(b"abc")) result = convert_document_element_to_html(image) assert_equal('<img src="data:image/png;base64,YWJj" />', result.value) -@istest -def images_have_alt_tags_if_available(): +def test_images_have_alt_tags_if_available(): image = documents.image(alt_text="It's a hat", content_type="image/png", open=lambda: io.BytesIO(b"abc")) result = convert_document_element_to_html(image) image_html = parse_xml(io.StringIO(result.value)) assert_equal('It\'s a hat', image_html.attributes["alt"]) -@istest -def can_define_custom_conversion_for_images(): +def test_can_define_custom_conversion_for_images(): def convert_image(image): with image.open() as image_file: return [html.element("img", {"alt": image_file.read().decode("ascii")})] @@ -541,8 +491,7 @@ def can_define_custom_conversion_for_images(): assert_equal('<img alt="abc" />', result.value) -@istest -def footnote_reference_is_converted_to_superscript_intra_page_link(): +def test_footnote_reference_is_converted_to_superscript_intra_page_link(): footnote_reference = documents.note_reference("footnote", "4") result = convert_document_element_to_html( footnote_reference, @@ -551,8 +500,7 @@ def footnote_reference_is_converted_to_superscript_intra_page_link(): assert_equal('<sup><a href="#doc-42-footnote-4" id="doc-42-footnote-ref-4">[1]</a></sup>', result.value) -@istest -def footnotes_are_included_after_the_main_body(): +def test_footnotes_are_included_after_the_main_body(): footnote_reference = documents.note_reference("footnote", "4") document = documents.document( [documents.paragraph([ @@ -572,8 +520,7 @@ def footnotes_are_included_after_the_main_body(): assert_equal(expected_html, result.value) -@istest -def comments_are_ignored_by_default(): +def test_comments_are_ignored_by_default(): reference = documents.comment_reference("4") comment = documents.comment( comment_id="4", @@ -591,8 +538,7 @@ def comments_are_ignored_by_default(): assert_equal(expected_html, result.value) -@istest -def comment_references_are_linked_to_comment_after_main_body(): +def test_comment_references_are_linked_to_comment_after_main_body(): reference = documents.comment_reference("4") comment = documents.comment( comment_id="4", @@ -621,8 +567,7 @@ def comment_references_are_linked_to_comment_after_main_body(): assert_equal(expected_html, result.value) -@istest -def when_initials_are_not_blank_then_comment_author_label_is_initials(): +def test_when_initials_are_not_blank_then_comment_author_label_is_initials(): assert_equal("TP", _comment_author_label(documents.comment( comment_id="0", body=[], @@ -630,8 +575,7 @@ def when_initials_are_not_blank_then_comment_author_label_is_initials(): ))) -@istest -def when_initials_are_blank_then_comment_author_label_is_blank(): +def test_when_initials_are_blank_then_comment_author_label_is_blank(): assert_equal("", _comment_author_label(documents.comment( comment_id="0", body=[], 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")) diff --git a/tests/html/collapse_tests.py b/tests/html/collapse_tests.py index 5dc7e6c..ebffd69 100644 --- a/tests/html/collapse_tests.py +++ b/tests/html/collapse_tests.py @@ -1,62 +1,55 @@ -from nose.tools import istest, assert_equal - from mammoth import html +from ..testing import assert_equal -@istest -def collapsing_does_nothing_to_single_text_node(): +def test_collapsing_does_nothing_to_single_text_node(): assert_equal( html.collapse([html.text("Bluebells")]), [html.text("Bluebells")]) -@istest -def consecutive_fresh_elements_are_not_collapsed(): +def test_consecutive_fresh_elements_are_not_collapsed(): assert_equal( html.collapse([html.element("p"), html.element("p")]), [html.element("p"), html.element("p")]) -@istest -def consecutive_collapsible_elements_are_collapsed_if_they_have_the_same_tag_and_attributes(): +def test_consecutive_collapsible_elements_are_collapsed_if_they_have_the_same_tag_and_attributes(): assert_equal( [html.collapsible_element("p", {}, [html.text("One"), html.text("Two")])], html.collapse([ html.collapsible_element("p", {}, [html.text("One")]), html.collapsible_element("p", {}, [html.text("Two")]) ])) - -@istest -def elements_with_different_tag_names_are_not_collapsed(): + +def test_elements_with_different_tag_names_are_not_collapsed(): assert_equal( [ html.collapsible_element("p", {}, [html.text("One")]), html.collapsible_element("div", {}, [html.text("Two")]) ], - + html.collapse([ html.collapsible_element("p", {}, [html.text("One")]), html.collapsible_element("div", {}, [html.text("Two")]) ])) - -@istest -def elements_with_different_attributes_are_not_collapsed(): + +def test_elements_with_different_attributes_are_not_collapsed(): assert_equal( [ html.collapsible_element("p", {"id": "a"}, [html.text("One")]), html.collapsible_element("p", {}, [html.text("Two")]) ], - + html.collapse([ html.collapsible_element("p", {"id": "a"}, [html.text("One")]), html.collapsible_element("p", {}, [html.text("Two")]) ])) -@istest -def children_of_collapsed_element_can_collapse_with_children_of_previous_element(): +def test_children_of_collapsed_element_can_collapse_with_children_of_previous_element(): assert_equal( [ html.collapsible_element("blockquote", {}, [ @@ -66,7 +59,7 @@ def children_of_collapsed_element_can_collapse_with_children_of_previous_element ]) ]), ], - + html.collapse([ html.collapsible_element("blockquote", {}, [ html.collapsible_element("p", {}, [html.text("One")]) @@ -77,8 +70,7 @@ def children_of_collapsed_element_can_collapse_with_children_of_previous_element ])) -@istest -def collapsible_element_can_collapse_into_previous_fresh_element(): +def test_collapsible_element_can_collapse_into_previous_fresh_element(): assert_equal( [html.element("p", {}, [html.text("One"), html.text("Two")])], html.collapse([ @@ -87,8 +79,7 @@ def collapsible_element_can_collapse_into_previous_fresh_element(): ])) -@istest -def element_with_choice_of_tag_names_can_collapse_into_previous_element_if_it_has_one_of_those_tag_names_as_its_main_tag_name(): +def test_element_with_choice_of_tag_names_can_collapse_into_previous_element_if_it_has_one_of_those_tag_names_as_its_main_tag_name(): assert_equal( [html.collapsible_element(["ol"])], html.collapse([ @@ -107,8 +98,7 @@ def element_with_choice_of_tag_names_can_collapse_into_previous_element_if_it_ha ])) -@istest -def when_separator_is_present_then_separator_is_prepended_to_collapsed_element(): +def test_when_separator_is_present_then_separator_is_prepended_to_collapsed_element(): assert_equal( [ html.element("pre", collapsible=False, children=[ diff --git a/tests/html/strip_empty_tests.py b/tests/html/strip_empty_tests.py index b72b50a..8660cff 100644 --- a/tests/html/strip_empty_tests.py +++ b/tests/html/strip_empty_tests.py @@ -1,65 +1,56 @@ -from nose.tools import istest, assert_equal - from mammoth import html +from ..testing import assert_equal -@istest -def text_nodes_with_text_are_not_stripped(): +def test_text_nodes_with_text_are_not_stripped(): assert_equal( [html.text("H")], html.strip_empty([html.text("H")])) -@istest -def empty_text_nodes_are_stripped(): +def test_empty_text_nodes_are_stripped(): assert_equal( [], html.strip_empty([html.text("")])) -@istest -def elements_with_non_empty_children_are_not_stripped(): +def test_elements_with_non_empty_children_are_not_stripped(): assert_equal( [html.element("p", {}, [html.text("H")])], html.strip_empty([html.element("p", {}, [html.text("H")])])) -@istest -def elements_with_no_children_are_stripped(): +def test_elements_with_no_children_are_stripped(): assert_equal( [], html.strip_empty([html.element("p")])) -@istest -def elements_with_only_empty_children_are_stripped(): +def test_elements_with_only_empty_children_are_stripped(): assert_equal( [], html.strip_empty([html.element("p", {}, [html.text("")])])) -@istest -def empty_children_are_removed(): +def test_empty_children_are_removed(): assert_equal( html.strip_empty([html.element("ul", {}, [ html.element("li", {}, [html.text("")]), html.element("li", {}, [html.text("H")]), ])]), - + [html.element("ul", {}, [ html.element("li", {}, [html.text("H")]) ])]) -@istest -def self_closing_elements_are_never_empty(): +def test_self_closing_elements_are_never_empty(): assert_equal( [html.element("br")], html.strip_empty([html.element("br")])) -@istest -def force_writes_are_never_empty(): +def test_force_writes_are_never_empty(): assert_equal( [html.force_write], html.strip_empty([html.force_write])) diff --git a/tests/images_tests.py b/tests/images_tests.py index 0ab29e1..6e2487d 100644 --- a/tests/images_tests.py +++ b/tests/images_tests.py @@ -1,18 +1,15 @@ import io -from nose.tools import istest from precisely import assert_that, has_attrs, is_sequence import mammoth -@istest -def inline_is_available_as_alias_of_img_element(): +def test_inline_is_available_as_alias_of_img_element(): assert mammoth.images.inline is mammoth.images.img_element -@istest -def data_uri_encodes_images_in_base64(): +def test_data_uri_encodes_images_in_base64(): image_bytes = b"abc" image = mammoth.documents.Image( alt_text=None, @@ -27,9 +24,7 @@ def data_uri_encodes_images_in_base64(): )) -@istest class ImgElementTests: - @istest def test_when_element_does_not_have_alt_text_then_alt_attribute_is_not_set(self): image_bytes = b"abc" image = mammoth.documents.Image( @@ -48,7 +43,6 @@ class ImgElementTests: has_attrs(attributes={"src": "<src>"}), )) - @istest def test_when_element_se_alt_text_then_alt_attribute_is_set(self): image_bytes = b"abc" image = mammoth.documents.Image( @@ -67,7 +61,6 @@ class ImgElementTests: has_attrs(attributes={"alt": "<alt>", "src": "<src>"}), )) - @istest def test_image_alt_text_can_be_overridden_by_alt_attribute_returned_from_function(self): image_bytes = b"abc" image = mammoth.documents.Image( diff --git a/tests/lists_tests.py b/tests/lists_tests.py index 49d5331..b8a37f0 100644 --- a/tests/lists_tests.py +++ b/tests/lists_tests.py @@ -1,13 +1,10 @@ -from nose.tools import istest, assert_equal - from mammoth.lists import unique +from .testing import assert_equal -@istest -def unique_of_empty_list_is_empty_list(): +def test_unique_of_empty_list_is_empty_list(): assert_equal([], unique([])) -@istest -def unique_removes_duplicates_while_preserving_order(): +def test_unique_removes_duplicates_while_preserving_order(): assert_equal(["apple", "banana"], unique(["apple", "banana", "apple"])) diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index 1dcbaa9..b8c6a11 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -8,80 +8,70 @@ import shutil import os import tempman -from nose.tools import istest, assert_equal -from .testing import test_path +from .testing import assert_equal, generate_test_path import mammoth from mammoth import results -@istest -def docx_containing_one_paragraph_is_converted_to_single_p_element(): - with open(test_path("single-paragraph.docx"), "rb") as fileobj: +def test_docx_containing_one_paragraph_is_converted_to_single_p_element(): + with open(generate_test_path("single-paragraph.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("<p>Walking on imported air</p>", result.value) assert_equal([], result.messages) -@istest -def can_read_xml_files_with_utf8_bom(): - with open(test_path("utf8-bom.docx"), "rb") as fileobj: +def test_can_read_xml_files_with_utf8_bom(): + with open(generate_test_path("utf8-bom.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("<p>This XML has a byte order mark.</p>", result.value) assert_equal([], result.messages) -@istest -def empty_paragraphs_are_ignored_by_default(): - with open(test_path("empty.docx"), "rb") as fileobj: +def test_empty_paragraphs_are_ignored_by_default(): + with open(generate_test_path("empty.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("", result.value) assert_equal([], result.messages) -@istest -def empty_paragraphs_are_preserved_if_ignore_empty_paragraphs_is_false(): - with open(test_path("empty.docx"), "rb") as fileobj: +def test_empty_paragraphs_are_preserved_if_ignore_empty_paragraphs_is_false(): + with open(generate_test_path("empty.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, ignore_empty_paragraphs=False) assert_equal("<p></p>", result.value) assert_equal([], result.messages) -@istest -def embedded_style_map_is_used_if_present(): - with open(test_path("embedded-style-map.docx"), "rb") as fileobj: +def test_embedded_style_map_is_used_if_present(): + with open(generate_test_path("embedded-style-map.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("<h1>Walking on imported air</h1>", result.value) assert_equal([], result.messages) -@istest -def explicit_style_map_takes_precedence_over_embedded_style_map(): - with open(test_path("embedded-style-map.docx"), "rb") as fileobj: +def test_explicit_style_map_takes_precedence_over_embedded_style_map(): + with open(generate_test_path("embedded-style-map.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, style_map="p => p") assert_equal("<p>Walking on imported air</p>", result.value) assert_equal([], result.messages) -@istest -def explicit_style_map_is_combined_with_embedded_style_map(): - with open(test_path("embedded-style-map.docx"), "rb") as fileobj: +def test_explicit_style_map_is_combined_with_embedded_style_map(): + with open(generate_test_path("embedded-style-map.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, style_map="r => strong") assert_equal("<h1><strong>Walking on imported air</strong></h1>", result.value) assert_equal([], result.messages) -@istest -def embedded_style_maps_can_be_disabled(): - with open(test_path("embedded-style-map.docx"), "rb") as fileobj: +def test_embedded_style_maps_can_be_disabled(): + with open(generate_test_path("embedded-style-map.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, include_embedded_style_map=False) assert_equal("<p>Walking on imported air</p>", result.value) assert_equal([], result.messages) -@istest -def embedded_style_map_can_be_written_and_then_read(): +def test_embedded_style_map_can_be_written_and_then_read(): with _copy_of_test_data("single-paragraph.docx") as fileobj: mammoth.embed_style_map(fileobj, "p => h1") result = mammoth.convert_to_html(fileobj=fileobj, ignore_empty_paragraphs=False) @@ -89,68 +79,61 @@ def embedded_style_map_can_be_written_and_then_read(): assert_equal([], result.messages) -@istest -def embedded_style_map_can_be_retrieved(): +def test_embedded_style_map_can_be_retrieved(): with _copy_of_test_data("single-paragraph.docx") as fileobj: mammoth.embed_style_map(fileobj, "p => h1") assert_equal("p => h1", mammoth.read_embedded_style_map(fileobj)) -@istest -def warning_if_style_mapping_is_not_understood(): +def test_warning_if_style_mapping_is_not_understood(): style_map = """ !!!! p => h1""" - with open(test_path("single-paragraph.docx"), "rb") as fileobj: + with open(generate_test_path("single-paragraph.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, style_map=style_map) assert_equal("<h1>Walking on imported air</h1>", result.value) warning = "Did not understand this style mapping, so ignored it: !!!!" assert_equal([results.warning(warning)], result.messages) -@istest -def inline_images_referenced_by_path_relative_to_part_are_included_in_output(): - with open(test_path("tiny-picture.docx"), "rb") as fileobj: +def test_inline_images_referenced_by_path_relative_to_part_are_included_in_output(): + with open(generate_test_path("tiny-picture.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value) assert_equal([], result.messages) -@istest -def inline_images_referenced_by_path_relative_to_base_are_included_in_output(): - with open(test_path("tiny-picture-target-base-relative.docx"), "rb") as fileobj: +def test_inline_images_referenced_by_path_relative_to_base_are_included_in_output(): + with open(generate_test_path("tiny-picture-target-base-relative.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value) assert_equal([], result.messages) -@istest -def images_stored_outside_of_document_are_included_in_output(): - with open(test_path("external-picture.docx"), "rb") as fileobj: +def test_images_stored_outside_of_document_are_included_in_output(): + with open(generate_test_path("external-picture.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value) assert_equal([], result.messages) -@istest -def warn_if_images_stored_outside_of_document_are_specified_when_passing_fileobj_without_name(): +def test_warn_if_images_stored_outside_of_document_are_specified_when_passing_fileobj_without_name(): fileobj = io.BytesIO() - with open(test_path("external-picture.docx"), "rb") as source_fileobj: + with open(generate_test_path("external-picture.docx"), "rb") as source_fileobj: shutil.copyfileobj(source_fileobj, fileobj) - + result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("", result.value) assert_equal([results.warning("could not find external image 'tiny-picture.png', fileobj has no name")], result.messages) -@istest -def warn_if_images_stored_outside_of_document_are_not_found(): +def test_warn_if_images_stored_outside_of_document_are_not_found(): with tempman.create_temp_dir() as temp_dir: document_path = os.path.join(temp_dir.path, "document.docx") with open(document_path, "wb") as fileobj: - with open(test_path("external-picture.docx"), "rb") as source_fileobj: + with open(generate_test_path("external-picture.docx"), "rb") as source_fileobj: shutil.copyfileobj(source_fileobj, fileobj) - + with open(document_path, "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("", result.value) @@ -160,90 +143,83 @@ def warn_if_images_stored_outside_of_document_are_not_found(): assert_equal(1, len(result.messages)) -@istest -def image_conversion_can_be_customised(): +def test_image_conversion_can_be_customised(): @mammoth.images.img_element def convert_image(image): with image.open() as image_bytes: encoded_src = base64.b64encode(image_bytes.read()).decode("ascii") - + return { "src": encoded_src[:2] + "," + image.content_type } - - with open(test_path("tiny-picture.docx"), "rb") as fileobj: + + with open(generate_test_path("tiny-picture.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, convert_image=convert_image) assert_equal("""<p><img src="iV,image/png" /></p>""", result.value) assert_equal([], result.messages) - -@istest -def simple_list_is_converted_to_list_elements(): - with open(test_path("simple-list.docx"), "rb") as fileobj: + +def test_simple_list_is_converted_to_list_elements(): + with open(generate_test_path("simple-list.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal([], result.messages) assert_equal("<ul><li>Apple</li><li>Banana</li></ul>", result.value) -@istest -def word_tables_are_converted_to_html_tables(): +def test_word_tables_are_converted_to_html_tables(): expected_html = ("<p>Above</p>" + "<table>" + "<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" + "<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" + "</table>" + "<p>Below</p>") - - - with open(test_path("tables.docx"), "rb") as fileobj: + + + with open(generate_test_path("tables.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal([], result.messages) assert_equal(expected_html, result.value) -@istest -def footnotes_are_appended_to_text(): +def test_footnotes_are_appended_to_text(): # TODO: don't duplicate footnotes with multiple references expected_html = ('<p>Ouch' + '<sup><a href="#doc-42-footnote-1" id="doc-42-footnote-ref-1">[1]</a></sup>.' + '<sup><a href="#doc-42-footnote-2" id="doc-42-footnote-ref-2">[2]</a></sup></p>' + '<ol><li id="doc-42-footnote-1"><p> A tachyon walks into a bar. <a href="#doc-42-footnote-ref-1">↑</a></p></li>' + '<li id="doc-42-footnote-2"><p> Fin. <a href="#doc-42-footnote-ref-2">↑</a></p></li></ol>') - - with open(test_path("footnotes.docx"), "rb") as fileobj: + + with open(generate_test_path("footnotes.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42-") assert_equal([], result.messages) assert_equal(expected_html, result.value) -@istest -def endnotes_are_appended_to_text(): +def test_endnotes_are_appended_to_text(): expected_html = ('<p>Ouch' + '<sup><a href="#doc-42-endnote-2" id="doc-42-endnote-ref-2">[1]</a></sup>.' + '<sup><a href="#doc-42-endnote-3" id="doc-42-endnote-ref-3">[2]</a></sup></p>' + '<ol><li id="doc-42-endnote-2"><p> A tachyon walks into a bar. <a href="#doc-42-endnote-ref-2">↑</a></p></li>' + '<li id="doc-42-endnote-3"><p> Fin. <a href="#doc-42-endnote-ref-3">↑</a></p></li></ol>') - - with open(test_path("endnotes.docx"), "rb") as fileobj: + + with open(generate_test_path("endnotes.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42-") assert_equal([], result.messages) assert_equal(expected_html, result.value) -@istest -def relationships_are_handled_properly_in_footnotes(): +def test_relationships_are_handled_properly_in_footnotes(): expected_html = ( '<p><sup><a href="#doc-42-footnote-1" id="doc-42-footnote-ref-1">[1]</a></sup></p>' + '<ol><li id="doc-42-footnote-1"><p> <a href="http://www.example.com">Example</a> <a href="#doc-42-footnote-ref-1">↑</a></p></li></ol>') - - with open(test_path("footnote-hyperlink.docx"), "rb") as fileobj: + + with open(generate_test_path("footnote-hyperlink.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42-") assert_equal([], result.messages) assert_equal(expected_html, result.value) -@istest -def when_style_mapping_is_defined_for_comment_references_then_comments_are_included(): +def test_when_style_mapping_is_defined_for_comment_references_then_comments_are_included(): expected_html = ( '<p>Ouch' + '<sup><a href="#doc-42-comment-0" id="doc-42-comment-ref-0">[MW1]</a></sup>.' + @@ -251,89 +227,80 @@ def when_style_mapping_is_defined_for_comment_references_then_comments_are_inclu '<dl><dt id="doc-42-comment-0">Comment [MW1]</dt><dd><p>A tachyon walks into a bar. <a href="#doc-42-comment-ref-0">↑</a></p></dd>' + '<dt id="doc-42-comment-2">Comment [MW2]</dt><dd><p>Fin. <a href="#doc-42-comment-ref-2">↑</a></p></dd></dl>' ) - - with open(test_path("comments.docx"), "rb") as fileobj: + + with open(generate_test_path("comments.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42-", style_map="comment-reference => sup") assert_equal([], result.messages) assert_equal(expected_html, result.value) -@istest -def text_boxes_are_read(): - with open(test_path("text-box.docx"), "rb") as fileobj: +def test_text_boxes_are_read(): + with open(generate_test_path("text-box.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal('<p>Datum plane</p>', result.value) -@istest -def underline_is_ignored_by_default(): - with open(test_path("underline.docx"), "rb") as fileobj: +def test_underline_is_ignored_by_default(): + with open(generate_test_path("underline.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal('<p><strong>The Sunset Tree</strong></p>', result.value) -@istest -def underline_can_be_configured_with_style_mapping(): - with open(test_path("underline.docx"), "rb") as fileobj: +def test_underline_can_be_configured_with_style_mapping(): + with open(generate_test_path("underline.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, style_map="u => em") assert_equal('<p><strong>The <em>Sunset</em> Tree</strong></p>', result.value) -@istest -def strikethrough_is_converted_to_s_element_by_default(): - with open(test_path("strikethrough.docx"), "rb") as fileobj: +def test_strikethrough_is_converted_to_s_element_by_default(): + with open(generate_test_path("strikethrough.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("<p><s>Today's Special: Salmon</s> Sold out</p>", result.value) -@istest -def strikethrough_conversion_can_be_configured_with_style_mapping(): - with open(test_path("strikethrough.docx"), "rb") as fileobj: +def test_strikethrough_conversion_can_be_configured_with_style_mapping(): + with open(generate_test_path("strikethrough.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, style_map="strike => del") assert_equal("<p><del>Today's Special: Salmon</del> Sold out</p>", result.value) -@istest -def transform_document_is_applied_to_document_before_conversion(): +def test_transform_document_is_applied_to_document_before_conversion(): def transform_document(document): document.children[0].style_id = "Heading1" return document - - with open(test_path("single-paragraph.docx"), "rb") as fileobj: + + with open(generate_test_path("single-paragraph.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, transform_document=transform_document) assert_equal("<h1>Walking on imported air</h1>", result.value) assert_equal([], result.messages) -@istest -def paragraph_transform_only_transforms_paragraphs(): +def test_paragraph_transform_only_transforms_paragraphs(): def transform_paragraph(paragraph): return paragraph.copy(style_id="Heading1") transform_document = mammoth.transforms.paragraph(transform_paragraph) - with open(test_path("single-paragraph.docx"), "rb") as fileobj: + with open(generate_test_path("single-paragraph.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj, transform_document=transform_document) assert_equal("<h1>Walking on imported air</h1>", result.value) assert_equal([], result.messages) -@istest -def docx_containing_one_paragraph_can_be_converted_to_markdown(): - with open(test_path("single-paragraph.docx"), "rb") as fileobj: +def test_docx_containing_one_paragraph_can_be_converted_to_markdown(): + with open(generate_test_path("single-paragraph.docx"), "rb") as fileobj: result = mammoth.convert_to_markdown(fileobj=fileobj) assert_equal("Walking on imported air\n\n", result.value) assert_equal([], result.messages) -@istest -def can_extract_raw_text(): - with open(test_path("simple-list.docx"), "rb") as fileobj: +def test_can_extract_raw_text(): + with open(generate_test_path("simple-list.docx"), "rb") as fileobj: result = mammoth.extract_raw_text(fileobj=fileobj) assert_equal([], result.messages) assert_equal("Apple\n\nBanana\n\n", result.value) - + def _copy_of_test_data(path): destination = io.BytesIO() - with open(test_path(path), "rb") as source: + with open(generate_test_path(path), "rb") as source: shutil.copyfileobj(source, destination) return destination diff --git a/tests/options_tests.py b/tests/options_tests.py index 182678e..e3d09ef 100644 --- a/tests/options_tests.py +++ b/tests/options_tests.py @@ -1,16 +1,13 @@ -from nose.tools import istest, assert_equal - from mammoth.options import read_options, _default_style_map from mammoth.styles.parser import read_style_mapping +from .testing import assert_equal -@istest -def default_style_map_is_used_if_style_map_is_not_set(): +def test_default_style_map_is_used_if_style_map_is_not_set(): assert_equal(_default_style_map, read_options({}).value["style_map"]) -@istest -def custom_style_mappings_are_prepended_to_default_style_mappings(): +def test_custom_style_mappings_are_prepended_to_default_style_mappings(): style_map = read_options({ "style_map": "p.SectionTitle => h2" }).value["style_map"] @@ -18,8 +15,7 @@ def custom_style_mappings_are_prepended_to_default_style_mappings(): assert_equal(_default_style_map, style_map[1:]) -@istest -def default_style_mappings_are_ignored_if_include_default_style_map_is_false(): +def test_default_style_mappings_are_ignored_if_include_default_style_map_is_false(): style_map = read_options({ "style_map": "p.SectionTitle => h2", "include_default_style_map": False @@ -27,8 +23,7 @@ def default_style_mappings_are_ignored_if_include_default_style_map_is_false(): assert_equal([read_style_mapping("p.SectionTitle => h2").value], style_map) -@istest -def lines_starting_with_hash_in_custom_style_map_are_ignored(): +def test_lines_starting_with_hash_in_custom_style_map_are_ignored(): style_map = read_options({ "style_map": "#p.SectionTitle => h3\np.SectionTitle => h2", "include_default_style_map": False diff --git a/tests/raw_text_tests.py b/tests/raw_text_tests.py index 7dd44a0..bdb1395 100644 --- a/tests/raw_text_tests.py +++ b/tests/raw_text_tests.py @@ -1,11 +1,9 @@ -from nose.tools import istest, assert_equal - from mammoth.raw_text import extract_raw_text_from_element from mammoth import documents +from .testing import assert_equal -@istest -def text_element_is_converted_to_text_content(): +def test_text_element_is_converted_to_text_content(): element = documents.Text("Hello.") result = extract_raw_text_from_element(element) @@ -13,8 +11,7 @@ def text_element_is_converted_to_text_content(): assert_equal("Hello.", result) -@istest -def tab_element_is_converted_to_tab_character(): +def test_tab_element_is_converted_to_tab_character(): element = documents.tab() result = extract_raw_text_from_element(element) @@ -22,8 +19,7 @@ def tab_element_is_converted_to_tab_character(): assert_equal("\t", result) -@istest -def paragraphs_are_terminated_with_newlines(): +def test_paragraphs_are_terminated_with_newlines(): element = documents.paragraph( children=[ documents.Text("Hello "), @@ -36,8 +32,7 @@ def paragraphs_are_terminated_with_newlines(): assert_equal("Hello world.\n\n", result) -@istest -def children_are_recursively_converted_to_text(): +def test_children_are_recursively_converted_to_text(): element = documents.document([ documents.paragraph( [ @@ -53,8 +48,7 @@ def children_are_recursively_converted_to_text(): assert_equal("Hello world.\n\n", result) -@istest -def non_text_element_without_children_is_converted_to_empty_string(): +def test_non_text_element_without_children_is_converted_to_empty_string(): element = documents.line_break assert not hasattr(element, "children") diff --git a/tests/styles/document_matcher_tests.py b/tests/styles/document_matcher_tests.py index dccf4f1..f9034ce 100644 --- a/tests/styles/document_matcher_tests.py +++ b/tests/styles/document_matcher_tests.py @@ -1,17 +1,14 @@ -from nose.tools import istest, assert_equal - from mammoth import document_matchers +from ..testing import assert_equal -@istest -def equal_to_matcher_is_case_insensitive(): +def test_equal_to_matcher_is_case_insensitive(): matcher = document_matchers.equal_to("Heading 1") assert_equal(True, matcher.matches("heaDING 1")) assert_equal(False, matcher.matches("heaDING 2")) -@istest -def starts_with_matcher_matches_string_with_prefix(): +def test_starts_with_matcher_matches_string_with_prefix(): matcher = document_matchers.starts_with("Heading") assert_equal(True, matcher.matches("Heading")) assert_equal(True, matcher.matches("Heading 1")) @@ -20,7 +17,6 @@ def starts_with_matcher_matches_string_with_prefix(): assert_equal(False, matcher.matches("Header 2")) -@istest -def starts_with_matcher_is_case_insensitive(): +def test_starts_with_matcher_is_case_insensitive(): matcher = document_matchers.starts_with("Heading") assert_equal(True, matcher.matches("heaDING")) diff --git a/tests/styles/parser/document_matcher_parser_tests.py b/tests/styles/parser/document_matcher_parser_tests.py index 18233d2..1808571 100644 --- a/tests/styles/parser/document_matcher_parser_tests.py +++ b/tests/styles/parser/document_matcher_parser_tests.py @@ -1,200 +1,172 @@ -from nose.tools import istest, assert_equal - from mammoth import documents, document_matchers from mammoth.styles.parser.document_matcher_parser import parse_document_matcher from mammoth.styles.parser.errors import LineParseError from mammoth.styles.parser.tokeniser import tokenise from mammoth.styles.parser.token_iterator import TokenIterator -from ...testing import assert_raises +from ...testing import assert_equal, assert_raises -@istest -def unrecognised_document_element_raises_error(): +def test_unrecognised_document_element_raises_error(): error = assert_raises(LineParseError, lambda: read_document_matcher("x")) assert_equal("Unrecognised document element: x", str(error)) -@istest -def reads_plain_paragraph(): +def test_reads_plain_paragraph(): assert_equal( document_matchers.paragraph(), read_document_matcher("p") ) -@istest -def reads_paragraph_with_style_id(): +def test_reads_paragraph_with_style_id(): assert_equal( document_matchers.paragraph(style_id="Heading1"), read_document_matcher("p.Heading1") ) -@istest -def reads_paragraph_with_exact_style_name(): +def test_reads_paragraph_with_exact_style_name(): assert_equal( document_matchers.paragraph(style_name=document_matchers.equal_to("Heading 1")), read_document_matcher("p[style-name='Heading 1']") ) -@istest -def reads_paragraph_with_style_name_prefix(): +def test_reads_paragraph_with_style_name_prefix(): assert_equal( document_matchers.paragraph(style_name=document_matchers.starts_with("Heading")), read_document_matcher("p[style-name^='Heading']") ) -@istest -def unrecognised_string_matcher_raises_error(): +def test_unrecognised_string_matcher_raises_error(): error = assert_raises(LineParseError, lambda: read_document_matcher("p[style-name*='Heading']")) assert_equal("Unrecognised string matcher: *", str(error)) -@istest -def reads_paragraph_ordered_list(): +def test_reads_paragraph_ordered_list(): assert_equal( document_matchers.paragraph(numbering=documents.numbering_level(1, is_ordered=True)), read_document_matcher("p:ordered-list(2)") ) -@istest -def reads_paragraph_unordered_list(): +def test_reads_paragraph_unordered_list(): assert_equal( document_matchers.paragraph(numbering=documents.numbering_level(1, is_ordered=False)), read_document_matcher("p:unordered-list(2)") ) -@istest -def unrecognised_list_type_raises_error(): +def test_unrecognised_list_type_raises_error(): error = assert_raises(LineParseError, lambda: read_document_matcher("p:blah")) assert_equal("Unrecognised list type: blah", str(error)) -@istest -def reads_plain_run(): +def test_reads_plain_run(): assert_equal( document_matchers.run(), read_document_matcher("r") ) -@istest -def reads_run_with_style_id(): +def test_reads_run_with_style_id(): assert_equal( document_matchers.run(style_id="Emphasis"), read_document_matcher("r.Emphasis") ) -@istest -def reads_run_with_style_name(): +def test_reads_run_with_style_name(): assert_equal( document_matchers.run(style_name=document_matchers.equal_to("Emphasis")), read_document_matcher("r[style-name='Emphasis']") ) -@istest -def reads_plain_table(): +def test_reads_plain_table(): assert_equal( document_matchers.table(), read_document_matcher("table") ) -@istest -def reads_table_with_style_id(): +def test_reads_table_with_style_id(): assert_equal( document_matchers.table(style_id="TableNormal"), read_document_matcher("table.TableNormal") ) -@istest -def reads_table_with_style_name(): +def test_reads_table_with_style_name(): assert_equal( document_matchers.table(style_name=document_matchers.equal_to("Normal Table")), read_document_matcher("table[style-name='Normal Table']") ) -@istest -def reads_bold(): +def test_reads_bold(): assert_equal( document_matchers.bold, read_document_matcher("b") ) -@istest -def reads_italic(): +def test_reads_italic(): assert_equal( document_matchers.italic, read_document_matcher("i") ) -@istest -def reads_underline(): +def test_reads_underline(): assert_equal( document_matchers.underline, read_document_matcher("u") ) -@istest -def reads_strikethrough(): +def test_reads_strikethrough(): assert_equal( document_matchers.strikethrough, read_document_matcher("strike") ) -@istest -def reads_all_caps(): +def test_reads_all_caps(): assert_equal( document_matchers.all_caps, read_document_matcher("all-caps") ) -@istest -def reads_small_caps(): +def test_reads_small_caps(): assert_equal( document_matchers.small_caps, read_document_matcher("small-caps") ) -@istest -def reads_comment_reference(): +def test_reads_comment_reference(): assert_equal( document_matchers.comment_reference, read_document_matcher("comment-reference") ) -@istest -def reads_line_breaks(): +def test_reads_line_breaks(): assert_equal( document_matchers.line_break, read_document_matcher("br[type='line']"), ) -@istest -def reads_page_breaks(): +def test_reads_page_breaks(): assert_equal( document_matchers.page_break, read_document_matcher("br[type='page']"), ) -@istest -def reads_column_breaks(): +def test_reads_column_breaks(): assert_equal( document_matchers.column_break, read_document_matcher("br[type='column']"), ) -@istest -def unrecognised_break_type_raises_error(): +def test_unrecognised_break_type_raises_error(): error = assert_raises(LineParseError, lambda: read_document_matcher("br[type='unknownBreakType']")) assert_equal("Unrecognised break type: unknownBreakType", str(error)) diff --git a/tests/styles/parser/html_path_parser_tests.py b/tests/styles/parser/html_path_parser_tests.py index 1e34ea1..0c2ef3a 100644 --- a/tests/styles/parser/html_path_parser_tests.py +++ b/tests/styles/parser/html_path_parser_tests.py @@ -1,84 +1,73 @@ -from nose.tools import istest, assert_equal - from mammoth import html_paths from mammoth.styles.parser.html_path_parser import parse_html_path from mammoth.styles.parser.tokeniser import tokenise from mammoth.styles.parser.token_iterator import TokenIterator +from ...testing import assert_equal -@istest -def can_read_empty_path(): +def test_can_read_empty_path(): assert_equal( html_paths.empty, read_html_path("") ) -@istest -def can_read_single_element(): +def test_can_read_single_element(): assert_equal( html_paths.path([html_paths.element(["p"])]), read_html_path("p") ) -@istest -def can_read_choice_of_two_elements(): +def test_can_read_choice_of_two_elements(): assert_equal( html_paths.path([html_paths.element(["ul", "ol"])]), read_html_path("ul|ol") ) -@istest -def can_read_choice_of_three_elements(): +def test_can_read_choice_of_three_elements(): assert_equal( html_paths.path([html_paths.element(["ul", "ol", "p"])]), read_html_path("ul|ol|p") ) -@istest -def can_read_nested_elements(): +def test_can_read_nested_elements(): assert_equal( html_paths.path([html_paths.element(["ul"]), html_paths.element(["li"])]), read_html_path("ul > li") ) -@istest -def can_read_class_on_element(): +def test_can_read_class_on_element(): assert_equal( html_paths.path([html_paths.element(["p"], class_names=["tip"])]), read_html_path("p.tip") ) -@istest -def can_read_multiple_classes_on_element(): +def test_can_read_multiple_classes_on_element(): assert_equal( html_paths.path([html_paths.element(["p"], class_names=["tip", "help"])]), read_html_path("p.tip.help") ) -@istest -def can_read_when_element_must_be_fresh(): +def test_can_read_when_element_must_be_fresh(): assert_equal( html_paths.path([html_paths.element(["p"], fresh=True)]), read_html_path("p:fresh") ) -@istest -def can_read_separator_for_elements(): +def test_can_read_separator_for_elements(): assert_equal( html_paths.path([html_paths.element(["p"], separator="x")]), read_html_path("p:separator('x')") ) -@istest -def can_read_ignore_element(): +def test_can_read_ignore_element(): assert_equal( html_paths.ignore, read_html_path("!") diff --git a/tests/styles/parser/style_mapping_parser_tests.py b/tests/styles/parser/style_mapping_parser_tests.py index 7b5b299..f2b92c0 100644 --- a/tests/styles/parser/style_mapping_parser_tests.py +++ b/tests/styles/parser/style_mapping_parser_tests.py @@ -1,13 +1,11 @@ -from nose.tools import istest, assert_equal - from mammoth import html_paths, document_matchers, styles from mammoth.styles.parser.style_mapping_parser import parse_style_mapping from mammoth.styles.parser.tokeniser import tokenise from mammoth.styles.parser.token_iterator import TokenIterator +from ...testing import assert_equal -@istest -def document_matcher_is_mapped_to_html_path_using_fat_arrow(): +def test_document_matcher_is_mapped_to_html_path_using_fat_arrow(): assert_equal( styles.style(document_matchers.paragraph(), html_paths.path([html_paths.element(["h1"])])), read_style_mapping("p => h1") diff --git a/tests/styles/parser/token_parser_tests.py b/tests/styles/parser/token_parser_tests.py index 0e278c7..3192dc9 100644 --- a/tests/styles/parser/token_parser_tests.py +++ b/tests/styles/parser/token_parser_tests.py @@ -1,12 +1,10 @@ -from nose.tools import istest, assert_equal - from mammoth.styles.parser.tokeniser import Token, TokenType from mammoth.styles.parser.token_parser import decode_escape_sequences, parse_identifier, parse_string from mammoth.styles.parser.token_iterator import TokenIterator +from ...testing import assert_equal -@istest -def escape_sequences_in_identifiers_are_decoded(): +def test_escape_sequences_in_identifiers_are_decoded(): assert_equal( ":", parse_identifier(TokenIterator([ @@ -15,8 +13,7 @@ def escape_sequences_in_identifiers_are_decoded(): ) -@istest -def escape_sequences_in_strings_are_decoded(): +def test_escape_sequences_in_strings_are_decoded(): assert_equal( "\n", parse_string(TokenIterator([ @@ -25,26 +22,21 @@ def escape_sequences_in_strings_are_decoded(): ) -@istest -def line_feeds_are_decoded(): +def test_line_feeds_are_decoded(): assert_equal("\n", decode_escape_sequences(r"\n")) -@istest -def carriage_returns_are_decoded(): +def test_carriage_returns_are_decoded(): assert_equal("\r", decode_escape_sequences(r"\r")) -@istest -def tabs_are_decoded(): +def test_tabs_are_decoded(): assert_equal("\t", decode_escape_sequences(r"\t")) -@istest -def backslashes_are_decoded(): +def test_backslashes_are_decoded(): assert_equal("\\", decode_escape_sequences(r"\\")) -@istest -def colons_are_decoded(): +def test_colons_are_decoded(): assert_equal(":", decode_escape_sequences(r"\:")) diff --git a/tests/testing.py b/tests/testing.py index c5013dc..e75d630 100644 --- a/tests/testing.py +++ b/tests/testing.py @@ -1,18 +1,21 @@ import os -from nose.tools import nottest +from precisely import assert_that, equal_to -@nottest -def test_path(path): +def generate_test_path(path): this_dir = os.path.dirname(__file__) return os.path.join(this_dir, "test-data", path) +def assert_equal(expected, actual): + assert_that(actual, equal_to(expected)) + + def assert_raises(exception, func): try: func() assert False, "Expected " + exception.__name__ except exception as error: return error - + diff --git a/tests/transforms_tests.py b/tests/transforms_tests.py index d0604f8..94ce79a 100644 --- a/tests/transforms_tests.py +++ b/tests/transforms_tests.py @@ -1,61 +1,53 @@ import cobble -from nose.tools import assert_equal, istest from mammoth import documents, transforms from mammoth.transforms import get_descendants, get_descendants_of_type, _each_element +from .testing import assert_equal -@istest class ParagraphTests(object): - @istest - def paragraph_is_transformed(self): + def test_paragraph_is_transformed(self): paragraph = documents.paragraph(children=[]) result = transforms.paragraph(lambda _: documents.tab())(paragraph) assert_equal(documents.tab(), result) - - @istest - def non_paragraph_elements_are_not_transformed(self): + + def test_non_paragraph_elements_are_not_transformed(self): run = documents.run(children=[]) result = transforms.paragraph(lambda _: documents.tab())(run) assert_equal(documents.run(children=[]), result) -@istest class RunTests(object): - @istest - def run_is_transformed(self): + def test_run_is_transformed(self): run = documents.run(children=[]) result = transforms.run(lambda _: documents.tab())(run) assert_equal(documents.tab(), result) - - @istest - def non_paragraph_elements_are_not_transformed(self): + + def test_non_paragraph_elements_are_not_transformed(self): paragraph = documents.paragraph(children=[]) result = transforms.run(lambda _: documents.tab())(paragraph) assert_equal(documents.paragraph(children=[]), result) -@istest class EachElementTests(object): - @istest - def all_descendants_are_transformed(self): + def test_all_descendants_are_transformed(self): @cobble.data class Count(documents.HasChildren): count = cobble.field() - + root = Count(count=None, children=[ Count(count=None, children=[ Count(count=None, children=[]), ]), ]) - + current_count = [0] def set_count(node): current_count[0] += 1 return node.copy(count=current_count[0]) - + result = _each_element(set_count)(root) - + assert_equal(Count(count=3, children=[ Count(count=2, children=[ Count(count=1, children=[]), @@ -63,34 +55,27 @@ class EachElementTests(object): ]), result) -@istest class GetDescendantsTests(object): - @istest - def returns_nothing_if_element_type_has_no_children(self): + def test_returns_nothing_if_element_type_has_no_children(self): assert_equal([], get_descendants(documents.tab())) - - @istest - def returns_nothing_if_element_has_empty_children(self): + + def test_returns_nothing_if_element_has_empty_children(self): assert_equal([], get_descendants(documents.paragraph(children=[]))) - - @istest - def includes_children(self): + + def test_includes_children(self): children = [documents.text("child 1"), documents.text("child 2")] element = documents.paragraph(children=children) assert_equal(children, get_descendants(element)) - - @istest - def includes_indirect_descendants(self): + + def test_includes_indirect_descendants(self): grandchild = documents.text("grandchild") child = documents.run(children=[grandchild]) element = documents.paragraph(children=[child]) assert_equal([grandchild, child], get_descendants(element)) -@istest class GetDescendantsOfTypeTests(object): - @istest - def filters_descendants_to_type(self): + def test_filters_descendants_to_type(self): tab = documents.tab() run = documents.run(children=[]) element = documents.paragraph(children=[tab, run]) diff --git a/tests/writers/markdown_tests.py b/tests/writers/markdown_tests.py index 27d6d56..7083fef 100644 --- a/tests/writers/markdown_tests.py +++ b/tests/writers/markdown_tests.py @@ -1,19 +1,16 @@ from __future__ import unicode_literals -from nose.tools import istest, assert_equal - from mammoth.writers.markdown import MarkdownWriter +from ..testing import assert_equal -@istest -def special_markdown_characters_are_escaped(): +def test_special_markdown_characters_are_escaped(): writer = _create_writer() writer.text(r"\*") assert_equal(r"\\\*", writer.as_string()) -@istest -def unrecognised_elements_are_treated_as_normal_text(): +def test_unrecognised_elements_are_treated_as_normal_text(): writer = _create_writer() writer.start("blah"); writer.text("Hello"); @@ -21,8 +18,7 @@ def unrecognised_elements_are_treated_as_normal_text(): assert_equal("Hello", writer.as_string()) -@istest -def paragraphs_are_terminated_with_double_new_line(): +def test_paragraphs_are_terminated_with_double_new_line(): writer = _create_writer() writer.start("p"); writer.text("Hello"); @@ -30,8 +26,7 @@ def paragraphs_are_terminated_with_double_new_line(): assert_equal("Hello\n\n", writer.as_string()) -@istest -def h1_elements_are_converted_to_heading_with_leading_hash(): +def test_h1_elements_are_converted_to_heading_with_leading_hash(): writer = _create_writer() writer.start("h1"); writer.text("Hello"); @@ -39,8 +34,7 @@ def h1_elements_are_converted_to_heading_with_leading_hash(): assert_equal("# Hello\n\n", writer.as_string()) -@istest -def h6_elements_are_converted_to_heading_with_six_leading_hashes(): +def test_h6_elements_are_converted_to_heading_with_six_leading_hashes(): writer = _create_writer() writer.start("h6"); writer.text("Hello"); @@ -48,16 +42,14 @@ def h6_elements_are_converted_to_heading_with_six_leading_hashes(): assert_equal("###### Hello\n\n", writer.as_string()) -@istest -def br_is_written_as_two_spaces_followed_by_newline(): +def test_br_is_written_as_two_spaces_followed_by_newline(): writer = _create_writer() writer.text("Hello"); writer.self_closing("br"); assert_equal("Hello \n", writer.as_string()) -@istest -def strong_text_is_surrounded_by_two_underscores(): +def test_strong_text_is_surrounded_by_two_underscores(): writer = _create_writer() writer.text("Hello "); writer.start("strong"); @@ -66,8 +58,7 @@ def strong_text_is_surrounded_by_two_underscores(): assert_equal("Hello __World__", writer.as_string()) -@istest -def emphasised_text_is_surrounded_by_one_asterix(): +def test_emphasised_text_is_surrounded_by_one_asterix(): writer = _create_writer() writer.text("Hello "); writer.start("em"); @@ -76,72 +67,63 @@ def emphasised_text_is_surrounded_by_one_asterix(): assert_equal("Hello *World*", writer.as_string()) -@istest -def anchor_tags_are_written_as_hyperlinks(): +def test_anchor_tags_are_written_as_hyperlinks(): writer = _create_writer() writer.start("a", {"href": "http://example.com"}); writer.text("Hello"); writer.end("a"); assert_equal("[Hello](http://example.com)", writer.as_string()) - -@istest -def anchor_tags_without_href_attribute_are_treated_as_ordinary_text(): + +def test_anchor_tags_without_href_attribute_are_treated_as_ordinary_text(): writer = _create_writer() writer.start("a"); writer.text("Hello"); writer.end("a"); assert_equal("Hello", writer.as_string()) - -@istest -def elements_with_ids_have_anchor_tags_with_ids_appended_to_start_of_markdown_element(): + +def test_elements_with_ids_have_anchor_tags_with_ids_appended_to_start_of_markdown_element(): writer = _create_writer() writer.start("h1", {"id": "start"}) writer.text("Hello") writer.end("h1") assert_equal('# <a id="start"></a>Hello\n\n', writer.as_string()) - -@istest -def links_have_anchors_before_opening_square_bracket(): + +def test_links_have_anchors_before_opening_square_bracket(): writer = _create_writer() writer.start("a", {"href": "http://example.com", "id": "start"}) writer.text("Hello") writer.end("a") assert_equal('<a id="start"></a>[Hello](http://example.com)', writer.as_string()) - -@istest -def image_elements_are_written_as_markdown_images(): + +def test_image_elements_are_written_as_markdown_images(): writer = _create_writer() writer.self_closing("img", {"src": "http://example.com/image.jpg", "alt": "Alt Text"}) assert_equal("", writer.as_string()) - -@istest -def images_are_written_even_if_they_dont_have_alt_text(): + +def test_images_are_written_even_if_they_dont_have_alt_text(): writer = _create_writer() writer.self_closing("img", {"src": "http://example.com/image.jpg"}) assert_equal("", writer.as_string()) - -@istest -def images_are_written_even_if_they_dont_have_a_src_attribute(): + +def test_images_are_written_even_if_they_dont_have_a_src_attribute(): writer = _create_writer() writer.self_closing("img", {"alt": "Alt Text"}) assert_equal("![Alt Text]()", writer.as_string()) - -@istest -def image_elements_are_ignored_if_they_have_no_src_and_no_alt_text(): + +def test_image_elements_are_ignored_if_they_have_no_src_and_no_alt_text(): writer = _create_writer() writer.self_closing("img") assert_equal("", writer.as_string()) -@istest -def list_item_outside_of_list_is_treated_as_unordered_list(): +def test_list_item_outside_of_list_is_treated_as_unordered_list(): writer = _create_writer() writer.start("li") writer.text("Fruit") @@ -149,8 +131,7 @@ def list_item_outside_of_list_is_treated_as_unordered_list(): assert_equal("- Fruit\n", writer.as_string()) -@istest -def ol_element_is_written_as_ordered_list_with_sequential_numbering(): +def test_ol_element_is_written_as_ordered_list_with_sequential_numbering(): writer = _create_writer() writer.start("ol") writer.start("li") @@ -163,8 +144,7 @@ def ol_element_is_written_as_ordered_list_with_sequential_numbering(): assert_equal("1. Fruit\n2. Condiments\n\n", writer.as_string()) -@istest -def ul_element_is_written_as_unordered_list_using_hyphens_as_bullets(): +def test_ul_element_is_written_as_unordered_list_using_hyphens_as_bullets(): writer = _create_writer() writer.start("ul") writer.start("li") @@ -177,11 +157,10 @@ def ul_element_is_written_as_unordered_list_using_hyphens_as_bullets(): assert_equal("- Fruit\n- Condiments\n\n", writer.as_string()) -@istest -def numbering_is_separate_for_nested_list_and_parent_list(): +def test_numbering_is_separate_for_nested_list_and_parent_list(): writer = _create_writer() writer.start("ol") - + writer.start("li") writer.text("Fruit") writer.start("ol") @@ -193,13 +172,13 @@ def numbering_is_separate_for_nested_list_and_parent_list(): writer.end("li") writer.end("ol") writer.end("li") - + writer.start("li") writer.text("Condiments") writer.end("li") writer.end("ol") assert_equal("1. Fruit\n\t1. Apple\n\t2. Banana\n2. Condiments\n\n", writer.as_string()) - + def _create_writer(): diff --git a/tests/zips_tests.py b/tests/zips_tests.py index 8d1505f..b443bcd 100644 --- a/tests/zips_tests.py +++ b/tests/zips_tests.py @@ -1,36 +1,30 @@ -from nose.tools import istest, assert_equal - from mammoth import zips +from .testing import assert_equal -@istest -def split_path_splits_zip_paths_on_last_forward_slash(): +def test_split_path_splits_zip_paths_on_last_forward_slash(): assert_equal(("a", "b"), zips.split_path("a/b")) assert_equal(("a/b", "c"), zips.split_path("a/b/c")) assert_equal(("/a/b", "c"), zips.split_path("/a/b/c")) -@istest -def when_path_has_no_forward_slashes_then_split_path_returns_empty_dirname(): +def test_when_path_has_no_forward_slashes_then_split_path_returns_empty_dirname(): assert_equal(("", "name"), zips.split_path("name")) -@istest -def join_path_joins_arguments_with_forward_slashes(): +def test_join_path_joins_arguments_with_forward_slashes(): assert_equal("a/b", zips.join_path("a", "b")) assert_equal("a/b/c", zips.join_path("a/b", "c")) assert_equal("/a/b/c", zips.join_path("/a/b", "c")) -@istest -def empty_parts_are_ignored_when_joining_paths(): +def test_empty_parts_are_ignored_when_joining_paths(): assert_equal("a", zips.join_path("a", "")) assert_equal("b", zips.join_path("", "b")) assert_equal("a/b", zips.join_path("a", "", "b")) -@istest -def when_joining_paths_then_absolute_paths_ignore_earlier_paths(): +def test_when_joining_paths_then_absolute_paths_ignore_earlier_paths(): assert_equal("/b", zips.join_path("a", "/b")) assert_equal("/b/c", zips.join_path("a", "/b", "c")) assert_equal("/b", zips.join_path("/a", "/b")) |
