def test_table_to_html(): res = Table([Row([Cell(estr('test'), 1, 1)], False)]).to_html() assert res == '<table><tr><td>test</td></tr></table>' res = Table([Row([Cell(estr('test'), 2, 1)], False)]).to_html() assert res == '<table><tr><td colspan="2">test</td></tr></table>' res = Table([Row([Cell(estr('test'), 1, 2)], False)]).to_html() assert res == '<table><tr><td rowspan="2">test</td></tr></table>' res = Table([Row([Cell(estr('test'), 1, 2)], True)]).to_html() assert res == '<table><tr><th rowspan="2">test</th></tr></table>'
def test_build_table_with_correct_rowspan(): assert _build_table_with_correct_rowspan([], []).rows == [] table = _build_table_with_correct_rowspan([Row(_random_cells(4), False)], [[None, None, None, None]]) assert not table.rows[0].is_header table = _build_table_with_correct_rowspan([Row(_random_cells(4), True)], [[None, None, None, None]]) assert table.rows[0].is_header assert [cell.colspan for cell in table.rows[0].cells] == [1, 1, 1, 1] assert [cell.rowspan for cell in table.rows[0].cells] == [1, 1, 1, 1] table = _build_table_with_correct_rowspan([Row(_random_cells(4), True)], [[None, 'restart', None, None]]) assert [cell.colspan for cell in table.rows[0].cells] == [1, 1, 1, 1] assert [cell.rowspan for cell in table.rows[0].cells] == [1, 1, 1, 1] table = _build_table_with_correct_rowspan( [ Row(_random_cells(1), True), Row(_random_cells(1), False), Row(_random_cells(1), False) ], [['restart'], ['continue'], ['continue']], ) assert [cell.colspan for cell in table.rows[0].cells] == [1] assert [cell.colspan for cell in table.rows[1].cells] == [] assert [cell.colspan for cell in table.rows[2].cells] == [] assert [cell.rowspan for cell in table.rows[0].cells] == [3] assert [cell.rowspan for cell in table.rows[1].cells] == [] rows = [Row(_random_cells(4), True) for _ in range(3)] v_merge = [[None, 'restart', None, None], [None, 'continue', None, None], [None, 'continue', None, None]] table = _build_table_with_correct_rowspan(rows, v_merge) assert [cell.colspan for cell in table.rows[0].cells] == [1, 1, 1, 1] assert [cell.rowspan for cell in table.rows[0].cells] == [1, 3, 1, 1] assert [cell.rowspan for cell in table.rows[1].cells] == [1, 1, 1] assert [cell.rowspan for cell in table.rows[2].cells] == [1, 1, 1] rows = [Row(_random_cells(4), True) for _ in range(3)] v_merge = [[None, 'restart', None, None], [None, 'continue', None, None], [None, None, None, None]] table = _build_table_with_correct_rowspan(rows, v_merge) assert [cell.colspan for cell in table.rows[0].cells] == [1, 1, 1, 1] assert [cell.rowspan for cell in table.rows[0].cells] == [1, 2, 1, 1] assert [cell.rowspan for cell in table.rows[1].cells] == [1, 1, 1] assert [cell.rowspan for cell in table.rows[2].cells] == [1, 1, 1, 1] rows = [Row(_random_cells(3), True) for _ in range(4)] v_merge = [[None, 'restart', None], [None, 'continue', None], [None, 'restart', None], [None, 'continue', None]] table = _build_table_with_correct_rowspan(rows, v_merge) assert [cell.colspan for cell in table.rows[0].cells] == [1, 1, 1] assert [cell.rowspan for cell in table.rows[0].cells] == [1, 2, 1] assert [cell.rowspan for cell in table.rows[1].cells] == [1, 1] assert [cell.rowspan for cell in table.rows[2].cells] == [1, 2, 1] assert [cell.rowspan for cell in table.rows[3].cells] == [1, 1]
def _get_simple_text() -> StructuredText: row1 = Row([_cell('AA'), _cell('BB'), _cell('CC')], True) row2 = Row([_cell('DD', rs=2), _cell('EE', cs=2)], False) row3 = Row([_cell('FF'), _cell('GG')], False) table = Table([row1, row2, row3]) alineas = [ _text('Alinea 1'), _text('Alinea 2'), EnrichedString('', table=table) ] sections = [StructuredText(_text('Title 2'), [], [], None, None, None)] text = StructuredText(_text('Title'), alineas, sections, None, None, None) return text
def test_count_nb_cols_in_each_row(): assert _count_nb_cols_in_each_row(Table([])) == [] assert _count_nb_cols_in_each_row(Table([Row([], False)])) == [0] assert _count_nb_cols_in_each_row(_tuples_to_table([[(1, 1)]])) == [1] assert _count_nb_cols_in_each_row( _tuples_to_table([[(1, 1), (1, 1), (1, 1)]])) == [3] assert _count_nb_cols_in_each_row( _tuples_to_table([[(1, 1), (1, 1), (1, 1)], [(1, 1), (1, 1), (1, 1)]])) == [3, 3] assert _count_nb_cols_in_each_row( _tuples_to_table([[(1, 1), (2, 1)], [(1, 1), (1, 1), (1, 1)]])) == [3, 3] assert _count_nb_cols_in_each_row( _tuples_to_table([[(1, 1), (1, 1), (1, 2)], [(1, 1), (1, 1)]])) == [3, 3] tb = _tuples_to_table([[(1, 1), (1, 1), (1, 1)], [(1, 1), (1, 1), (1, 1), (1, 1)]]) assert _count_nb_cols_in_each_row(tb) == [3, 4] tb = _tuples_to_table([[(1, 1), (1, 1), (1, 1)], [(3, 3), (1, 1)]]) assert _count_nb_cols_in_each_row(tb) == [3, 4] tb = _tuples_to_table([[(1, 1), (1, 1), (1, 1)], [(3, 3), (1, 1)], [], []]) assert _count_nb_cols_in_each_row(tb) == [3, 4, 3, 3] tb = _tuples_to_table([[(1, 1), (1, 1), (1, 1)], [(3, 3), (1, 1)], [], [(1, 1)], [(1, 1), (1, 1), (1, 1), (1, 1)]]) assert _count_nb_cols_in_each_row(tb) == [3, 4, 3, 4, 4]
def _extract_row_data(row: Tag) -> Row: cell_iterator = row.find_all('td' if row.find('td') else 'th') res = [ Cell(_extract_cell_data(cell), int(cell.get('colspan') or 1), int(cell.get('rowspan') or 1)) # type: ignore for cell in cell_iterator ] return Row(res, _is_header(row))
def _table() -> Table: return Table([Row([Cell(EnrichedString('bonjour'), 1, 1)], True)])
def test_count_cells(): assert _count_cells(Table([])) == 0 assert _count_cells(Table([Row([], True)])) == 0 cells = [Cell(EnrichedString(''), 1, 1)] assert _count_cells(Table([Row(cells, True)])) == 1 assert _count_cells(Table([Row(cells, True)] * 3)) == 3
def _tuples_to_table(spans: List[List[Tuple[int, int]]]) -> Table: return Table([ Row([ Cell(estr(str((i, j))), cs, rs) for j, (cs, rs) in enumerate(row) ], False) for i, row in enumerate(spans) ])