def test_parse_cals_colspec(attrib, styles, nature): # --without namespaces cals_colspec = etree.Element("colspec", attrib=attrib) parser = CalsParser(BaseBuilder()) parser.setup_table() state = parser._state state.next_col() state = parser.parse_cals_colspec(cals_colspec) col_pos = int(attrib.get("colnum", 1)) col = state.table.cols[col_pos] assert col.styles == styles assert col.nature == nature # --with default namespaces cals_colspec_attrib = {cals(k): v for k, v in attrib.items()} cals_colspec = etree.Element(cals("colspec"), attrib=cals_colspec_attrib, nsmap={None: CALS_NS}) parser = CalsParser(BaseBuilder(), cals_ns=CALS_NS) parser.setup_table() state = parser._state state.next_col() state = parser.parse_cals_colspec(cals_colspec) col_pos = int(attrib.get("colnum", 1)) col = state.table.cols[col_pos] assert col.styles == styles assert col.nature == nature
def test_parse_cals_entry(attrib, styles, nature): # --without namespaces cals_entry = etree.Element("entry", attrib=attrib) parser = CalsParser(BaseBuilder()) parser.setup_table() state = parser._state state.next_row() state.row = state.table.rows[state.row_pos] state.next_col() state = parser.parse_cals_entry(cals_entry) cell = state.table[(1, 1)] assert cell.styles == styles assert cell.nature == nature # --with default namespaces cals_entry_attrib = {cals(k): v for k, v in attrib.items()} cals_entry = etree.Element(cals("entry"), attrib=cals_entry_attrib, nsmap={None: CALS_NS}) parser = CalsParser(BaseBuilder(), cals_ns=CALS_NS) parser.setup_table() state = parser._state state.next_row() state.row = state.table.rows[state.row_pos] state.next_col() state = parser.parse_cals_entry(cals_entry) cell = state.table[(1, 1)] assert cell.styles == styles assert cell.nature == nature
def test_parse_cals_tgroup__overrides_table(): # --without namespaces parser = CalsParser(BaseBuilder()) parser.setup_table( { "x-cell-border-right": BORDER_NONE, "x-cell-border-bottom": BORDER_NONE, "x-sect-orient": "landscape", "x-sect-cols": "1", "background-color": "velvet", }, "TableOld", ) cals_tgroup_attrib = { "colsep": "1", "rowsep": "1", "tgroupstyle": "TableNew" } cals_tgroup = etree.Element("tgroup", attrib=cals_tgroup_attrib) state = parser.parse_cals_tgroup(cals_tgroup) table = state.table assert table.styles == { "x-cell-border-right": BORDER_SOLID, "x-cell-border-bottom": BORDER_SOLID, "x-sect-orient": "landscape", # preserved "x-sect-cols": "1", # preserved "background-color": "velvet", # preserved } assert table.nature == "TableNew"
def test_parse_cals_row__overrides_parent_valign(): # --without namespaces cals_tbody = etree.Element("tbody", valign="top") cals_row = etree.SubElement(cals_tbody, "row", valign="middle") parser = CalsParser(BaseBuilder()) parser.setup_table() parser._state.next_row() state = parser.parse_cals_row(cals_row) row = state.table.rows[1] assert row.styles["vertical-align"] == "middle"
def test_parse_cals_row__nature_from_parent(tag, nature): # --without namespaces cals_parent = etree.Element(tag) cals_row = etree.SubElement(cals_parent, "row") parser = CalsParser(BaseBuilder()) parser.setup_table() parser._state.next_row() state = parser.parse_cals_row(cals_row) row = state.table.rows[1] assert row.nature == nature
def test_parse_cals_entry__spanning(attrib, size): # --without namespaces cals_entry = etree.Element("entry", attrib=attrib) parser = CalsParser(BaseBuilder()) parser.setup_table() state = parser._state state.next_row() state.row = state.table.rows[state.row_pos] state.next_col() state = parser.parse_cals_entry(cals_entry) cell = state.table[(1, 1)] assert cell.size == size
def test_parse_cals_tgroup(attrib, styles, nature): # --without namespaces cals_tgroup = etree.Element("tgroup", attrib=attrib) parser = CalsParser(BaseBuilder()) parser.setup_table() state = parser.parse_cals_tgroup(cals_tgroup) table = state.table assert table.styles == styles assert table.nature == nature # --with default namespaces cals_tgroup_attrib = {cals(k): v for k, v in attrib.items()} cals_tgroup = etree.Element(cals("tgroup"), attrib=cals_tgroup_attrib, nsmap={None: CALS_NS}) parser = CalsParser(BaseBuilder(), cals_ns=CALS_NS) parser.setup_table() state = parser.parse_cals_tgroup(cals_tgroup) table = state.table assert table.styles == styles assert table.nature == nature
def test_parse_cals_entry(attrib, styles, nature): # --without namespaces cals_entry = etree.Element("entry", attrib=attrib) parser = CalsParser(BaseBuilder()) parser.setup_table() state = parser._state state.next_row() state.row = state.table.rows[state.row_pos] state.next_col() state = parser.parse_cals_entry(cals_entry) cell = state.table[(1, 1)] # Ignore cell styles extensions (like 'x-cell-empty'). actual = { k: v for k, v in cell.styles.items() if not k.startswith("x-cell-") } assert actual == styles assert cell.nature == nature # --with default namespaces cals_entry_attrib = {cals(k): v for k, v in attrib.items()} cals_entry = etree.Element(cals("entry"), attrib=cals_entry_attrib, nsmap={None: CALS_NS}) parser = CalsParser(BaseBuilder(), cals_ns=CALS_NS) parser.setup_table() state = parser._state state.next_row() state.row = state.table.rows[state.row_pos] state.next_col() state = parser.parse_cals_entry(cals_entry) cell = state.table[(1, 1)] # Ignore cell styles extensions (like 'x-cell-empty'). actual = { k: v for k, v in cell.styles.items() if not k.startswith("x-cell-") } assert actual == styles assert cell.nature == nature
def test_parse_cals_row(attrib, styles, nature): # --without namespaces cals_row = etree.Element("row", attrib=attrib) parser = CalsParser(BaseBuilder()) parser.setup_table() parser._state.next_row() state = parser.parse_cals_row(cals_row) row = state.table.rows[1] assert row.styles == styles assert row.nature == nature # --with default namespaces cals_row_attrib = {cals(k): v for k, v in attrib.items()} cals_row = etree.Element(cals("row"), attrib=cals_row_attrib, nsmap={None: CALS_NS}) parser = CalsParser(BaseBuilder(), cals_ns=CALS_NS) parser.setup_table() parser._state.next_row() state = parser.parse_cals_row(cals_row) row = state.table.rows[1] assert row.styles == styles assert row.nature == nature
def test_setup_table(): parser = CalsParser(BaseBuilder()) state = parser.setup_table({"key": "value"}, nature="something") assert state.table is not None assert state.table.styles == {"key": "value"} assert state.table.nature == "something"