Ejemplo n.º 1
0
 def test_init(self):
     builder = CalsBuilder()
     assert builder.cals_ns.uri is None
     assert builder.cals_ns.prefix is None
     assert builder.ns_map == {}
     assert builder.width_unit == "mm"
     assert builder.table_in_tgroup is False
     assert builder.tgroup_sorting == {"header": 0, "footer": 1, "body": 2}
Ejemplo n.º 2
0
def test_build_cell__with_sep(cell_styles, expected):
    # .. note::
    #
    #   CALS default value for @colsep and @rowsep is "1".

    builder = CalsBuilder()

    # -- create a minimal <row> element
    row_elem = etree.XML("<row/>")

    # -- setup a minimal cell
    p_elem = etree.XML("<p>text</p>")
    cell_x1_y1 = Cell([p_elem], x=1, y=1, styles=cell_styles)
    cell_x2_y1 = Cell([p_elem], x=2, y=1, styles=cell_styles)
    cell_x1_y2 = Cell([p_elem], x=1, y=2, styles=cell_styles)
    cell_x2_y2 = Cell([p_elem], x=2, y=2, styles=cell_styles)
    builder.setup_table(Table([cell_x1_y1, cell_x2_y1, cell_x1_y2,
                               cell_x2_y2]))

    # -- build the cell
    builder.build_cell(row_elem, cell_x1_y1)

    # -- check the '<entry>' attributes
    entry_elem = row_elem[0]  # type: ElementType
    assert entry_elem.tag == u"entry"
    assert entry_elem.attrib == expected
    assert entry_elem[0] == p_elem
Ejemplo n.º 3
0
def test_build_cell__align(cell_styles, expected):
    builder = CalsBuilder()

    # -- create a minimal <row> element
    row_elem = etree.XML("<row/>")

    # -- setup a minimal cell
    p_elem = etree.XML("<p>text</p>")
    cell = Cell([p_elem], x=1, y=1, styles=cell_styles)
    builder.setup_table(Table([cell]))

    # -- build the cell
    builder.build_cell(row_elem, cell)

    # -- check the '<entry>' attributes
    entry_elem = row_elem[0]  # type: ElementType
    assert entry_elem.tag == u"entry"

    # we don't want to check @colsep/@rowsep here
    actual = dict(entry_elem.attrib)
    actual.pop("colsep", None)
    actual.pop("rowsep", None)

    assert actual == expected
    assert entry_elem[0] == p_elem
Ejemplo n.º 4
0
def test_build_tgroup__tgroup_sorting(tgroup_sorting, expected_tags):
    # -- create a table with different natures
    table = Table()
    row1 = table.rows[1]
    row2 = table.rows[2]
    row3 = table.rows[3]
    row1.nature = "header"
    row2.nature = "body"
    row3.nature = "footer"
    row1.insert_cell("a")
    row2.insert_cell("b")
    row3.insert_cell("c")

    # -- create a builder
    table_elem = etree.Element("table")
    builder = CalsBuilder(tgroup_sorting=tgroup_sorting)
    builder.setup_table(table)
    builder.build_tgroup(table_elem, table)

    # -- check the tgroup children name and order
    actual_tags = [elem.tag for elem in table_elem.xpath("tgroup/*")]
    assert actual_tags == expected_tags
Ejemplo n.º 5
0
def test_setup_table():
    builder = CalsBuilder()
    table = Table()
    result = builder.setup_table(table)
    assert result == table
Ejemplo n.º 6
0
 def test_option_tgroup_sorting__missing(self):
     with pytest.raises(ValueError):
         CalsBuilder(tgroup_sorting=["header", "body"])
Ejemplo n.º 7
0
 def test_option_tgroup_sorting(self):
     builder = CalsBuilder(tgroup_sorting=["header", "body", "footer"])
     assert builder.tgroup_sorting == {"header": 0, "body": 1, "footer": 2}
Ejemplo n.º 8
0
 def test_option_table_in_tgroup(self):
     builder = CalsBuilder(table_in_tgroup=True)
     assert builder.table_in_tgroup is True
Ejemplo n.º 9
0
 def test_option_width_unit(self):
     builder = CalsBuilder(width_unit="pt")
     assert builder.width_unit == "pt"
Ejemplo n.º 10
0
 def test_option_cals_prefix__without_ns(self):
     with self.assertRaises(ValueError):
         CalsBuilder(cals_prefix="cals")
Ejemplo n.º 11
0
 def test_option_cals_prefix(self):
     builder = CalsBuilder(cals_ns="http://cals", cals_prefix="cals")
     assert builder.cals_ns.prefix == "cals"
     assert builder.ns_map == {"cals": "http://cals"}
Ejemplo n.º 12
0
def test_build_table():
    # see: formex-4/samples/jo-compl-2002C_061/C_2002061EN.01000403.xml

    table = Table(
        styles={
            "border-top": "solid",
            "border-bottom": "solid",
            "x-sect-orient": "landscape",
            "x-sect-cols": "1",
            "background-color": "yellow",
            "width": "247",
        })
    table.rows[1].nature = "header"
    table.rows[1].insert_cell(u"Expert group")
    table.rows[1].insert_cell(u"First name and surname of the expert")
    table.rows[2].insert_cell(u"Control of infectious diseases")
    table.rows[2].insert_cell(u"Michael Angelo BORG")
    table.rows[3].insert_cell(u"Information society", height=3)
    table.rows[3].insert_cell(u"Tony HEY")
    table.rows[4].insert_cell(u"José L. ENCARNAÇÃO")
    table.rows[5].insert_cell(u"Berit SVENDSEN")
    table.rows[6].insert_cell(u"Controlled thermonuclear fusion")
    table.rows[6].insert_cell(u"Pekka PIRILÄ")

    builder = CalsBuilder()
    table_elem = builder.build_table(table)

    xml_parser = etree.XMLParser(remove_blank_text=True)

    # fmt: off
    expected = etree.XML(u"""\
    <table frame="topbot" colsep="0" rowsep="0" orient="land" pgwide="1" bgcolor="yellow" width="247.00mm">
      <tgroup cols="2">
        <colspec colnum="1" colname="c1"/>
        <colspec colnum="2" colname="c2"/>
        <thead>
          <row>
            <entry>Expert group</entry>
            <entry>First name and surname of the expert</entry>
          </row>
        </thead>
        <tbody>
          <row>
            <entry>Control of infectious diseases</entry>
            <entry>Michael Angelo BORG</entry>
          </row>
          <row>
            <entry morerows="2">Information society</entry>
            <entry>Tony HEY</entry>
          </row>
          <row>
            <entry>José L. ENCARNAÇÃO</entry>
          </row>
          <row>
            <entry>Berit SVENDSEN</entry>
          </row>
          <row>
            <entry>Controlled thermonuclear fusion</entry>
            <entry>Pekka PIRILÄ</entry>
          </row>
        </tbody>
      </tgroup>
    </table>""",
                         parser=xml_parser)
    # fmt: on

    for elem in table_elem.xpath("//*"):
        elem.text = elem.text or None
    for elem in expected.xpath("//*"):
        elem.text = elem.text or None

    diff_list = xmldiff.main.diff_trees(table_elem, expected)
    if diff_list:
        print(etree.tounicode(table_elem, pretty_print=True, with_tail=False),
              file=sys.stderr)
        assert diff_list == []
Ejemplo n.º 13
0
 def test_option_cals_ns(self):
     builder = CalsBuilder(cals_ns="http://cals")
     assert builder.cals_ns.uri == "http://cals"
     assert builder.ns_map == {None: "http://cals"}