a
    ¡˜ëj›  ã                   @  sx   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
 G dd„ dƒZG dd	„ d	ƒZG d
d„ dƒZG dd„ dƒZdS )z@Unit-test suite for the `unstructured.common.html_table` module.é    )ÚannotationsN)Úfragment_fromstring)ÚHtmlCellÚHtmlRowÚ	HtmlTableÚhtmlify_matrix_of_cell_textsc                   @  s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú%Describe_htmlify_matrix_of_cell_textszTUnit-test suite for `unstructured.common.html_table.htmlify_matrix_of_cell_texts()`.c                 C  s    t g d¢g d¢gƒdksJ ‚d S )N)Úcell1Ú Úcell3)r
   Zcell5r
   zZ<table><tr><td>cell1</td><td/><td>cell3</td></tr><tr><td/><td>cell5</td><td/></tr></table>©r   ©Úself© r   úg/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/test_unstructured/common/test_html_table.pyÚ'test_htmlify_matrix_handles_empty_cells   s    ÿzMDescribe_htmlify_matrix_of_cell_texts.test_htmlify_matrix_handles_empty_cellsc                 C  s   t ddggƒdksJ ‚d S )Nz<>&"znewline
zI<table><tr><td>&lt;&gt;&amp;&quot;</td><td>newline<br/></td></tr></table>r   r   r   r   r   Ú.test_htmlify_matrix_handles_special_characters   s    ÿzTDescribe_htmlify_matrix_of_cell_texts.test_htmlify_matrix_handles_special_charactersc                 C  s    t ddgddggƒdksJ ‚d S )Nr	   Zcell2r   Zcell4zY<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>r   r   r   r   r   Ú3test_htmlify_matrix_handles_multiple_rows_and_cells"   s    ÿzYDescribe_htmlify_matrix_of_cell_texts.test_htmlify_matrix_handles_multiple_rows_and_cellsc                 C  s   t g ƒdksJ ‚d S )Nr
   r   r   r   r   r   Ú(test_htmlify_matrix_handles_empty_matrix*   s    zNDescribe_htmlify_matrix_of_cell_texts.test_htmlify_matrix_handles_empty_matrixN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s
   r   c                   @  s�   e Zd ZdZdd„ Zej dg d¢¡ddœdd	„ƒZd
d„ Z	dd„ Z
ej dg d¢¡ddœdd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚDescribeHtmlTablez?Unit-test suite for `unstructured.common.html_table.HtmlTable`.c                 C  s,   t  d¡}t|t ƒsJ ‚|jjdks(J ‚d S )Nú'<table><tr><td>foobar</td></tr></table>Útable©r   Úfrom_html_textÚ
isinstanceZ_tableÚtag©r   Ú
html_tabler   r   r   Úit_can_construct_from_html_text1   s    
z1DescribeHtmlTable.it_can_construct_from_html_textÚ	html_text)r   z4<body><table><tr><td>foobar</td></tr></table></body>zA<html><body><table><tr><td>foobar</td></tr></table></body></html>Ústr)r#   c                 C  s,   t  |¡}t|t ƒsJ ‚|jjdks(J ‚d S )Nr   r   ©r   r#   r!   r   r   r   Ú6it_can_find_a_table_wrapped_in_an_html_or_body_element7   s    	
zHDescribeHtmlTable.it_can_find_a_table_wrapped_in_an_html_or_body_elementc                 C  s<   t jtdd�� t d¡ W d   ƒ n1 s.0    Y  d S )Nz)`html_text` contains no `<table>` element)Úmatchz2<html><body><tr><td>foobar</td></tr></body></html>)ÚpytestÚraisesÚ
ValueErrorr   r   r   r   r   r   Ú:but_it_raises_when_no_table_element_is_present_in_the_htmlE   s    zLDescribeHtmlTable.but_it_raises_when_no_table_element_is_present_in_the_htmlc                 C  s   t  d¡}|jdksJ ‚d S )NzB<table border="1", class="foobar"><tr><td>foobar</td></tr></table>r   ©r   r   Úhtmlr    r   r   r   Ú6it_removes_any_attributes_present_on_the_table_elementI   s    ÿzHDescribeHtmlTable.it_removes_any_attributes_present_on_the_table_element)z6<table><thead><tr><td>foobar</td></tr></thead></table>zE<table><thead><tr><td>foobar</td></tr></thead><tbody></tbody></table>zE<table><tbody><tr><td>foobar</td></tr></tbody><tfoot></tfoot></table>c                 C  s   t  |¡}|jdksJ ‚d S ©Nr   r,   r%   r   r   r   ÚMit_removes_any_thead_tbody_or_tfoot_elements_present_within_the_table_elementO   s    
z_DescribeHtmlTable.it_removes_any_thead_tbody_or_tfoot_elements_present_within_the_table_elementc                 C  s   t  d¡}|jdksJ ‚d S )NzR<table>  <tr><th>a</th><th/><th>b</th></tr>  <tr><td/><td>c</td><td/></tr></table>zN<table><tr><td>a</td><td/><td>b</td></tr><tr><td/><td>c</td><td/></tr></table>r,   r    r   r   r   ÚEit_changes_any_th_elements_to_td_elements_for_cell_element_uniformity]   s    ÿÿzWDescribeHtmlTable.it_changes_any_th_elements_to_td_elements_for_cell_element_uniformityc                 C  s   t  d¡}|jdksJ ‚d S )NzB
  <table>
  <tr>
    <td>	abc   def
ghi </td>
  </tr>
</table>
  z,<table><tr><td>abc def ghi</td></tr></table>r,   r    r   r   r   ÚRit_removes_any_extra_whitespace_between_elements_and_normalizes_whitespace_in_texth   s    ÿzdDescribeHtmlTable.it_removes_any_extra_whitespace_between_elements_and_normalizes_whitespace_in_textc                 C  s"   t dƒ}t|ƒ}|jdksJ ‚d S r/   )r   r   r-   )r   r   r!   r   r   r   Ú3it_can_serialize_the_table_element_to_str_html_textn   s    zEDescribeHtmlTable.it_can_serialize_the_table_element_to_str_html_textc                 C  s´   t  d¡}| ¡ }t|ƒ}t|tƒs(J ‚|jdks6J ‚t|ƒ}t|tƒsLJ ‚|jdksZJ ‚t|ƒ}t|tƒspJ ‚|jdks~J ‚t t	¡� t|ƒ W d   ƒ n1 s¦0    Y  d S )Nz›<table>  <tr><td>abc</td><td>def</td><td>ghi</td></tr>  <tr><td>jkl</td><td>mno</td><td>pqr</td></tr>  <tr><td>stu</td><td>vwx</td><td>yz</td></tr></table>z-<tr><td>abc</td><td>def</td><td>ghi</td></tr>z-<tr><td>jkl</td><td>mno</td><td>pqr</td></tr>z,<tr><td>stu</td><td>vwx</td><td>yz</td></tr>)
r   r   Z	iter_rowsÚnextr   r   r-   r(   r)   ÚStopIteration)r   r!   Zrow_iterÚrowr   r   r   Ú$it_can_iterate_the_rows_in_the_tablet   s    ÿz6DescribeHtmlTable.it_can_iterate_the_rows_in_the_tablec                 C  s   t  d¡}|jdksJ ‚d S )Nz’<table>  <tr><th> a
 b  c  </th><th/><th>def</th></tr>  <tr><td>gh 	i</td><td/><td>
 jk l </td></tr>  <tr><td/><td> m n op
</td><td/></tr></table>za b c def gh i jk l m n op)r   r   Útextr    r   r   r   Ú>it_provides_access_to_the_clear_concatenated_text_of_the_tableŽ   s    ÿzPDescribeHtmlTable.it_provides_access_to_the_clear_concatenated_text_of_the_tableN)r   r   r   r   r"   r(   ÚmarkÚparametrizer&   r+   r.   r0   r1   r2   r3   r7   r9   r   r   r   r   r   .   s&   þþr   c                   @  s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚDescribeHtmlRowz=Unit-test suite for `unstructured.common.html_table.HtmlRow`.c                 C  s   t tdƒƒjdksJ ‚d S )Nú"<tr><td>a</td><td>b</td><td/></tr>)r   r   r-   r   r   r   r   Ú it_can_serialize_the_row_to_htmlœ   s    ÿz0DescribeHtmlRow.it_can_serialize_the_row_to_htmlc                 C  s¶   t tdƒƒ}| ¡ }t|ƒ}t|tƒs*J ‚|jdks8J ‚t|ƒ}t|tƒsNJ ‚|jdks\J ‚t|ƒ}t|tƒsrJ ‚|jdks€J ‚t t	¡� t|ƒ W d   ƒ n1 s¨0    Y  d S )Nr=   z
<td>a</td>z
<td>b</td>ú<td/>)
r   r   Z
iter_cellsr4   r   r   r-   r(   r)   r5   )r   r6   Z	cell_iterÚcellr   r   r   Ú#it_can_iterate_the_cells_in_the_row¡   s    z3DescribeHtmlRow.it_can_iterate_the_cells_in_the_rowc                 C  sj   t tdƒƒ}| ¡ }t|ƒdks$J ‚t|ƒdks4J ‚t t¡� t|ƒ W d   ƒ n1 s\0    Y  d S )Nr=   ÚaÚb)r   r   Ziter_cell_textsr4   r(   r)   r5   )r   r6   Z	text_iterr   r   r   Ú0it_can_iterate_the_texts_of_the_cells_in_the_rowµ   s    z@DescribeHtmlRow.it_can_iterate_the_texts_of_the_cells_in_the_rowN)r   r   r   r   r>   rA   rD   r   r   r   r   r<   ™   s   r<   c                   @  s:   e Zd ZdZdd„ Zej dddg¡dddœdd	„ƒZd
S )ÚDescribeHtmlCellz>Unit-test suite for `unstructured.common.html_table.HtmlCell`.c                 C  s   t tdƒƒjdksJ ‚d S )Nz<td>a b c</td>)r   r   r-   r   r   r   r   Ú!it_can_serialize_the_cell_to_htmlÃ   s    z2DescribeHtmlCell.it_can_serialize_the_cell_to_html)Ú	cell_htmlÚexpected_value)z<td>  Lorem ipsum  </td>zLorem ipsum)r?   r
   r$   c                 C  s   t t|ƒƒj|ksJ ‚d S )N)r   r   r8   )r   rG   rH   r   r   r   Úit_knows_the_text_in_the_cellÆ   s    z.DescribeHtmlCell.it_knows_the_text_in_the_cellN)	r   r   r   r   rF   r(   r:   r;   rI   r   r   r   r   rE   À   s   þrE   )r   Ú
__future__r   r(   Z	lxml.htmlr   Zunstructured.common.html_tabler   r   r   r   r   r   r<   rE   r   r   r   r   Ú<module>   s   k'