def setUp(self):
     unittest.TestCase.setUp(self)
     self.maxDiff = None
     self.doc = Document()
class WrappingTestCase(unittest.TestCase):
    __lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque facilisis nisi vel nibh luctus sit amet semper tellus gravida. Proin lorem libero, aliquet vitae suscipit ac, egestas sit amet velit. In justo nisi, porttitor vel fermentum id, feugiat eget eros. Nullam vulputate risus tempor odio suscipit sit amet ornare est rhoncus. Vestibulum malesuada mattis sollicitudin. Duis ac lectus ac neque semper euismod imperdiet nec eros. Ut ac odio libero. Morbi a diam quis libero volutpat euismod. Etiam gravida fringilla erat quis facilisis. Morbi venenatis malesuada dapibus. Phasellus libero dui, congue a tincidunt ut, cursus in risus. Ut sapien sapien, scelerisque at hendrerit sed, vestibulum a sem. Sed vitae odio vel est aliquam suscipit ut gravida quam. Morbi a faucibus ipsum. In eros orci, feugiat et scelerisque non, faucibus et eros."
    __shortLorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque facilisis nisi vel nibh"

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.maxDiff = None
        self.doc = Document()

    def testSingleParagraph(self):
        self.doc.add(Paragraph(self.__lorem))
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                # 70 chars ###########################################################
                """\
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque
                facilisis nisi vel nibh luctus sit amet semper tellus gravida. Proin
                lorem libero, aliquet vitae suscipit ac, egestas sit amet velit. In
                justo nisi, porttitor vel fermentum id, feugiat eget eros. Nullam
                vulputate risus tempor odio suscipit sit amet ornare est rhoncus.
                Vestibulum malesuada mattis sollicitudin. Duis ac lectus ac neque
                semper euismod imperdiet nec eros. Ut ac odio libero. Morbi a diam
                quis libero volutpat euismod. Etiam gravida fringilla erat quis
                facilisis. Morbi venenatis malesuada dapibus. Phasellus libero dui,
                congue a tincidunt ut, cursus in risus. Ut sapien sapien, scelerisque
                at hendrerit sed, vestibulum a sem. Sed vitae odio vel est aliquam
                suscipit ut gravida quam. Morbi a faucibus ipsum. In eros orci,
                feugiat et scelerisque non, faucibus et eros.
                """
            )
        )

    def testParagraphInSubSection(self):
        self.doc.add(Section("Section").add(Section("Sub-section").add(Paragraph(self.__lorem))))
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                # 70 chars ###########################################################
                """\
                Section:
                  Sub-section:
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                    Pellentesque facilisis nisi vel nibh luctus sit amet semper tellus
                    gravida. Proin lorem libero, aliquet vitae suscipit ac, egestas
                    sit amet velit. In justo nisi, porttitor vel fermentum id, feugiat
                    eget eros. Nullam vulputate risus tempor odio suscipit sit amet
                    ornare est rhoncus. Vestibulum malesuada mattis sollicitudin. Duis
                    ac lectus ac neque semper euismod imperdiet nec eros. Ut ac odio
                    libero. Morbi a diam quis libero volutpat euismod. Etiam gravida
                    fringilla erat quis facilisis. Morbi venenatis malesuada dapibus.
                    Phasellus libero dui, congue a tincidunt ut, cursus in risus. Ut
                    sapien sapien, scelerisque at hendrerit sed, vestibulum a sem. Sed
                    vitae odio vel est aliquam suscipit ut gravida quam. Morbi a
                    faucibus ipsum. In eros orci, feugiat et scelerisque non, faucibus
                    et eros.
                """
            )
        )

    def testLongSectionTitles(self):
        self.doc.add(
            Section("Section " + self.__shortLorem)
            .add(
                Section("Sub-section " + self.__shortLorem)
                .add(Paragraph("Some text"))
            )
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                # 70 chars ###########################################################
                """\
                Section Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                Pellentesque facilisis nisi vel nibh:
                  Sub-section Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                  Pellentesque facilisis nisi vel nibh:
                    Some text
                """
            )
        )
class IndentationTestCase(unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)
        self.doc = Document()

    def testEmptyDocument(self):
        self.assertEqual(self.doc.format(), "\n")

    def testAddNoneIsNoOp(self):
        self.doc.add(None)
        self.assertEqual(self.doc.format(), "\n")

    def testOneSectionWithOneParagraph(self):
        self.doc.add(
            Section("First section")
            .add(Paragraph("Some text"))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                First section:
                  Some text
                """
            )
        )

    def testOneSectionWithTwoParagraphs(self):
        self.doc.add(
            Section("First section")
            .add(Paragraph("Some text"))
            .add(Paragraph("Some other text"))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                First section:
                  Some text

                  Some other text
                """
            )
        )

    def testSeveralSectionsWithSeveralParagraphs(self):
        self.doc.add(
            Section("Section A")
            .add(Paragraph("Text A.1"))
            .add(Paragraph("Text A.2"))
            .add(Paragraph("Text A.3"))
        ).add(
            Section("Section B")
            .add(Paragraph("Text B.1"))
            .add(Paragraph("Text B.2"))
            .add(Paragraph("Text B.3"))
        ).add(
            Section("Section C")
            .add(Paragraph("Text C.1"))
            .add(Paragraph("Text C.2"))
            .add(Paragraph("Text C.3"))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Section A:
                  Text A.1

                  Text A.2

                  Text A.3

                Section B:
                  Text B.1

                  Text B.2

                  Text B.3

                Section C:
                  Text C.1

                  Text C.2

                  Text C.3
                """
            )
        )

    def testParagraphThenSection(self):
        self.doc.add(
            Paragraph("Some text")
        ).add(
            Section("Section title")
            .add(Paragraph("Section text"))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Some text

                Section title:
                  Section text
                """
            )
        )

    def testSectionThenParagraph(self):
        self.doc.add(
            Section("Section title")
            .add(Paragraph("Section text"))
        ).add(
            Paragraph("Some text")
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Section title:
                  Section text

                Some text
                """
            )
        )

    def testEmptySection(self):
        self.doc.add(
            Section("Empty section title")
        ).add(
            Paragraph("Some text")
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Empty section title:

                Some text
                """
            )
        )

    def testImbricatedSections(self):
        self.doc.add(
            Section("Section A")
            .add(Section("Section A.1").add(Paragraph("Text A.1.a")).add(Paragraph("Text A.1.b")))
            .add(Section("Section A.2").add(Paragraph("Text A.2.a")).add(Paragraph("Text A.2.b")))
        ).add(
            Section("Section B")
            .add(Section("Section B.1").add(Paragraph("Text B.1.a")).add(Paragraph("Text B.1.b")))
            .add(Section("Section B.2").add(Paragraph("Text B.2.a")).add(Paragraph("Text B.2.b")))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Section A:
                  Section A.1:
                    Text A.1.a

                    Text A.1.b

                  Section A.2:
                    Text A.2.a

                    Text A.2.b

                Section B:
                  Section B.1:
                    Text B.1.a

                    Text B.1.b

                  Section B.2:
                    Text B.2.a

                    Text B.2.b
                """
            )
        )

    def testRecursiveContainersIsSameAsFlatContainer(self):
        self.doc.add(
            Container()
            .add(Paragraph("P1"))
            .add(Paragraph("P2"))
            .add(
                Container()
                .add(Paragraph("P3"))
                .add(Paragraph("P4"))
                .add(
                    Container()
                    .add(Paragraph("P5"))
                )
                .add(
                    Container()
                    .add(Paragraph("P6"))
                )
            )
            .add(Paragraph("P7"))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                P1

                P2

                P3

                P4

                P5

                P6

                P7
                """
            )
        )

    def testEmptyContainer(self):
        self.doc.add(Container())
        self.assertEqual(self.doc.format(), "\n")

    def testEmptySection2(self):
        self.doc.add(Section("Title"))
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Title:
                """
            )
        )

    def testRecursiveEmptyContainers(self):
        self.doc.add(
            Container().add(
                Container().add(
                    Container().add(
                        Container()
                    )
                )
            )
        )
        self.assertEqual(self.doc.format(), "\n")

    def testSuccessiveEmptyContainers(self):
        self.doc.add(
            Container()
        ).add(
            Container()
        ).add(
            Container()
        ).add(
            Container()
        ).add(
            Container()
        )
        self.assertEqual(self.doc.format(), "\n")

    def testEmptyContainersAfterParagraph(self):
        self.doc.add(
            Paragraph("Foobar")
        ).add(
            Container()
        ).add(
            Container()
        )
        self.assertEqual(self.doc.format(), "Foobar\n")

    def testEmptyContainersBeforeParagraph(self):
        self.doc.add(
            Container()
        ).add(
            Container()
        ).add(
            Paragraph("Foobar")
        )
        self.assertEqual(self.doc.format(), "Foobar\n")
 def setUp(self):
     unittest.TestCase.setUp(self)
     self.doc = Document()
class DefinitionListTestCase(unittest.TestCase):
    __shortLorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque facilisis nisi vel nibh"

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.maxDiff = None
        self.doc = Document()

    def testDefinitionList(self):
        self.doc.add(
            DefinitionList()
            .add("Item 1", Paragraph("Definition 1"))
            .add("Item 2", Paragraph("Definition 2"))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Item 1  Definition 1
                Item 2  Definition 2
                """
            )
        )

    def testItemsWithDifferentLengths(self):
        self.doc.add(
            DefinitionList()
            .add("Item 1", Paragraph("Definition 1"))
            .add("Longer item 2", Paragraph("Definition 2"))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Item 1         Definition 1
                Longer item 2  Definition 2
                """
            )
        )

    def testWithinSubSection(self):
        self.doc.add(
            Section("Section")
            .add(
                Section("Sub-section")
                .add(
                    DefinitionList()
                    .add("Item 1", Paragraph("Definition 1"))
                    .add("Longer item 2", Paragraph("Definition 2"))
                )
            )
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Section:
                  Sub-section:
                    Item 1         Definition 1
                    Longer item 2  Definition 2
                """
            )
        )

    def testEmptyDefinition(self):
        self.doc.add(
            DefinitionList()
            .add("Longer item 1", Paragraph("Definition 1"))
            .add("Item 2", Paragraph(""))
            .add("Longer item 3", Paragraph(""))
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Longer item 1  Definition 1
                Item 2
                Longer item 3
                """
            )
        )

    def testWrappingOfDefinitionWithOnlyShortItems(self):
        self.doc.add(
            Section("Section")
            .add(
                DefinitionList()
                .add("Item 1 (short enought)", Paragraph("Definition 1 " + self.__shortLorem))
                .add("Item 2", Paragraph("Definition 2 " + self.__shortLorem))
            )
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                # 70 chars ###########################################################
                """\
                Section:
                  Item 1 (short enought)  Definition 1 Lorem ipsum dolor sit amet,
                                          consectetur adipiscing elit. Pellentesque
                                          facilisis nisi vel nibh
                  Item 2                  Definition 2 Lorem ipsum dolor sit amet,
                                          consectetur adipiscing elit. Pellentesque
                                          facilisis nisi vel nibh
                """
            )
        )

    def testWrappingOfDefinitionWithShortAndLongItems(self):
        self.doc.add(
            Section("Section")
            .add(
                DefinitionList()
                .add("Item 1 (just tooo long)", Paragraph("Definition 1 " + self.__shortLorem))
                .add("Item 2", Paragraph("Definition 2 " + self.__shortLorem))
            )
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                # 70 chars ###########################################################
                """\
                Section:
                  Item 1 (just tooo long)
                          Definition 1 Lorem ipsum dolor sit amet, consectetur
                          adipiscing elit. Pellentesque facilisis nisi vel nibh
                  Item 2  Definition 2 Lorem ipsum dolor sit amet, consectetur
                          adipiscing elit. Pellentesque facilisis nisi vel nibh
                """
            )
        )

    def testWrappingOfDefinitionWithOnlyLongItems(self):
        self.doc.add(
            Section("Section")
            .add(
                DefinitionList()
                .add("Item 1 (just tooo long)", Paragraph("Definition 1 " + self.__shortLorem))
                .add("Item 2 (also too long, really)", Paragraph("Definition 2 " + self.__shortLorem))
            )
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                # 70 chars ###########################################################
                """\
                Section:
                  Item 1 (just tooo long)
                    Definition 1 Lorem ipsum dolor sit amet, consectetur adipiscing
                    elit. Pellentesque facilisis nisi vel nibh
                  Item 2 (also too long, really)
                    Definition 2 Lorem ipsum dolor sit amet, consectetur adipiscing
                    elit. Pellentesque facilisis nisi vel nibh
                """
            )
        )

    def testContainerAsDefinition(self):
        self.doc.add(
            Section("Section")
            .add(
                DefinitionList()
                .add("Item", Container().add(Paragraph("Para 1")).add(Paragraph("Para 2")))
            )
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Section:
                  Item  Para 1

                        Para 2
                """
            )
        )

    def testDefinitionListAsDefinition(self):
        self.doc.add(
            Section("Section")
            .add(
                DefinitionList()
                .add(
                    "Item 1",
                    DefinitionList()
                    .add("Item A", Paragraph("Definition A"))
                    .add("Item B", Paragraph("Definition B"))
                )
                .add(
                    "Item 2",
                    DefinitionList()
                    .add("Item C", Paragraph("Definition C"))
                    .add("Item D", Paragraph("Definition D"))
                )
            )
        )
        self.assertEqual(
            self.doc.format(),
            textwrap.dedent(
                """\
                Section:
                  Item 1  Item A  Definition A
                          Item B  Definition B
                  Item 2  Item C  Definition C
                          Item D  Definition D
                """
            )
        )