コード例 #1
0
    def test_constructor(self) -> None:
        p = Paragraph()
        assert p.runs == []

        p = Paragraph([])
        assert p.runs == []

        p = Paragraph([Run("hi"), Run(" there")])
        assert p.runs == [Run("hi"), Run(" there")]

        p = Paragraph(Run("Hello"))
        assert p.runs == [Run("Hello")]

        p = Paragraph("World")
        assert p.runs == [Run("World")]
コード例 #2
0
    def _transform_function(
            self, function: Function) -> List[Union[Paragraph, ListingTable]]:
        rows = []

        # Function definiton
        args = ", ".join(a.name for a in function.arguments)
        rows.append(
            ListingLine(
                fdef(function.name) + code(f"({args}):"),
                self._transform_eol_comment(function.eol),
            ))

        # Function body
        for stmt in function.body:
            rows.extend(self._indent_listing_lines(self._transform_stmt(stmt)))

        return [ListingTable(rows), Paragraph()]
コード例 #3
0
def test_sanity(tmpdir: Any) -> None:
    # Just writes a document and checks nothing crashes...
    doc = ListingDocument([
        # Paragraph
        Paragraph("Hello"),
        # Paragrpah with runs with multiple styles
        Paragraph([Run(style.name, style) for style in RunStyle]),
        # Empty paragraph
        Paragraph(),
        # Table with comments
        ListingTable([
            ListingLine(Paragraph("Code 1"), Paragraph("Comment 1")),
            ListingLine(Paragraph("Code 2"), Paragraph("Comment 2")),
            ListingLine(Paragraph("Code 3"), Paragraph("Comment 3")),
        ]),
        # Table with no comments
        ListingTable([
            ListingLine(Paragraph("Code 1"), Paragraph("")),
            ListingLine(Paragraph("Code 2"), Paragraph("")),
            ListingLine(Paragraph("Code 3"), Paragraph("")),
        ]),
        # Empty table
        ListingTable([]),
    ])

    filename = str(tmpdir.join("test.docx"))
    docx_doc = doc.make_docx_document()
    docx_doc.save(filename)
コード例 #4
0
    def test_add(self) -> None:
        # __add__
        assert Paragraph() + Paragraph() == Paragraph()
        assert Paragraph("foo") + Paragraph("bar") == Paragraph(
            [Run("foo"), Run("bar")])
        assert Paragraph("foo") + Run("bar") == Paragraph(
            [Run("foo"), Run("bar")])
        assert Paragraph("foo") + "bar" == Paragraph([Run("foo"), Run("bar")])

        # __radd__
        assert Run("foo") + Paragraph("bar") == Paragraph(
            [Run("foo"), Run("bar")])
        assert "foo" + Paragraph("bar") == Paragraph([Run("foo"), Run("bar")])
コード例 #5
0
 def test_str(self) -> None:
     assert str(Paragraph()) == ""
     assert str(Paragraph("")) == ""
     assert str(Paragraph("Foo")) == "Foo"
     assert str(Paragraph([Run("Hello"), Run(" world")])) == "Hello world"
コード例 #6
0
 def test_bool(self) -> None:
     assert bool(Paragraph()) is False
     assert bool(Paragraph("")) is False
     assert bool(Paragraph("hi")) is True
コード例 #7
0
 def _transform_eol_comment(self, eol: Optional[EOL]) -> Paragraph:
     if eol is None or eol.comment is None or eol.comment.string.lstrip(
             "# ") == "":
         return Paragraph("")
     else:
         return Paragraph(eol.comment.string.lstrip("# ").rstrip())
コード例 #8
0
def label(string: str) -> Paragraph:
    return Paragraph(Run(string, RunStyle.pseudocode_label))
コード例 #9
0
def fdef(string: str) -> Paragraph:
    return Paragraph(Run(string, RunStyle.pseudocode_fdef))
コード例 #10
0
def keyword(string: str) -> Paragraph:
    return Paragraph(Run(string, RunStyle.pseudocode_keyword))
コード例 #11
0
def code(string: str) -> Paragraph:
    return Paragraph(Run(string, RunStyle.pseudocode))