<s> import mock <EOL> import tempfile <EOL> import shutil <EOL> from os import path <EOL> from zipfile import ZipFile <EOL> from nose . plugins . skip import SkipTest <EOL> from nose . tools import assert_raises <EOL> from docx2html . tests import collapse_html <EOL> from docx2html import convert <EOL> from docx2html . core import ( <EOL> _get_document_data , <EOL> DETECT_FONT_SIZE , <EOL> ) <EOL> from docx2html . exceptions import ( <EOL> ConversionFailed , <EOL> ) <EOL> def assert_html_equal ( actual_html , expected_html ) : <EOL> assert collapse_html ( <EOL> actual_html , <EOL> ) == collapse_html ( <EOL> expected_html <EOL> ) , actual_html <EOL> def test_extract_html ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_nested_list ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_simple_list ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_inline_tags ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_unicode ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert actual_html is not None <EOL> def test_special_chars ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_table_col_row_span ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_nested_table_rowspan ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_nested_tables ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_list_in_table ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_tables_in_lists ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_track_changes_on ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_headers ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def _copy_file_to_tmp_dir ( file_path , filename ) : <EOL> dp = tempfile . mkdtemp ( ) <EOL> new_file_path = path . join ( dp , filename ) <EOL> shutil . copyfile ( file_path , new_file_path ) <EOL> return new_file_path , dp <EOL> def test_split_headers ( ) : <EOL> filename = '<STR_LIT>' <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> new_file_path , _ = _copy_file_to_tmp_dir ( file_path , filename ) <EOL> def image_handler ( * args , ** kwargs ) : <EOL> return '<STR_LIT:test>' <EOL> actual_html = convert ( new_file_path , image_handler = image_handler ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_has_image ( ) : <EOL> filename = '<STR_LIT>' <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> new_file_path , dp = _copy_file_to_tmp_dir ( file_path , filename ) <EOL> actual_html = convert ( new_file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' % dp ) <EOL> def test_has_image_using_image_handler ( ) : <EOL> filename = '<STR_LIT>' <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> new_file_path , _ = _copy_file_to_tmp_dir ( file_path , filename ) <EOL> def image_handler ( * args , ** kwargs ) : <EOL> return '<STR_LIT:test>' <EOL> actual_html = convert ( new_file_path , image_handler = image_handler ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_attachment_is_tiff ( ) : <EOL> filename = '<STR_LIT>' <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> new_file_path , _ = _copy_file_to_tmp_dir ( file_path , filename ) <EOL> try : <EOL> zf = ZipFile ( new_file_path ) <EOL> _ , meta_data = _get_document_data ( zf ) <EOL> finally : <EOL> zf . close ( ) <EOL> image_file = None <EOL> for file_path in meta_data . relationship_dict . values ( ) : <EOL> if file_path . endswith ( '<STR_LIT>' ) : <EOL> image_file = file_path <EOL> assert image_file is not None <EOL> with open ( image_file ) as f : <EOL> magic_number = f . read ( ) [ : <NUM_LIT:4> ] <EOL> assert magic_number == '<STR_LIT>' <EOL> def test_headers_with_full_line_styles ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_convert_p_to_h ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_bigger_font_size_to_header ( ) : <EOL> if not DETECT_FONT_SIZE : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_fake_headings_by_length ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_shift_enter ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_lists_with_styles ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_list_to_header ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_has_title ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def test_upper_alpha_all_bold ( ) : <EOL> file_path = path . join ( <EOL> path . abspath ( path . dirname ( __file__ ) ) , <EOL> '<STR_LIT:..>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> actual_html = convert ( file_path ) <EOL> assert_html_equal ( actual_html , '''<STR_LIT>''' ) <EOL> def _converter ( * args , ** kwargs ) : <EOL> pass <EOL> def test_converter_broken ( ) : <EOL> file_path = '<STR_LIT>' <EOL> assert_raises ( <EOL> ConversionFailed , <EOL> lambda : convert ( file_path , converter = _converter ) , <EOL> ) <EOL> def test_fall_back ( ) : <EOL> file_path = '<STR_LIT>' <EOL> def fall_back ( * args , ** kwargs ) : <EOL> return '<STR_LIT:success>' <EOL> html = convert ( file_path , fall_back = fall_back , converter = _converter ) <EOL> assert html == '<STR_LIT:success>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_html_files ( patch_zip_handler , patch_read ) : <EOL> def raise_assertion ( * args , ** kwargs ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> patch_zip_handler . side_effect = raise_assertion <EOL> def return_text ( * args , ** kwargs ) : <EOL> return '<STR_LIT:test>' <EOL> patch_read . side_effect = return_text <EOL> file_path = '<STR_LIT>' <EOL> html = convert ( file_path ) <EOL> assert html == '<STR_LIT:test>' <EOL> file_path = '<STR_LIT>' <EOL> html = convert ( file_path ) <EOL> assert html == '<STR_LIT:test>' </s>
